-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #36
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package genre | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jakopako/event-api/config" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type GenreCache struct { | ||
lookupSpotifyGenre bool | ||
spotifyToken string | ||
spotifyTokenExpiry time.Time | ||
mu sync.RWMutex | ||
eventsColl *mongo.Collection | ||
spotifyErrorCount int // do we need this? if the spotify responds with an error we probably don't want to hammer it with 1000s of requests, so might need to stop querying the api after a certain error count | ||
} | ||
|
||
type spotifyTokenResponse struct { | ||
AccessToken string `json:access_token"` | ||
TokenType string `json:token_type"` | ||
ExpiresIn int `json:expires_in"` // minutes | ||
} | ||
|
||
var GC *GenreCache | ||
|
||
func (gc *GenreCache) renewSpotifyToken() error { | ||
client := http.Client{} | ||
if gc.spotifyToken == "" || gc.spotifyTokenExpiry.After(time.Now().UTC()) { | ||
tokenUrl := "https://accounts.spotify.com/api/token" | ||
clientId := os.Getenv("SPOTIFY_CLIENT_ID") | ||
clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET") | ||
if clientId == "" || clientSecret == "" { | ||
return fmt.Errorf("env vars SPOTIFY_CLIENT_ID and/or SPOTIFY_CLIENT_SECRET are empty") | ||
} | ||
|
||
form := url.Values{} | ||
form.Add("grant_type", "client_credentials") | ||
form.Add("client_id", clientId) | ||
form.Add("client_secret", clientSecret) | ||
|
||
req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(form.Encode())) | ||
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var tokenResp spotifyTokenResponse | ||
if err := json.Unmarshal(body, &tokenResp); err != nil { | ||
return err | ||
} | ||
|
||
gc.spotifyToken = tokenResp.AccessToken | ||
gc.spotifyTokenExpiry = time.Now().UTC().Add(time.Minute * time.Duration(tokenResp.ExpiresIn-1)) | ||
} | ||
return nil | ||
} | ||
|
||
func (gc *GenreCache) getSpoticyGenres(artist string) ([]string, error) { | ||
genres := []string{} | ||
|
||
if gc.lookupSpotifyGenre { | ||
gc.renewSpotifyToken() | ||
|
||
// find genres | ||
} | ||
|
||
return genres, nil | ||
} | ||
|
||
func InitGenreCache() { | ||
// this code assumes that the DB has already been initialized | ||
|
||
GC = &GenreCache{ | ||
lookupSpotifyGenre: os.Getenv("LOOKUP_SPOTIFY_GENRE") == "true", | ||
eventsColl: config.MI.DB.Collection("events"), | ||
} | ||
} | ||
|
||
func LookupGenres(artist string) ([]string, error) { | ||
return GC.getSpoticyGenres(artist) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters