Initial commit
This commit is contained in:
commit
02a6dabcfb
62 files changed
+15751
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
|
||||
"gitea.mrixs.me/Mrixs/yandex-music-api-go/yandexmusic"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Get token from environment
|
||||
token := os.Getenv("YANDEX_MUSIC_TOKEN")
|
||||
if token == "" {
|
||||
log.Println("YANDEX_MUSIC_TOKEN not set, running without auth")
|
||||
}
|
||||
|
||||
// Create client
|
||||
client := yandexmusic.NewClient(yandexmusic.ClientConfig{
|
||||
Token: token,
|
||||
Language: "en",
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize client (fetches account info)
|
||||
if err := client.Init(ctx); err != nil {
|
||||
log.Printf("Failed to init client: %v", err)
|
||||
} else if client.GetMe() != nil && client.GetMe().Account != nil {
|
||||
fmt.Printf("Logged in as: %s (UID: %d)\n", client.GetMe().Account.DisplayName, client.GetMe().Account.UID)
|
||||
}
|
||||
|
||||
var tracks []*models.Track
|
||||
|
||||
// Example: Search for tracks
|
||||
fmt.Println("\n--- Searching for tracks ---")
|
||||
searchResult, err := client.SearchTracks(ctx, "Daft Punk", 1, 5)
|
||||
if err != nil {
|
||||
log.Printf("Search failed: %v", err)
|
||||
} else if searchResult != nil {
|
||||
for i := range searchResult.Results {
|
||||
artistName := ""
|
||||
if len(searchResult.Results[i].Artists) > 0 {
|
||||
artistName = searchResult.Results[i].Artists[0].Name
|
||||
}
|
||||
fmt.Printf("%d. %s - %s (%d ms)\n", i+1, searchResult.Results[i].Title, artistName, searchResult.Results[i].DurationMs)
|
||||
}
|
||||
// Convert []Track to []*Track
|
||||
tracks = make([]*models.Track, len(searchResult.Results))
|
||||
for i := range searchResult.Results {
|
||||
tracks[i] = &searchResult.Results[i]
|
||||
}
|
||||
}
|
||||
|
||||
// Example: Get track by ID (if we have results)
|
||||
if len(tracks) > 0 {
|
||||
fmt.Println("\n--- Getting track details ---")
|
||||
track, err := client.GetTrack(ctx, tracks[0].ID)
|
||||
if err != nil {
|
||||
log.Printf("Get track failed: %v", err)
|
||||
} else {
|
||||
fmt.Printf("Track: %s\n", track.Title)
|
||||
fmt.Printf("Albums: %d\n", len(track.Albums))
|
||||
fmt.Printf("Download info: %d options\n", len(track.DownloadInfo))
|
||||
}
|
||||
}
|
||||
|
||||
// Example: Get liked tracks (requires auth)
|
||||
if token != "" {
|
||||
fmt.Println("\n--- Getting liked tracks ---")
|
||||
liked, err := client.GetLikedTracks(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Get liked tracks failed: %v", err)
|
||||
} else {
|
||||
fmt.Printf("Found %d liked tracks\n", len(liked))
|
||||
for i, track := range liked {
|
||||
if i >= 3 {
|
||||
break
|
||||
}
|
||||
artistName := ""
|
||||
if len(track.Artists) > 0 {
|
||||
artistName = track.Artists[0].Name
|
||||
}
|
||||
fmt.Printf(" %s - %s\n", track.Title, artistName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\nDone!")
|
||||
}
|
||||
Reference in new issue
Block a user