2024-03-18 14:00:16 +00:00
|
|
|
package main
|
|
|
|
|
2024-03-19 09:28:24 +00:00
|
|
|
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"
|
|
|
|
)
|
2024-03-18 14:00:16 +00:00
|
|
|
|
|
|
|
func main() {
|
2024-03-19 09:28:24 +00:00
|
|
|
|
|
|
|
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 {
|
2024-03-19 10:32:55 +00:00
|
|
|
log.Println(err)
|
|
|
|
time.Sleep(10 * time.Minute)
|
|
|
|
continue
|
2024-03-19 09:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !playing.Playing {
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if playing.Item.ID.String() == lastId {
|
2024-03-18 14:00:16 +00:00
|
|
|
time.Sleep(30 * time.Second)
|
2024-03-19 09:28:24 +00:00
|
|
|
continue
|
2024-03-18 14:00:16 +00:00
|
|
|
}
|
2024-03-19 09:28:24 +00:00
|
|
|
|
|
|
|
lastId = playing.Item.ID.String()
|
|
|
|
|
|
|
|
builder := strings.Builder{}
|
|
|
|
|
|
|
|
for _, artist := range playing.Item.Artists {
|
|
|
|
builder.WriteString("?[<plain>")
|
|
|
|
builder.WriteString(artist.Name)
|
|
|
|
builder.WriteString("</plain>](<https://open.spotify.com/artist/")
|
|
|
|
builder.WriteString(artist.ID.String())
|
|
|
|
builder.WriteString(">),")
|
|
|
|
}
|
|
|
|
|
|
|
|
before, _ := strings.CutSuffix(builder.String(), ",")
|
|
|
|
text := fmt.Sprintf("[<plain>%s</plain>](<https://open.spotify.com/track/%s>)\\n<small>%s</small>", 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)
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:00:16 +00:00
|
|
|
}
|