package main import ( "context" "fmt" "github.com/zmb3/spotify/v2" spotifyauth "github.com/zmb3/spotify/v2/auth" "golang.org/x/oauth2" "log" "net/http" "os" "strings" "time" ) func main() { ctx := context.Background() token := &oauth2.Token{ AccessToken: os.Getenv("SPOTIFY_ACCESS_TOKEN"), RefreshToken: os.Getenv("SPOTIFY_REFRESH_TOKEN"), TokenType: "Bearer", Expiry: time.Now(), } endpoint := os.Getenv("MISSKEY_ENDPOINT") misskeyToken := "Bearer " + os.Getenv("MISSKEY_TOKEN") httpClient := spotifyauth.New( spotifyauth.WithClientID(os.Getenv("SPOTIFY_CLIENT_ID")), spotifyauth.WithClientSecret(os.Getenv("SPOTIFY_CLIENT_SECRET")), ).Client(ctx, token) client := spotify.New(httpClient) lastId := "" for { playing, err := client.PlayerCurrentlyPlaying(ctx) if err != nil { log.Println(err) time.Sleep(10 * time.Minute) continue } if !playing.Playing { time.Sleep(5 * time.Minute) continue } if playing.Item.ID.String() == lastId { time.Sleep(30 * time.Second) continue } lastId = playing.Item.ID.String() builder := strings.Builder{} for _, artist := range playing.Item.Artists { builder.WriteString("?[") builder.WriteString(artist.Name) builder.WriteString("](),") } before, _ := strings.CutSuffix(builder.String(), ",") text := fmt.Sprintf("[%s]()\\n%s", playing.Item.Name, playing.Item.ID, before) body := fmt.Sprintf("{ \"visibility\" : \"home\", \"text\": \"%s\\n\\n#NowPlaying #usbharu_NowPlaying \"}", text) request, err := http.NewRequest("POST", endpoint, strings.NewReader(body)) if err != nil { log.Println(err) continue } request.Header.Add("Authorization", misskeyToken) request.Header.Add("Content-Type", "application/json") misskeyHttpClient := new(http.Client) post, err := misskeyHttpClient.Do(request) if err != nil { log.Println(err) continue } if post.StatusCode != 200 { log.Printf("Status is not 200. status: %d \n", post.StatusCode) continue } time.Sleep(1 * time.Minute) } }