From efb316aacb0bbdd51d8442eb56a31d5d984db26e Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Mon, 3 Jun 2024 15:16:53 -0600 Subject: [PATCH] Remove spotify --- .env.example | 5 +- .vscode/extensions.json | 8 +++ api/spotify.nowplaying.go | 30 --------- docker-compose.yml | 3 - lib/spotify.go | 86 ------------------------- main.go | 19 ------ pages/home.go | 5 -- pages/templates/partials/iconlinks.html | 13 ---- 8 files changed, 9 insertions(+), 160 deletions(-) create mode 100644 .vscode/extensions.json delete mode 100644 api/spotify.nowplaying.go delete mode 100644 lib/spotify.go diff --git a/.env.example b/.env.example index 536f073..6b963a4 100644 --- a/.env.example +++ b/.env.example @@ -10,7 +10,4 @@ BUCKET_NAME= AWS_REGION= AWS_ENDPOINT_URL_S3= AWS_ACCESS_KEY_ID= -AWS_SECRET_ACCESS_KEY= -SPOTIFY_CLIENT_ID= -SPOTIFY_CLIENT_SECRET= -SPOTIFY_REFRESH_TOKEN= \ No newline at end of file +AWS_SECRET_ACCESS_KEY= \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..247037c --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,8 @@ +{ + "recommendations": [ + "golang.go", + "bradlc.vscode-tailwindcss", + "redhat.vscode-yaml", + "a-h.templ" + ] +} \ No newline at end of file diff --git a/api/spotify.nowplaying.go b/api/spotify.nowplaying.go deleted file mode 100644 index 08ae9c8..0000000 --- a/api/spotify.nowplaying.go +++ /dev/null @@ -1,30 +0,0 @@ -package api - -import ( - "net/http" - "os" - - "atri.dad/lib" - "github.com/labstack/echo/v4" -) - -func NowPlayingHandler(c echo.Context) error { - clientID := os.Getenv("SPOTIFY_CLIENT_ID") - clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET") - refreshToken := os.Getenv("SPOTIFY_REFRESH_TOKEN") - - playing, err := lib.GetCurrentlyPlayingTrack(clientID, clientSecret, refreshToken) - if err != nil { - http.Error(c.Response().Writer, err.Error(), http.StatusInternalServerError) - return err - } - - if playing.Item != nil && playing.Playing { - songName := lib.NowPlayingTextFilter(playing.Item.Name) - artistName := lib.NowPlayingTextFilter(playing.Item.Artists[0].Name) - - return c.String(http.StatusOK, `
🔥
`) - } else { - return c.String(http.StatusOK, "") - } -} diff --git a/docker-compose.yml b/docker-compose.yml index 7daf3ba..08da422 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,9 +21,6 @@ services: - CLERK_SECRET_KEY=$CLERK_SECRET_KEY - CLERK_WEBHOOK_SECRET=$CLERK_WEBHOOK_SECRET - RESEND_API_KEY=$RESEND_API_KEY - - SPOTIFY_CLIENT_ID=$SPOTIFY_CLIENT_ID - - SPOTIFY_CLIENT_SECRET=$SPOTIFY_CLIENT_SECRET - - SPOTIFY_REFRESH_TOKEN=$SPOTIFY_REFRESH_TOKEN - STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY networks: diff --git a/lib/spotify.go b/lib/spotify.go deleted file mode 100644 index 40c1859..0000000 --- a/lib/spotify.go +++ /dev/null @@ -1,86 +0,0 @@ -package lib - -import ( - "context" - "os" - "strings" - "sync" - - "atri.dad/lib/pubsub" - "github.com/zmb3/spotify" - "golang.org/x/oauth2" -) - -var ( - spotifyOAuth2Endpoint = oauth2.Endpoint{ - TokenURL: "https://accounts.spotify.com/api/token", - AuthURL: "https://accounts.spotify.com/authorize", - } - config *oauth2.Config - once sync.Once -) - -func NowPlayingTextFilter(s string) string { - s = strings.Replace(s, "'", "'", -1) - s = strings.Replace(s, "\"", """, -1) - return s -} - -func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config { - once.Do(func() { - config = &oauth2.Config{ - ClientID: clientID, - ClientSecret: clientSecret, - Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying}, - Endpoint: spotifyOAuth2Endpoint, - } - }) - return config -} - -func GetCurrentlyPlayingTrack(clientID string, clientSecret string, refreshToken string) (*spotify.CurrentlyPlaying, error) { - // OAuth2 config - config := GetOAuth2Config(clientID, clientSecret) - - // Token source - tokenSource := config.TokenSource(context.Background(), &oauth2.Token{RefreshToken: refreshToken}) - - // Get new token - newToken, err := tokenSource.Token() - if err != nil { - return nil, err - } - - // Create new client - client := spotify.Authenticator{}.NewClient(newToken) - - // Get currently playing track - playing, err := client.PlayerCurrentlyPlaying() - if err != nil { - return nil, err - } - - return playing, nil -} - -func CurrentlyPlayingTrackSSE(ctx context.Context, pubSub pubsub.PubSub) error { - clientID := os.Getenv("SPOTIFY_CLIENT_ID") - clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET") - refreshToken := os.Getenv("SPOTIFY_REFRESH_TOKEN") - - playing, err := GetCurrentlyPlayingTrack(clientID, clientSecret, refreshToken) - if err != nil { - return err - } - - if playing.Item != nil && playing.Playing { - songName := NowPlayingTextFilter(playing.Item.Name) - artistName := NowPlayingTextFilter(playing.Item.Artists[0].Name) - - return SendSSE(ctx, pubSub, "spotify", `
🔥
`) - } else { - SendSSE(ctx, pubSub, "spotify", "") - } - - return nil -} diff --git a/main.go b/main.go index 3b17dc3..7b67047 100755 --- a/main.go +++ b/main.go @@ -88,31 +88,12 @@ func main() { return api.SSEDemoSend(c, pubSub) }) - apiGroup.GET("/spotify/nowplaying", api.NowPlayingHandler) apiGroup.POST("/tools/resize", api.ResizeHandler) // Webhook Routes: webhookGroup := e.Group("/webhook") webhookGroup.POST("/clerk", webhooks.ClerkWebhookHandler) - // Spotify Polling - go func() { - ticker := time.NewTicker(5 * time.Second) - defer ticker.Stop() - - for range ticker.C { - // Check if there are any clients connected to the "spotify" channel - if lib.SSEServer.ClientCount("spotify") > 0 { - // Get the currently playing track - err := lib.CurrentlyPlayingTrackSSE(context.Background(), pubSub) - if err != nil { - // Handle error - continue - } - } - } - }() - // Parse command-line arguments for IP and port ip := flag.String("ip", "", "IP address to bind the server to") port := flag.String("port", "3000", "Port to bind the server to") diff --git a/pages/home.go b/pages/home.go index cd6e2e5..4860ea4 100644 --- a/pages/home.go +++ b/pages/home.go @@ -25,11 +25,6 @@ func Home(c echo.Context) error { Href: "/api/rss", Icon: template.HTML(``), }, - { - Name: "Spotify", - Href: "https://open.spotify.com/user/31v3p4oq6im7fvpqhhbju222pbr4?si=f25bb92301434db2", - Icon: template.HTML(``), - }, { Name: "Fediverse", Href: "https://blahaj.zone/@himbothy", diff --git a/pages/templates/partials/iconlinks.html b/pages/templates/partials/iconlinks.html index 534f9f1..da882e7 100644 --- a/pages/templates/partials/iconlinks.html +++ b/pages/templates/partials/iconlinks.html @@ -1,18 +1,5 @@ {{define "iconlinks"}} -{{if eq .Name "Spotify"}} -
-
- - -
- - - {{.Icon}} - -
-{{else}} {{.Icon}} -{{end}} {{end}} \ No newline at end of file