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