Compare commits

..

No commits in common. "6a4892944babe89a7e0b85ec1cd1bddd13a7b7a9" and "60b4af4139337a3025fd30100f3a3ca191fb2e07" have entirely different histories.

6 changed files with 66 additions and 26 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
.env* .env
*.sqlite3 *.sqlite3
nitterbot unpaywall

View File

@ -1,2 +0,0 @@
nitterbot: *.go go.sum go.mod
go build -o nitterbot .

6
db.go
View File

@ -42,9 +42,7 @@ func ItemHasComment(parentId int) bool {
err := db.QueryRow(`SELECT COUNT(1) FROM comments WHERE parent_id = ?`, parentId).Scan(&count) err := db.QueryRow(`SELECT COUNT(1) FROM comments WHERE parent_id = ?`, parentId).Scan(&count)
if err != nil { if err != nil {
err = fmt.Errorf("error during item check: %w", err) err = fmt.Errorf("error during item check: %w", err)
log.Println(err) log.Fatal(err)
// pretend that item has comment to avoid duplicate comments
return true
} }
return count > 0 return count > 0
} }
@ -53,6 +51,6 @@ func SaveComment(comment *sn.Comment) {
_, err := db.Exec(`INSERT INTO comments(id, text, parent_id) VALUES (?, ?, ?)`, comment.Id, comment.Text, comment.ParentId) _, err := db.Exec(`INSERT INTO comments(id, text, parent_id) VALUES (?, ?, ?)`, comment.Id, comment.Text, comment.ParentId)
if err != nil { if err != nil {
err = fmt.Errorf("error during item insert: %w", err) err = fmt.Errorf("error during item insert: %w", err)
log.Println(err) log.Fatal(err)
} }
} }

2
go.mod
View File

@ -3,6 +3,6 @@ module git.ekzyis.com/ekzyis/unpaywall
go 1.21.0 go 1.21.0
require ( require (
github.com/ekzyis/snappy v0.5.3 github.com/ekzyis/snappy v0.5.2
github.com/mattn/go-sqlite3 v1.14.17 github.com/mattn/go-sqlite3 v1.14.17
) )

4
go.sum
View File

@ -1,4 +1,4 @@
github.com/ekzyis/snappy v0.5.3 h1:Pq8b2s4WPz5gnHgCoZR2LVqArEVqWeVCyHSVZCawat4= github.com/ekzyis/snappy v0.5.2 h1:3dZyj3Kz7fV3OStllq/koL6d5qNfBRK+4UB2L3RNmiQ=
github.com/ekzyis/snappy v0.5.3/go.mod h1:BxJwdGlCwUw0Q5pQzBr59weAIS6pkVdivBBaZkkWTSo= github.com/ekzyis/snappy v0.5.2/go.mod h1:BxJwdGlCwUw0Q5pQzBr59weAIS6pkVdivBBaZkkWTSo=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

74
main.go
View File

@ -25,6 +25,31 @@ var (
NitterClearnetUrls = []string{ NitterClearnetUrls = []string{
"xcancel.com", "xcancel.com",
} }
// since v0.4.0, bot also replaces nostr links with nostr.com so users can pick their client
NostrUrlRegexp = regexp.MustCompile(
`^(?:https?:\/\/)?(?:www\.)?` +
`(?:` +
`primal\.net\/(?:e\/)?` +
`|snort\.social\/(?:e\/)?` +
`|iris\.to\/` +
`|highlighter\.com\/(?:a\/)?` +
`|nostter\.app\/` +
`|coracle\.social\/(?:notes\/)?` +
`|satellite\.earth\/` +
`|nostrudel\.ninja\/(?:#\/n\/)?` +
`)((note|nevent)[a-zA-Z0-9]+)$`)
NostrClients = []NostrClient{
// list from nostr.com
NostrClient{"https://primal.net/e/", "primal.net"},
NostrClient{"https://snort.social/e/", "snort.social"},
NostrClient{"https://nostrudel.ninja/#/n/", "nostrudel.ninja"},
NostrClient{"https://satellite.earth/thread/", "satellite.earth"},
NostrClient{"https://coracle.social/", "coracle.social"},
NostrClient{"https://nostter.app/", "nostter.app"},
NostrClient{"https://highlighter.com/a/", "highlighter.com"},
NostrClient{"https://iris.to/", "iris.to"},
}
) )
func WaitUntilNext(d time.Duration) { func WaitUntilNext(d time.Duration) {
@ -44,7 +69,7 @@ func main() {
for { for {
log.Println("fetching items ...") log.Println("fetching items ...")
r, err := c.Items(&sn.ItemsQuery{Sort: "recent", Type: "all", Limit: 100}) r, err := c.Items(&sn.ItemsQuery{Sort: "recent", Limit: 21})
if err != nil { if err != nil {
log.Println(err) log.Println(err)
SendToNostr(fmt.Sprint(err)) SendToNostr(fmt.Sprint(err))
@ -53,35 +78,54 @@ func main() {
} }
for _, item := range r.Items { for _, item := range r.Items {
var m []string if m := TwitterUrlRegexp.FindStringSubmatch(item.Url); m != nil {
var comment string
if m = TwitterUrlRegexp.FindStringSubmatch(item.Url); m != nil {
comment = strings.Replace(item.Url, m[1], "xcancel.com", 1)
} else if m = TwitterUrlRegexp.FindStringSubmatch(item.Text); m != nil {
comment = strings.Replace(item.Text, m[1], "xcancel.com", 1)
}
if comment != "" {
log.Printf("item %d is twitter link\n", item.Id) log.Printf("item %d is twitter link\n", item.Id)
if ItemHasComment(item.Id) { if ItemHasComment(item.Id) {
log.Printf("item %d already has nitter links comment\n", item.Id) log.Printf("item %d already has nitter links comment\n", item.Id)
continue continue
} }
comment := "**Twitter2Nitter**\n\nClearnet: "
for _, nUrl := range NitterClearnetUrls {
nitterLink := strings.Replace(item.Url, m[1], nUrl, 1)
comment += fmt.Sprintf("[%s](%s) | ", nUrl, nitterLink)
}
comment = strings.TrimRight(comment, "| ")
comment += "\n\n_Nitter is a free and open source alternative Twitter front-end focused on privacy and performance. "
comment += "Click [here](https://github.com/zedeus/nitter) for more information._"
cId, err := c.CreateComment(item.Id, comment) cId, err := c.CreateComment(item.Id, comment)
if err != nil { if err != nil {
log.Println("create comment failed:", err) log.Println(err)
SendToNostr(fmt.Sprint(err)) SendToNostr(fmt.Sprint(err))
continue continue
} }
log.Printf("created comment %d\n", cId) log.Printf("created comment %d\n", cId)
SaveComment(&sn.Comment{Id: cId, Text: comment, ParentId: item.Id}) SaveComment(&sn.Comment{Id: cId, Text: comment, ParentId: item.Id})
} else { } else {
log.Printf("item %d is not twitter link\n", item.Id) log.Printf("item %d is not twitter link\n", item.Id)
} }
if m := NostrUrlRegexp.FindStringSubmatch(item.Url); m != nil {
log.Printf("item %d is nostr link\n", item.Id)
if ItemHasComment(item.Id) {
log.Printf("item %d already has nostr links comment\n", item.Id)
continue
}
noteId := m[1]
comment := "**Nostr Client Picker**\n\n"
for _, client := range NostrClients {
comment += fmt.Sprintf("[%s](%s) | ", client.Name, client.Url+noteId)
}
comment = strings.TrimRight(comment, "| ")
cId, err := c.CreateComment(item.Id, comment)
if err != nil {
log.Println(err)
SendToNostr(fmt.Sprint(err))
continue
}
log.Printf("created comment %d\n", cId)
SaveComment(&sn.Comment{Id: cId, Text: comment, ParentId: item.Id})
} else {
log.Printf("item %d is not nostr link\n", item.Id)
}
} }
WaitUntilNext(time.Minute) WaitUntilNext(time.Minute)