-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncmpcpp2scrbblr.sh
executable file
·128 lines (111 loc) · 3.99 KB
/
ncmpcpp2scrbblr.sh
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
#!/bin/bash
# ncmpcpp2scrobblr.sh: last.fm/audioscrobbler song submission script for ncmpcpp
# by CrimsonGlory
# based on mp3blstr2dscrbblr.sh 0.4 by Alexander Heinlein <[email protected]>
#
# License: GPL v3
#
# version: 0.1
#
# perl audioscrobbler plugin directory
# (http://search.cpan.org/~roam/Audio-Scrobbler-0.01/lib/Audio/Scrobbler.pm)
# plugin binary/script
#BIN="../bin/scrobbler-helper"
BIN="./Audio-Scrobbler-0.01/bin/scrobbler-helper"
# maximum retries if first submission failed
MAX_RETRIES=15
# seconds to wait before second submission
# this value increases during further retries
WAIT=10
# check for scrobbler binary
if ! which $BIN >/dev/null
then
echo "$BIN doesn't exist, make sure libaudio-scrobbler-perl is installed"
exit
fi
# we need 2 + 7 arguments:
# -P <client> -V <clientversion>
# title, artist, album, year, comment, genre, length
# note: - comment will be left empty
# - genre will be specified globally
# unfortunately mp3blaster isn't recognized as client >:(
CLIENT="mpd"
CLIVER="0.11"
while [ 1 ]; do
# now get all information from ncmpcpp
genre=$(/usr/bin/ncmpcpp --current-song="{%g}")
title=$(/usr/bin/ncmpcpp --current-song="{%t}|{%f}")
artist=$(/usr/bin/ncmpcpp --current-song="{%a}|{<unknown>}")
album=$(/usr/bin/ncmpcpp --current-song="{%b}|{<unknown>}")
year=$(/usr/bin/ncmpcpp --current-song="{%y}")
length=$(/usr/bin/ncmpcpp --current-song="%l" | grep -E -o -e "^[0-9]+:[0-9]+$" | sed "s/:/*60+/g" | bc | tr -d \"\n\" )
echo "genre=$genre"
echo "title=$title"
echo "artist=$artist"
echo "album=$album"
echo "year=$year"
echo "length=$length"
# check if we have artist and title
# else print error message to stderr
if [ "$title" = "" ] || [ "$artist" = "" ]
then
echo "error: $(grep "^path " $MP3_STAT | cut -d ' ' -f2-) has no/invalid ID tag" 1>&2
elif [ "$titleprev" == "$title" ] && [ "$artistprev" == "$artist" ] && [ "$albumprev" == "$album" ]
then
echo "same song";
else
echo "titleprev=$titleprev"
echo "title=$title"
echo "artistprev=$artistprev"
echo "artist=$artist"
echo "albumprev=$albumprev"
echo "album=$album"
# and finally execute plugin
if [ -n "$DIR" ]; then cd "$DIR"; fi
# creating function and variable for multiple use
SUBMIT="$BIN -P $CLIENT -V $CLIVER \"$title\" \"$artist\" \"$album\" \"$year\" \"\" \"$genre\" \"$length\""
submit()
{
"$BIN" -P "$CLIENT" -V "$CLIVER" "$title" "$artist" "$album" "$year" "" "$genre" "$length" >/dev/null
}
echo
echo "executing $SUBMIT"
echo
submit
retval="$?"
# known return values:
# 22 bad hostname
# 104 Connection reset by peer
# 110 connection timeout
# 111 Connection refused (fuck you)
# 114 couldn't complete handshake
# 115 connection timeout
# 255 couldn't complete handshake
# 500 read timeout / EOF
# last submission failed, resubmit
if [ "$retval" == "22" ] || [ "$retval" == "104" ] || [ "$retval" == "110" ] || [ "$retval" == "111" ] || [ "$retval" == "114" ] || [ "$retval" == "115" ] || [ "$retval" == "255" ] || [ "$retval" == "500" ]
then
for ((i = 1; i <= MAX_RETRIES; i++))
do
echo "waiting for $WAIT seconds..."
sleep $WAIT
echo "retry #$i: executing $SUBMIT"
submit
if [ "$?" == "0" ]
then
exit 0
fi
# doubling next wait, server may be down for any length of time
let "WAIT = $WAIT * 2"
done
echo "couldn't submit song after $MAX_RETRIES retries: $?" 1>&2
elif [ "$retval" != "0" ]
then
echo "return value: $retval" 1>&2
fi
fi
sleep 30
titleprev=$title
artistprev=$artist
albumprev=$album
done