Compare commits
3 Commits
main
...
3-empty-command
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d71fc2605 | |||
| c49a32a741 | |||
| e62ddaa19c |
+2
-1
@@ -1,2 +1,3 @@
|
|||||||
bin/
|
bin/
|
||||||
.idea
|
.idea
|
||||||
|
.vscode/launch.json
|
||||||
|
|||||||
+87
-22
@@ -24,14 +24,15 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/zmb3/spotify/v2"
|
"github.com/zmb3/spotify/v2"
|
||||||
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// telegramBotCmd represents the telegramBot command
|
// telegramBotCmd represents the telegramBot command
|
||||||
@@ -100,34 +101,89 @@ It is not planned to extend this in the future.`,
|
|||||||
err = viper.WriteConfig()
|
err = viper.WriteConfig()
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
}
|
}
|
||||||
results, err := client.Search(ctx, strings.TrimPrefix(update.Message.Text, "/spotigram "), spotify.SearchTypeTrack)
|
|
||||||
cobra.CheckErr(err)
|
|
||||||
|
|
||||||
if results != nil {
|
var results *spotify.SearchResult
|
||||||
track := results.Tracks.Tracks[0]
|
|
||||||
playlist, err := client.GetPlaylist(ctx, spotify.ID(viper.GetString("spotify_playlist_id")))
|
if strings.HasPrefix(update.Message.Text, "/spotigram ") {
|
||||||
|
results, err = client.Search(ctx, strings.TrimPrefix(update.Message.Text, "/spotigram "), spotify.SearchTypeTrack)
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
addTrackResponse, err := client.AddTracksToPlaylist(ctx,
|
if !strings.HasPrefix(update.Message.Text, "/spotigram ") {
|
||||||
playlist.ID,
|
msg.Text = "⛔ Ooops, du musst einen Titel angeben!\n <code>/spotigram [Titel]</code>"
|
||||||
results.Tracks.Tracks[0].ID)
|
|
||||||
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,
|
|
||||||
track.Artists[0].Name,
|
|
||||||
playlist.Name,
|
|
||||||
)
|
|
||||||
msg.ParseMode = "HTML"
|
msg.ParseMode = "HTML"
|
||||||
_, err = bot.Send(msg)
|
_, err = bot.Send(msg)
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
fmt.Printf("New track added to playlist: %s by %s\nsnapshot: %s\n\n",
|
fmt.Printf("Command used incorrectly.")
|
||||||
track.Name,
|
|
||||||
track.Artists[0].Name,
|
|
||||||
addTrackResponse)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if results != nil {
|
||||||
|
|
||||||
|
playlist, err := client.GetPlaylist(ctx, spotify.ID(viper.GetString("spotify_playlist_id")))
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
|
tracks, err := client.GetPlaylistItems(ctx, playlist.ID)
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
|
trackIDs := make([]spotify.ID, 0, len(tracks.Items))
|
||||||
|
|
||||||
|
for page := 1; ; page++ {
|
||||||
|
|
||||||
|
for _, track := range tracks.Items {
|
||||||
|
trackIDs = append(trackIDs, track.Track.Track.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = client.NextPage(ctx, tracks)
|
||||||
|
if err == spotify.ErrNoMorePages {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if contains(trackIDs, results.Tracks.Tracks[0].ID) {
|
||||||
|
track := results.Tracks.Tracks[0]
|
||||||
|
|
||||||
|
msg.Text = fmt.Sprintf("⛔ Ooops, dieser Titel ist schon in der Playlist!\n Ich habe den Titel <pre>%s</pre> von <pre>%s</pre> nicht ERNEUT zur Playlist <pre>%s</pre> hinzugefügt.",
|
||||||
|
track.Name,
|
||||||
|
track.Artists[0].Name,
|
||||||
|
playlist.Name,
|
||||||
|
)
|
||||||
|
msg.ParseMode = "HTML"
|
||||||
|
_, err = bot.Send(msg)
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
|
fmt.Printf("Declined adding duplicate track: %s by %s",
|
||||||
|
track.Name,
|
||||||
|
track.Artists[0].Name)
|
||||||
|
|
||||||
|
}
|
||||||
|
if !contains(trackIDs, results.Tracks.Tracks[0].ID) {
|
||||||
|
track := results.Tracks.Tracks[0]
|
||||||
|
|
||||||
|
addTrackResponse, err := client.AddTracksToPlaylist(ctx,
|
||||||
|
playlist.ID,
|
||||||
|
results.Tracks.Tracks[0].ID)
|
||||||
|
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,
|
||||||
|
track.Artists[0].Name,
|
||||||
|
playlist.Name,
|
||||||
|
)
|
||||||
|
msg.ParseMode = "HTML"
|
||||||
|
_, err = bot.Send(msg)
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
|
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 = "🤣"
|
||||||
_, err = bot.Send(msg)
|
_, err = bot.Send(msg)
|
||||||
@@ -156,3 +212,12 @@ func init() {
|
|||||||
// is called directly, e.g.:
|
// is called directly, e.g.:
|
||||||
// telegramBotCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
// telegramBotCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func contains(s []spotify.ID, e spotify.ID) bool {
|
||||||
|
for _, a := range s {
|
||||||
|
if a == e {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user