-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmas.go
74 lines (65 loc) · 1.87 KB
/
christmas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"time"
)
type Song struct {
Artist string `json:"artist"`
Channel int32 `json:"channel"`
Song string `json:"song"`
Time string `json:"time"`
}
type ChristmasRadioResponse struct {
PageLastPlayed struct {
Content struct {
Hero struct {
Text string `json:"text"`
} `json:"Hero"`
RecentlyPlayed struct {
Songs []Song `json:"songs"`
} `json:"recently_played"`
} `json:"Content"`
} `json:"PageLastPlayed"`
}
func addChristmasRadioLists(playlistID, sToken string) {
// loop all interesting lists
radioUrls := []string{
"https://jouluradio-wp.production.geniem.io/viimeksi-soitetut/",
"https://jouluradio-wp.production.geniem.io/viimeksi-soitetut/?kanava=indiejoulu",
"https://jouluradio-wp.production.geniem.io/viimeksi-soitetut/?kanava=jazzjoulu",
"https://jouluradio-wp.production.geniem.io/viimeksi-soitetut/?kanava=klassinen-joulu",
}
for _, radioURL := range radioUrls {
fmt.Printf("Adding songs from %s\n", radioURL)
tracks, err := fetchChristmasRadioSongs(radioURL)
if err != nil {
fmt.Printf("Error when fetching songs %s\n", err.Error())
}
if err := addSongsFromRadioToPlaylist(tracks, playlistID, sToken); err != nil {
fmt.Printf("Error when adding songs %s\n", err.Error())
}
fmt.Printf("Sleeping a while...\n")
time.Sleep(sleepSeconds * time.Second)
}
fmt.Println("All done! Merry Christmas! Hyvää joulua!")
}
func fetchChristmasRadioSongs(url string) ([]*Track, error) {
data, err := doGetRequest(url, "")
if err != nil {
return nil, err
}
response := ChristmasRadioResponse{}
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
tracks := make([]*Track, 0)
for _, track := range response.PageLastPlayed.Content.RecentlyPlayed.Songs {
tracks = append(tracks, &Track{
Artist: track.Artist,
Title: track.Song,
})
}
return tracks, err
}