-
Notifications
You must be signed in to change notification settings - Fork 0
/
yandex-radio.go
298 lines (248 loc) · 7.06 KB
/
yandex-radio.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"encoding/json"
"errors"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/TheCreeper/go-notify"
"github.com/kr/pretty"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/zserge/lorca"
)
const (
songInfoFrequency = time.Second
defaultURL = "https://radio.yandex.ru/"
apiURL = "https://api.music.yandex.net/"
trackApiURL = apiURL + "tracks/"
albumApiURL = apiURL + "albums/"
additionalInfoClass = "__additional_info"
)
var errTrackNotFound = errors.New("track not found")
var (
logLevel = pflag.StringP("log-level", "L", "info", "logging level (trace, debug, info, warn, error, fatal)")
notifyDelay = pflag.Int32P("notify-delay", "d", 5000, "notification dismiss timer delay, milliseconds")
)
type (
artist struct {
ID int `json:"id"`
Name string `json:"name"`
Cover struct {
URI string `json:"uri"`
} `json:"cover"`
}
album struct {
ID int `json:"id"`
Title string `json:"title"`
Year int `json:"year"`
CoverURI string `json:"coverUri"`
Artists []artist `json:"artists"`
}
track struct {
ID string `json:"id"`
Title string `json:"title"`
Artists []artist `json:"artists"`
Albums []album `json:"albums"`
}
)
func (t *track) String() string {
if t == nil {
return ""
}
return pretty.Sprint(t)
}
var albumFromCover = regexp.MustCompile(`^url\("//avatars.yandex.net/get-music-content/\d+/.+\.a\.(\d+)-\d+/600x600"\)$`)
func main() {
pflag.Parse()
if lvl, err := logrus.ParseLevel(*logLevel); err == nil {
logrus.SetLevel(lvl)
} else {
logrus.WithError(err).Warnf("Unable to parse log level")
}
if err := doMain(pflag.Args()); err != nil {
log.Fatal(err)
}
}
func doMain(args []string) error {
hd, err := homedir.Dir()
if err != nil {
logrus.WithError(err).Warn("Unable to acquire home dir. Using current one.")
hd = "."
}
url := defaultURL
if len(args) > 0 {
url = args[0]
}
ui, err := lorca.New(url, hd+"/.yandex-radio", 800, 600)
if err != nil {
logrus.WithError(err).Fatal("Fatal error")
}
defer ui.Close()
// https://api.music.yandex.net/albums/12983383/with-tracks
// https://api.music.yandex.net/albums/12983383/with-tracks
// https://api.music.yandex.net/tracks/13087591
go func() {
var (
playingNow = ""
songInfoFile = hd + "/.current-song"
songImgFile = hd + "/.current-song-img.png"
)
ticker := time.Tick(songInfoFrequency)
for range ticker {
trackIDVal := ui.Eval(`document.querySelector('.player-controls__bar button.like').attributes['data-idx'].value`)
tt := ui.Eval(`document.querySelector('.player-controls__title').title`)
ta := ui.Eval(`document.querySelector('.player-controls__artists').title`)
ti := ui.Eval(`document.querySelector('.slider__item_playing div span').style.backgroundImage`)
albumID := ""
if albumFromCover.MatchString(ti.String()) {
albumID = albumFromCover.FindStringSubmatch(ti.String())[1]
}
trackInfo := fmt.Sprintf("%s • %s", tt, ta)
err := os.WriteFile(songInfoFile, []byte(trackInfo), 0o664)
if err != nil {
logrus.WithError(err).Warn("Unable to save song info file")
}
logrus.WithFields(logrus.Fields{
"trackID": trackIDVal,
"albumID": albumID,
"tt": tt,
"ta": ta,
"trackInfo": trackInfo,
"ti": ti,
}).Debug("Track info was got.")
var (
albumInfo = ""
existsAlbumInfo = ui.Eval(`document.querySelector('.slider__item_playing div span').title`).String()
existsArtist = ui.Eval(`document.querySelector('.player-controls__artists').innerText`).String()
)
if existsAlbumInfo == "" || existsArtist == ta.String() {
if len(albumID) > 0 {
al, err := getAlbumInfo(albumID)
if err != nil {
logrus.WithError(err).Warnf("Unable to acquire album info for album %s", albumID)
} else {
logrus.WithField("tr", al).Info("Album playing now")
albumInfo = fmt.Sprintf("%s (%d)", al.Title, al.Year)
ui.Eval(`document.querySelector('.slider__item_playing div span').title="` + albumInfo + `"`)
ui.Eval(`document.querySelector('.player-controls__artists').innerText='` + ta.String() + `\n\n` + albumInfo + `'`)
}
}
}
if playingNow != "" && playingNow != trackInfo {
// https://developer.gnome.org/notification-spec/
ntf := notify.NewNotification("Yandex Radio", fmt.Sprintf("<b>%s</b>\r%s\r%s",
html.EscapeString(tt.String()),
html.EscapeString(ta.String()),
html.EscapeString(albumInfo)),
)
ntf.Timeout = *notifyDelay
ntf.Hints = make(map[string]interface{})
ntf.Actions = []string{"dismiss", "Dismiss"}
if ti != nil && len(ti.String()) > 7 {
tiUrl := ti.String()[5 : len(ti.String())-2]
logrus.Tracef("tiUrl='%s'", tiUrl)
err = updateImageFile(tiUrl, songImgFile)
if err != nil {
logrus.WithError(err).Warn("Unable to get album img")
} else {
ntf.Hints[notify.HintImagePath] = songImgFile
}
}
_, err = ntf.Show()
if err != nil {
logrus.WithError(err).Warn("Unable to show notification")
}
}
playingNow = trackInfo
}
}()
<-ui.Done()
return nil
}
// download album img
func updateImageFile(url, imgFile string) error {
res, err := http.Get("http:" + url)
if err != nil {
return err
}
defer res.Body.Close()
bb, err := io.ReadAll(res.Body)
if err != nil {
return err
}
err = os.WriteFile(imgFile, bb, 0o644)
if err != nil {
return err
}
return nil
}
func getAlbumInfo(albumID string) (*album, error) {
albumURL := albumApiURL + albumID
res, err := http.Get(albumURL)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
alinf := new(struct {
InvInfo json.RawMessage `json:"invocationInfo"`
Result *album `json:"result"`
})
bb, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
logrus.WithFields(logrus.Fields{
"albumURL": albumURL,
"json": string(bb),
}).Trace("Got album info")
err = json.Unmarshal(bb, alinf)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal response for album %s: %v", albumID, err)
}
return alinf.Result, nil
}
func getTrackInfo(trackID string) (*track, error) {
trackURL := trackApiURL + trackID
res, err := http.Get(trackURL)
if res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
trinf := new(struct {
Result []*track `json:"result"`
})
bb, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
logrus.WithFields(logrus.Fields{
"trackURL": trackURL,
"json": string(bb),
}).Trace("Got track info")
err = json.Unmarshal(bb, trinf)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal response for track %s: %v", trackID, err)
}
if len(trinf.Result) == 0 {
return nil, fmt.Errorf("unable to get info for track %s: %v", trackID, errTrackNotFound)
}
return trinf.Result[0], nil
}
// func commitNotifyAction(ui lorca.UI, a string) {
// switch a {
// case "default":
// default:
// logrus.Warnf("Invalid action %s", a)
// }
// }