From 18809164185ec83dff0293a4f6fd8de12354c117 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 3 Jul 2024 08:49:33 +0200 Subject: [PATCH] Update to snappy v0.5.2 --- .env.template | 2 +- db.go | 2 +- go.mod | 7 +---- go.sum | 10 ++----- main.go | 81 +++++++++++++++++++++++++++++---------------------- 5 files changed, 51 insertions(+), 51 deletions(-) diff --git a/.env.template b/.env.template index f487789..a92fb8e 100644 --- a/.env.template +++ b/.env.template @@ -1 +1 @@ -SN_AUTH_COOKIE= \ No newline at end of file +SN_API_KEY= \ No newline at end of file diff --git a/db.go b/db.go index 2540bd3..076978b 100644 --- a/db.go +++ b/db.go @@ -5,7 +5,7 @@ import ( "fmt" "log" - "github.com/ekzyis/sn-goapi" + sn "github.com/ekzyis/snappy" _ "github.com/mattn/go-sqlite3" ) diff --git a/go.mod b/go.mod index ea1400e..b1ded81 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,6 @@ module git.ekzyis.com/ekzyis/unpaywall go 1.21.0 require ( - github.com/ekzyis/sn-goapi v0.3.3 + github.com/ekzyis/snappy v0.5.2 github.com/mattn/go-sqlite3 v1.14.17 ) - -require ( - github.com/joho/godotenv v1.5.1 // indirect - github.com/namsral/flag v1.7.4-pre // indirect -) diff --git a/go.sum b/go.sum index 2ee13ca..0e7e9fe 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,4 @@ -github.com/ekzyis/sn-goapi v0.3.2 h1:BGY2tOD9IZD40UXUpexysWoC+eLEoUs7JRo9FQ37S6M= -github.com/ekzyis/sn-goapi v0.3.2/go.mod h1:FObbYr/NXgnXNWU+EwiWKoWQy+wAaRS6AoW3NgsJ/Oo= -github.com/ekzyis/sn-goapi v0.3.3 h1:5WHGLyYVPwZ12lQrRD40eM+gjWEpDdgdWTshwL8CDEE= -github.com/ekzyis/sn-goapi v0.3.3/go.mod h1:FObbYr/NXgnXNWU+EwiWKoWQy+wAaRS6AoW3NgsJ/Oo= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/ekzyis/snappy v0.5.2 h1:3dZyj3Kz7fV3OStllq/koL6d5qNfBRK+4UB2L3RNmiQ= +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/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs= -github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo= diff --git a/main.go b/main.go index 8ba9676..ec42b65 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,15 @@ package main import ( + "bufio" "fmt" "log" + "os" "regexp" "strings" "time" - "github.com/ekzyis/sn-goapi" + sn "github.com/ekzyis/snappy" ) type NostrClient struct { @@ -57,41 +59,17 @@ func WaitUntilNext(d time.Duration) { time.Sleep(dur) } -func CheckNotifications() { - var prevHasNewNotes bool - for { - log.Println("Checking notifications ...") - hasNewNotes, err := sn.CheckNotifications() - if err != nil { - SendToNostr(fmt.Sprint(err)) - } else { - if !prevHasNewNotes && hasNewNotes { - // only send on "rising edge" - SendToNostr("new notifications") - log.Println("Forwarded notifications to monitoring") - } else if hasNewNotes { - log.Println("Notifications already forwarded") - } - } - prevHasNewNotes = hasNewNotes - WaitUntilNext(time.Hour) - } -} - -func SessionKeepAlive() { - for { - log.Println("Refresh session using GET /api/auth/session ...") - sn.RefreshSession() - WaitUntilNext(time.Hour) - } -} - func main() { - go CheckNotifications() - go SessionKeepAlive() + loadEnv() + + c := sn.NewClient( + sn.WithBaseUrl(os.Getenv("SN_BASE_URL")), + sn.WithApiKey(os.Getenv("SN_API_KEY")), + ) + for { log.Println("fetching items ...") - r, err := sn.Items(&sn.ItemsQuery{Sort: "recent", Limit: 21}) + r, err := c.Items(&sn.ItemsQuery{Sort: "recent", Limit: 21}) if err != nil { log.Println(err) SendToNostr(fmt.Sprint(err)) @@ -114,7 +92,7 @@ func main() { 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 := sn.CreateComment(item.Id, comment) + cId, err := c.CreateComment(item.Id, comment) if err != nil { log.Println(err) SendToNostr(fmt.Sprint(err)) @@ -137,7 +115,7 @@ func main() { comment += fmt.Sprintf("[%s](%s) | ", client.Name, client.Url+noteId) } comment = strings.TrimRight(comment, "| ") - cId, err := sn.CreateComment(item.Id, comment) + cId, err := c.CreateComment(item.Id, comment) if err != nil { log.Println(err) SendToNostr(fmt.Sprint(err)) @@ -153,3 +131,36 @@ func main() { WaitUntilNext(time.Minute) } } + +func loadEnv() { + var ( + f *os.File + s *bufio.Scanner + err error + ) + + if f, err = os.Open(".env"); err != nil { + log.Fatalf("error opening .env: %v", err) + } + defer f.Close() + + s = bufio.NewScanner(f) + s.Split(bufio.ScanLines) + for s.Scan() { + line := s.Text() + parts := strings.SplitN(line, "=", 2) + + // Check if we have exactly 2 parts (key and value) + if len(parts) == 2 { + os.Setenv(parts[0], parts[1]) + } else { + log.Fatalf(".env: invalid line: %s\n", line) + } + } + + // Check for errors during scanning + if err = s.Err(); err != nil { + fmt.Println("error scanning .env:", err) + } + +}