forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_ytm.go
107 lines (95 loc) · 2.61 KB
/
segment_ytm.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
package main
import (
"encoding/json"
"fmt"
)
type ytm struct {
props *properties
env environmentInfo
status playStatus
artist string
track string
}
const (
// APIURL is the YTMDA Remote Control API URL property.
APIURL Property = "api_url"
)
func (y *ytm) string() string {
icon := ""
separator := y.props.getString(TrackSeparator, " - ")
switch y.status {
case paused:
icon = y.props.getString(PausedIcon, "\uF8E3 ")
case playing:
icon = y.props.getString(PlayingIcon, "\uE602 ")
case stopped:
return y.props.getString(StoppedIcon, "\uF04D ")
}
return fmt.Sprintf("%s%s%s%s", icon, y.artist, separator, y.track)
}
func (y *ytm) enabled() bool {
err := y.setStatus()
// If we don't get a response back (error), the user isn't running
// YTMDA, or they don't have the RC API enabled.
return err == nil
}
func (y *ytm) init(props *properties, env environmentInfo) {
y.props = props
y.env = env
}
type playStatus int
const (
playing playStatus = iota
paused
stopped
)
type ytmdaStatusResponse struct {
player `json:"player"`
track `json:"track"`
}
type player struct {
HasSong bool `json:"hasSong"`
IsPaused bool `json:"isPaused"`
VolumePercent int `json:"volumePercent"`
SeekbarCurrentPosition int `json:"seekbarCurrentPosition"`
SeekbarCurrentPositionHuman string `json:"seekbarCurrentPositionHuman"`
StatePercent float64 `json:"statePercent"`
LikeStatus string `json:"likeStatus"`
RepeatType string `json:"repeatType"`
}
type track struct {
Author string `json:"author"`
Title string `json:"title"`
Album string `json:"album"`
Cover string `json:"cover"`
Duration int `json:"duration"`
DurationHuman string `json:"durationHuman"`
URL string `json:"url"`
ID string `json:"id"`
IsVideo bool `json:"isVideo"`
IsAdvertisement bool `json:"isAdvertisement"`
InLibrary bool `json:"inLibrary"`
}
func (y *ytm) setStatus() error {
// https://github.com/ytmdesktop/ytmdesktop/wiki/Remote-Control-API
url := y.props.getString(APIURL, "http://127.0.0.1:9863")
httpTimeout := y.props.getInt(APIURL, DefaultHTTPTimeout)
body, err := y.env.doGet(url+"/query", httpTimeout)
if err != nil {
return err
}
q := new(ytmdaStatusResponse)
err = json.Unmarshal(body, &q)
if err != nil {
return err
}
y.status = playing
if !q.player.HasSong {
y.status = stopped
} else if q.player.IsPaused {
y.status = paused
}
y.artist = q.track.Author
y.track = q.track.Title
return nil
}