forked from CamilleScholtz/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpdlisten
executable file
·84 lines (70 loc) · 1.86 KB
/
mpdlisten
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
#!/bin/fish
#
# mpdlisten - Send mpd listens to ListenBrainz.
# onodera, https://github.com/onodera-punpun
## CONFIGURATION
# Your ListenBrainz key
set key (cat $HOME/.listenbrainz)
## FUNCTIONS
# This function echoes listen info
function echolisten
if test $argv = single
echo '
{
"listen_type": "single",
"payload": [
{
"listened_at": '(date +%s)',
"track_metadata": {
"artist_name": "'$oldinfo[1]'",
"track_name": "'$oldinfo[4]'",
"release_name": "'$oldinfo[2]'",
"additional_info": {
"tracknumber": "'$oldinfo[3]'"
}
}
}
]
}
'
else
echo '
{
"listen_type": "now_playing",
"payload": [
{
"listened_at": '(date +%s)',
"track_metadata": {
"artist_name": "'$info[1]'",
"track_name": "'$info[4]'",
"release_name": "'$info[2]'",
"additional_info": {
"tracknumber": "'$info[3]'"
}
}
}
]
}
'
end
end
## EXECUTE
set oldinfo (mpc -f "%artist%\n%album%\n%track%\n%title%")
while true
set info (mpc -f "%artist%\n%album%\n%track%\n%title%")
# Send "just listened to" info
if test "$info[1..4]" != "$oldinfo[1..4]"
set elapsed (echo $oldinfo[5] | grep -o "[0-9]*:[0-9]*" | head -n 1 | tr : \n)
set seconds (math $elapsed[1] \* 60 + $elapsed[2])
set percent (echo $oldinfo[5] | grep -o "[0-9]*%" | head -n 1 | tr -d \%)
if test $seconds -ge 240 -o $percent -ge 55
echolisten single > /tmp/listen
curl -H "Content-Type: application/json" -H "Authorization: Token $key" -X POST -d "@/tmp/listen" https://api.listenbrainz.org/1/submit-listens
end
end
# Send "listening to" info
#echolisten now_playing > /tmp/listen
# curl -H "Content-Type: application/json" -H "Authorization: Token $key" -X POST -d "@/tmp/listen" https://api.listenbrainz.org/1/submit-listens
set oldinfo $info
sleep 5
end