41 lines
975 B
Go
41 lines
975 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
func reportPlayback(address, token string) {
|
|
endpoint, err := url.Parse(address)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if endpoint.Scheme == "wss" {
|
|
endpoint.Scheme = "https"
|
|
} else {
|
|
endpoint.Scheme = "http"
|
|
}
|
|
endpoint.Path = "/v1/device/status"
|
|
client := &http.Client{Timeout: 4 * time.Second}
|
|
ticker := time.NewTicker(15 * time.Second)
|
|
defer ticker.Stop()
|
|
for {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
state := musicCallContext(ctx, "spotify_control", json.RawMessage(`{"action":"status"}`))
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(state))
|
|
if err == nil {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if response, err := client.Do(req); err == nil {
|
|
response.Body.Close()
|
|
}
|
|
}
|
|
cancel()
|
|
<-ticker.C
|
|
}
|
|
}
|