/
main.go
182 lines (166 loc) 路 4.55 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"syscall"
"time"
"github.com/tnychn/mpv-discord/discordrpc"
"github.com/tnychn/mpv-discord/mpvrpc"
)
var (
client *mpvrpc.Client
presence *discordrpc.Presence
)
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(log.Lmsgprefix)
client = mpvrpc.NewClient()
presence = discordrpc.NewPresence(os.Args[2])
}
func getActivity() (activity discordrpc.Activity, err error) {
getProperty := func(key string) (prop interface{}) {
prop, err = client.GetProperty(key)
return
}
getPropertyString := func(key string) (prop string) {
prop, err = client.GetPropertyString(key)
return
}
// Large Image
activity.LargeImageKey = "mpv"
activity.LargeImageText = "mpv"
if version := getPropertyString("mpv-version"); version != "" {
activity.LargeImageText += " " + version[4:]
}
// Details
activity.Details = getPropertyString("media-title")
metaTitle := getProperty("metadata/by-key/Title")
metaArtist := getProperty("metadata/by-key/Artist")
metaAlbum := getProperty("metadata/by-key/Album")
if metaTitle != nil {
activity.Details = metaTitle.(string)
}
// State
if metaArtist != nil {
activity.State += " by " + metaArtist.(string)
}
if metaAlbum != nil {
activity.State += " on " + metaAlbum.(string)
}
if activity.State == "" {
if aid, ok := getProperty("aid").(string); !ok || aid != "no" {
activity.State += "A"
}
if vid, ok := getProperty("vid").(string); !ok || vid != "no" {
activity.State += "V"
}
_timePos := getProperty("time-pos")
_duration := getProperty("duration")
if _timePos != nil && _duration != nil {
timePos := time.Unix(int64(_timePos.(float64)), 0).UTC().Format("15:04:05")
duration := time.Unix(int64(_duration.(float64)), 0).UTC().Format("15:04:05")
activity.State += fmt.Sprintf(": %s/%s", timePos, duration)
}
}
// Small Image
buffering := getProperty("paused-for-cache")
pausing := getProperty("pause")
loopingFile := getPropertyString("loop-file")
loopingPlaylist := getPropertyString("loop-playlist")
if buffering != nil && buffering.(bool) {
activity.SmallImageKey = "buffer"
activity.SmallImageText = "Buffering"
} else if pausing != nil && pausing.(bool) {
activity.SmallImageKey = "pause"
activity.SmallImageText = "Paused"
} else if loopingFile != "no" || loopingPlaylist != "no" {
activity.SmallImageKey = "loop"
activity.SmallImageText = "Looping"
} else {
activity.SmallImageKey = "play"
activity.SmallImageText = "Playing"
}
if percentage := getProperty("percent-pos"); percentage != nil {
activity.SmallImageText += fmt.Sprintf(" (%d%%)", int(percentage.(float64)))
}
if pcount := getProperty("playlist-count"); pcount != nil && int(pcount.(float64)) > 1 {
if ppos := getProperty("playlist-pos-1"); ppos != nil {
activity.SmallImageText += fmt.Sprintf(" [%d/%d]", int(ppos.(float64)), int(pcount.(float64)))
}
}
// Timestamps
if pausing != nil && !pausing.(bool) {
if timeRemaining := getPropertyString("time-remaining"); timeRemaining != "" {
d, _ := time.ParseDuration(timeRemaining + "s")
endTime := time.Now().Add(d)
activity.Timestamps = &discordrpc.ActivityTimestamps{End: &endTime}
}
}
return
}
func openClient() {
if err := client.Open(os.Args[1]); err != nil {
log.Fatalln(err)
}
log.Println("(mpv-ipc): connected")
}
func openPresence() {
// try until success
for range time.Tick(500 * time.Millisecond) {
if client.IsClosed() {
return // stop trying when mpv shuts down
}
if err := presence.Open(); err == nil {
break // break when successfully opened
}
}
log.Println("(discord-ipc): connected")
}
func main() {
defer func() {
if !client.IsClosed() {
if err := client.Close(); err != nil {
log.Fatalln(err)
}
log.Println("(mpv-ipc): disconnected")
}
if !presence.IsClosed() {
if err := presence.Close(); err != nil {
log.Fatalln(err)
}
log.Println("(discord-ipc): disconnected")
}
}()
openClient()
go openPresence()
for range time.Tick(time.Second) {
activity, err := getActivity()
if err != nil {
if errors.Is(err, syscall.EPIPE) {
break
} else if !errors.Is(err, io.EOF) {
log.Println(err)
continue
}
}
if !presence.IsClosed() {
go func() {
if err = presence.Update(activity); err != nil {
if errors.Is(err, syscall.EPIPE) {
// close it before retrying
if err = presence.Close(); err != nil {
log.Fatalln(err)
}
log.Println("(discord-ipc): reconnecting...")
go openPresence()
} else if !errors.Is(err, io.EOF) {
log.Println(err)
}
}
}()
}
}
}