package main import ( "context" "fmt" "github.com/zmb3/spotify/v2" spotifyauth "github.com/zmb3/spotify/v2/auth" "golang.org/x/oauth2" "gopkg.in/ini.v1" "log" "net/http" "os" "strings" "time" ) func main() { ctx := context.Background() load, err := ini.Load("config.ini") if err != nil { log.Fatal(err) } token := &oauth2.Token{ AccessToken: load.Section("Spotify").Key("AccessToken").String(), RefreshToken: load.Section("Spotify").Key("RefreshToken").String(), TokenType: "Bearer", Expiry: time.Now(), } endpoint := load.Section("Misskey").Key("Endpoint").String() misskeyToken := "Bearer " + load.Section("Misskey").Key("Token").String() os.Setenv("SPOTIFY_ID", load.Section("Spotify").Key("ClientId").String()) os.Setenv("SPOTIFY_SECRET", load.Section("Spotify").Key("ClientSecret").String()) httpClient := spotifyauth.New( spotifyauth.WithClientID(load.Section("Spotify").Key("ClientId").String()), spotifyauth.WithClientSecret(load.Section("Spotify").Key("ClientSecret").String()), ).Client(ctx, token) client := spotify.New(httpClient) lastId := "" for { playing, err := client.PlayerCurrentlyPlaying(ctx) if err != nil { log.Fatal(err) } 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\"}", 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) } }