### Retrieve Yandex Music Track Metadata with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Fetches detailed track information (title, artists) from the Yandex Music API using an HTTP GET request. It requires a track ID and an OAuth token for authentication and includes custom headers for the Yandex Music client and user agent. ```go package main import ( "bytes" "encoding/json" "fmt" "net/http" ) type TrackResponse struct { Result []struct { ID string `json:"id"` Title string `json:"title"` Artists []struct { Name string `json:"name"` } `json:"artists"` } `json:"result"` } func getTrackData(trackID, token string) (title, artists string, err error) { req, err := http.NewRequest("GET", "https://api.music.yandex.net/tracks/"+trackID, nil) if err != nil { return "", "", err } req.Header.Add("Authorization", "OAuth "+token) req.Header.Add("x-Yandex-Music-Client", "YandexMusicAndroid/24024312") req.Header.Add("Accept", "application/json") req.Header.Add("User-Agent", "okhttp/4.12.0") client := &http.Client{} resp, err := client.Do(req) if err != nil { return "", "", err } defer resp.Body.Close() if resp.StatusCode != 200 { return "", "", fmt.Errorf("API returned status %d", resp.StatusCode) } var data TrackResponse if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return "", "", err } if len(data.Result) > 0 { title = data.Result[0].Title for i, artist := range data.Result[0].Artists { if i != 0 { artists += ", " } artists += artist.Name } } return title, artists, nil } ``` -------------------------------- ### Initialize Telegram User Client with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Initializes a Telegram user client using the gotgproto library, requiring API ID, API Hash, phone number, and session storage configuration. It supports SQLite for persistent session management and logs the client's username upon successful startup. ```go package main import ( "context" "github.com/celestix/gotgproto" "github.com/celestix/gotgproto/sessionMaker" "github.com/glebarez/sqlite" "log" ) func main() { ctx := context.Background() client, err := gotgproto.NewClient( 12935793, // API ID "a2926e8cbd01ded5bed25b48cf622927", // API Hash gotgproto.ClientTypePhone("+79991231212"), // Phone number &gotgproto.ClientOpts{ Session: sessionMaker.SqlSession(sqlite.Open("user.session")), }, ) if err != nil { log.Fatal("Failed to start client:", err) } defer client.Stop() log.Printf("Client (@%s) started successfully", client.Self.Username) } ``` -------------------------------- ### Upload Audio to Telegram with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Uploads an audio file, including its ID3 tags, to Telegram using the provided context and client. It then sets this audio as the user's profile music status. Dependencies include 'context', 'github.com/celestix/gotgproto', 'github.com/gotd/td/telegram/uploader', and 'github.com/gotd/td/tg'. ```go package main import ( "bytes" "context" "github.com/celestix/gotgproto" "github.com/gotd/td/telegram/uploader" "github.com/gotd/td/tg" "log" ) func updateMusicStatus(ctx context.Context, client *gotgproto.Client, buffer *bytes.Buffer, size int64) error { uploadClient := uploader.NewUploader(client.API()) upload := uploader.NewUpload("empty.mp3", buffer, size) inputFile, err := uploadClient.Upload(ctx, upload) if err != nil { return err } media := &tg.MessagesUploadMediaRequest{ Peer: client.Self.AsInputPeer(), Media: &tg.InputMediaUploadedDocument{ MimeType: "audio/mpeg", File: inputFile, }, } messageMedia, err := client.Client.API().MessagesUploadMedia(ctx, media) if err != nil { return err } mediaDocument, ok := messageMedia.(*tg.MessageMediaDocument) if !ok { return nil } document, ok := mediaDocument.Document.AsNotEmpty() if !ok { return nil } _, err = client.API().AccountSaveMusic(ctx, &tg.AccountSaveMusicRequest{ ID: document.AsInput(), Unsave: false, }) if err != nil { return err } log.Println("Music status updated successfully") return nil } ``` -------------------------------- ### Create ID3v2 Tags with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Generates ID3v2 tags containing track metadata (title, artist) and writes them to a bytes.Buffer. This is useful for preparing audio files for upload or further processing. Requires the 'github.com/bogem/id3v2' library. ```go package main import ( "bytes" "github.com/bogem/id3v2" "log" ) func createTrackFile(title, artist string) (*bytes.Buffer, int64, error) { tag := id3v2.NewEmptyTag() tag.SetTitle(title) tag.SetArtist(artist) var buffer bytes.Buffer n, err := tag.WriteTo(&buffer) if err != nil { return nil, 0, err } log.Printf("Created ID3 tag: %s - %s (%d bytes)", artist, title, n) return &buffer, n, nil } func main() { buf, size, err := createTrackFile("Bohemian Rhapsody", "Queen") if err != nil { log.Fatal(err) } log.Printf("Buffer size: %d bytes", size) } ``` -------------------------------- ### Go Worker Implementation: Yandex Music to Telegram Sync Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt This Go program implements a worker that monitors Yandex Music playback. It uses a Telegram client to update the 'Now Playing' status with track details. Dependencies include 'context', 'log', 'os', 'os/signal', and 'time'. It takes a context for cancellation and updates music status and optionally profile 'about' links. ```go package main import ( "context" "log" "os" "os/signal" "time" ) const ( phone = "+79991231212" ymToken = "your_oauth_token" addLink = true ) func worker(ctx context.Context) { var lastTrackID string client, err := setupTelegramClient(phone) if err != nil { log.Fatal("Telegram client error:", err) } defer client.Stop() ynison := setupYnisonClient(ymToken) defer ynison.Close() ynison.OnMessage(func(state YnisonState) { if len(state.PlayerState.PlayerQueue.PlayableList) == 0 { return } currentIdx := state.PlayerState.PlayerQueue.CurrentPlayableIndex trackID := state.PlayerState.PlayerQueue.PlayableList[currentIdx].PlayableID if trackID == lastTrackID { return } lastTrackID = trackID title, artists, err := getTrackData(trackID, ymToken) if err != nil { log.Printf("Track data error: %v", err) return } buffer, size, err := createTrackFile(title, artists) if err != nil { log.Printf("Tag creation error: %v", err) return } if err := updateMusicStatus(ctx, client, buffer, size); err != nil { log.Printf("Status update error: %v", err) return } if addLink { updateProfileAbout(ctx, client, trackID) } log.Printf("Now playing: %s - %s", artists, title) }) if err := ynison.Connect(); err != nil { log.Fatal("Ynison connection error:", err) } <-ctx.Done() } func main() { interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) ctx, cancel := context.WithCancel(context.Background()) go func() { <-interrupt cancel() }() for { select { case <-ctx.Done(): return default: worker(ctx) if ctx.Err() != context.Canceled { log.Println("Worker crashed, restarting in 1 minute") time.Sleep(time.Minute) } } } } ``` -------------------------------- ### Go TLS Configuration: Bypass Yandex CAPTCHA Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt This Go code configures a custom HTTP client with specific TLS cipher suites and versions to potentially bypass Yandex API CAPTCHA protections. It imports 'crypto/tls' and 'net/http'. The function 'setupHTTPClient' returns a configured http.Client, which can then be used for making requests to the Yandex API. ```go package main import ( "crypto/tls" "net/http" ) func setupHTTPClient() *http.Client { tlsConfig := &tls.Config{ CipherSuites: []uint16{ tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384, tls.TLS_CHACHA20_POLY1305_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, tls.TLS_RSA_WITH_AES_256_CBC_SHA, tls.TLS_RSA_WITH_AES_128_CBC_SHA, }, MinVersion: tls.VersionTLS11, MaxVersion: tls.VersionTLS13, } transport := &http.Transport{ TLSClientConfig: tlsConfig, } return &http.Client{Transport: transport} } func main() { client := setupHTTPClient() // Use client for Yandex API requests _ = client } ``` -------------------------------- ### Connect to Yandex Music Ynison API with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Establishes a WebSocket connection to the Yandex Music Ynison API to receive real-time playback state updates. It handles connection and disconnection events, logs the currently playing track ID, and requires a Yandex Music OAuth token. ```go package main import ( "github.com/bulatorr/go-yaynison/ynison" "log" ) func main() { token := "your_yandex_music_oauth_token" y := ynison.NewClient(token) defer y.Close() y.OnConnect(func() { log.Println("Connected to Ynison") }) y.OnDisconnect(func() { log.Println("Disconnected from Ynison") }) y.OnMessage(func(state ynison.PutYnisonStateResponse) { if len(state.PlayerState.PlayerQueue.PlayableList) > 0 { currentIndex := state.PlayerState.PlayerQueue.CurrentPlayableIndex trackID := state.PlayerState.PlayerQueue.PlayableList[currentIndex].PlayableID log.Printf("Now playing: %s", trackID) } }) if err := y.Connect(); err != nil { log.Fatal("Failed to connect:", err) } select {} // Keep running } ``` -------------------------------- ### Update Telegram Profile About Field with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Updates the 'About' field in a Telegram profile with a link to a specific track. It retrieves the user's current profile information and then applies the update. This function requires 'context', 'github.com/celestix/gotgproto', and 'github.com/gotd/td/tg'. ```go package main import ( "context" "github.com/celestix/gotgproto" "github.com/gotd/td/tg" "log" ) func updateProfileAbout(ctx context.Context, client *gotgproto.Client, trackID string) error { userdata, err := client.Client.Self(ctx) if err != nil { return err } about := "https://music.yandex.ru/track/" + trackID _, err = client.Client.API().AccountUpdateProfile(ctx, &tg.AccountUpdateProfileRequest{ FirstName: userdata.FirstName, LastName: userdata.LastName, About: about, }) if err != nil { return err } log.Printf("Profile updated with track link: %s", about) return nil } func main() { // Example: Update profile with track link // updateProfileAbout(ctx, client, "12345678") } ``` -------------------------------- ### Clear Telegram Saved Music History with Go Source: https://context7.com/bulatorr/go-ynisonbiostate/llms.txt Removes all saved music tracks from a Telegram profile, effectively clearing the music history. It fetches saved music using 'UsersGetSavedMusic' and then iterates through them, unsaving each track. Dependencies include 'context', 'github.com/celestix/gotgproto', and 'github.com/gotd/td/tg'. ```go package main import ( "context" "github.com/celestix/gotgproto" "github.com/gotd/td/tg" "log" ) func clearMusicHistory(ctx context.Context, client *gotgproto.Client) error { sm, err := client.API().UsersGetSavedMusic(ctx, &tg.UsersGetSavedMusicRequest{ ID: client.Self.AsInput(), Offset: 0, Limit: 999, }) if err != nil { return err } savedMusic, ok := sm.AsModified() if !ok { log.Println("No saved music to clear") return nil } count := 0 for _, doc := range savedMusic.Documents { document, ok := doc.AsNotEmpty() if !ok { continue } _, err := client.API().AccountSaveMusic(ctx, &tg.AccountSaveMusicRequest{ ID: document.AsInput(), Unsave: true, }) if err != nil { log.Printf("Failed to remove track: %v", err) continue } count++ } log.Printf("Cleared %d tracks from music history", count) return nil } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.