cache the refreshed tokens
This commit is contained in:
@@ -50,7 +50,6 @@ var initConfigCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(initConfigCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
|
||||
+22
-8
@@ -41,16 +41,30 @@ var spotifyPlaylistsCmd = &cobra.Command{
|
||||
Take note of the desired playlist ID and add it to your config file.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
token = new(oauth2.Token)
|
||||
auth = spotifyauth.New()
|
||||
ctx = context.Background()
|
||||
cachedToken = new(oauth2.Token)
|
||||
auth = spotifyauth.New(
|
||||
spotifyauth.WithClientID(viper.GetString("spotify_client_id")),
|
||||
spotifyauth.WithClientSecret(viper.GetString("spotify_client_secret")),
|
||||
spotifyauth.WithScopes(spotifyauth.ScopePlaylistModifyPublic),
|
||||
)
|
||||
ctx = context.Background()
|
||||
)
|
||||
// Spotify client initialization
|
||||
token.AccessToken = viper.GetString("spotify_access_token")
|
||||
token.RefreshToken = viper.GetString("spotify_refresh_token")
|
||||
token.TokenType = viper.GetString("spotify_token_type")
|
||||
token.Expiry = viper.GetTime("spotify_token_expiry")
|
||||
client := spotify.New(auth.Client(ctx, token))
|
||||
cachedToken.AccessToken = viper.GetString("spotify_access_token")
|
||||
cachedToken.RefreshToken = viper.GetString("spotify_refresh_token")
|
||||
cachedToken.TokenType = viper.GetString("spotify_token_type")
|
||||
cachedToken.Expiry = viper.GetTime("spotify_token_expiry")
|
||||
client := spotify.New(auth.Client(ctx, cachedToken))
|
||||
newToken, err := client.Token()
|
||||
cobra.CheckErr(err)
|
||||
if newToken != cachedToken {
|
||||
viper.Set("spotify_access_token", newToken.AccessToken)
|
||||
viper.Set("spotify_refresh_token", newToken.RefreshToken)
|
||||
viper.Set("spotify_token_type", newToken.TokenType)
|
||||
viper.Set("spotify_token_expiry", newToken.Expiry)
|
||||
err = viper.WriteConfig()
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
|
||||
results, err := client.CurrentUsersPlaylists(ctx, spotify.Limit(50))
|
||||
if err != nil {
|
||||
|
||||
+32
-39
@@ -45,18 +45,19 @@ It is not planned to extend this in the future.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
cachedToken = new(oauth2.Token)
|
||||
auth = spotifyauth.New()
|
||||
ctx = context.Background()
|
||||
auth = spotifyauth.New(
|
||||
spotifyauth.WithClientID(viper.GetString("spotify_client_id")),
|
||||
spotifyauth.WithClientSecret(viper.GetString("spotify_client_secret")),
|
||||
spotifyauth.WithScopes(spotifyauth.ScopePlaylistModifyPublic),
|
||||
)
|
||||
ctx = context.Background()
|
||||
)
|
||||
viper.SetDefault("telegram_debug", false)
|
||||
viper.SetDefault("telegram_update_timeout", 60)
|
||||
|
||||
// Telegram Bot
|
||||
bot, err := tgbotapi.NewBotAPI(viper.GetString("telegram_api_token"))
|
||||
if err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
|
||||
cobra.CheckErr(err)
|
||||
bot.Debug = viper.GetBool("telegram_debug")
|
||||
|
||||
log.Printf("Authorized account: %s", bot.Self.UserName)
|
||||
@@ -66,8 +67,15 @@ It is not planned to extend this in the future.`,
|
||||
|
||||
updates := bot.GetUpdatesChan(u)
|
||||
|
||||
// Spotify client initialization
|
||||
cachedToken.AccessToken = viper.GetString("spotify_access_token")
|
||||
cachedToken.RefreshToken = viper.GetString("spotify_refresh_token")
|
||||
cachedToken.TokenType = viper.GetString("spotify_token_type")
|
||||
cachedToken.Expiry = viper.GetTime("spotify_token_expiry")
|
||||
client := spotify.New(auth.Client(ctx, cachedToken))
|
||||
|
||||
for update := range updates {
|
||||
if update.Message.Chat.IsGroup() && update.Message.Chat.ID == viper.GetInt64("telegram_group_id") {
|
||||
if update.Message.Chat.ID == viper.GetInt64("telegram_group_id") {
|
||||
|
||||
if update.Message == nil {
|
||||
continue
|
||||
@@ -82,43 +90,28 @@ It is not planned to extend this in the future.`,
|
||||
|
||||
switch update.Message.Command() {
|
||||
case "spotigram":
|
||||
// Spotify client initialization
|
||||
cachedToken.AccessToken = viper.GetString("spotify_access_token")
|
||||
cachedToken.RefreshToken = viper.GetString("spotify_refresh_token")
|
||||
cachedToken.TokenType = viper.GetString("spotify_token_type")
|
||||
cachedToken.Expiry = viper.GetTime("spotify_token_expiry")
|
||||
client := spotify.New(auth.Client(ctx, cachedToken))
|
||||
newToken, err := client.Token()
|
||||
if err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
cobra.CheckErr(err)
|
||||
if newToken != cachedToken {
|
||||
viper.Set("spotify_access_token", newToken.AccessToken)
|
||||
viper.Set("spotify_refresh_token", newToken.RefreshToken)
|
||||
viper.Set("spotify_token_type", newToken.TokenType)
|
||||
viper.Set("spotify_token_expiry", newToken.Expiry)
|
||||
if err = viper.WriteConfig(); err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
results, err := client.Search(ctx, strings.TrimPrefix(update.Message.Text, "/spotigram "), spotify.SearchTypeTrack)
|
||||
if err != nil {
|
||||
err = viper.WriteConfig()
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
results, err := client.Search(ctx, strings.TrimPrefix(update.Message.Text, "/spotigram "), spotify.SearchTypeTrack)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
if results != nil {
|
||||
track := results.Tracks.Tracks[0]
|
||||
playlist, err := client.GetPlaylist(ctx, spotify.ID(viper.GetString("spotify_playlist_id")))
|
||||
if err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
cobra.CheckErr(err)
|
||||
|
||||
addTrackResponse, err := client.AddTracksToPlaylist(ctx,
|
||||
playlist.ID,
|
||||
results.Tracks.Tracks[0].ID)
|
||||
if err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
cobra.CheckErr(err)
|
||||
|
||||
msg.Text = fmt.Sprintf("Ich habe den Titel <pre>%s</pre> von <pre>%s</pre> zur Playlist <pre>%s</pre> hinzugefügt.",
|
||||
track.Name,
|
||||
@@ -126,23 +119,23 @@ It is not planned to extend this in the future.`,
|
||||
playlist.Name,
|
||||
)
|
||||
msg.ParseMode = "HTML"
|
||||
if _, err := bot.Send(msg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = bot.Send(msg)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Printf("New track added to playlist, snapshot: %s\n", addTrackResponse)
|
||||
fmt.Printf("New track added to playlist: %s by %s\nsnapshot: %s\n\n",
|
||||
track.Name,
|
||||
track.Artists[0].Name,
|
||||
addTrackResponse)
|
||||
|
||||
}
|
||||
case "rofl":
|
||||
msg.Text = "🤣"
|
||||
if _, err := bot.Send(msg); err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
_, err = bot.Send(msg)
|
||||
cobra.CheckErr(err)
|
||||
default:
|
||||
msg.Text = "🖕"
|
||||
if _, err := bot.Send(msg); err != nil {
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
_, err = bot.Send(msg)
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user