forked from flymia/SpotCurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotcurrent
executable file
·79 lines (66 loc) · 1.87 KB
/
spotcurrent
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
#!/bin/bash
#Grepping out the PID of spotify
spotifyid=$(ps -ef | grep '[/]usr/share/spotify' | awk '{print $2}' | head -1)
#echo $spotifyid;
#If no PID is found, exit with the warning.
if [ -z "$spotifyid" ]; then
echo 'Spotify not running! PID not found.';
exit 1;
fi;
#Trapping CTRL+C
onexit()
{
rm -f /tmp/currentsong.txt;
echo 'OK, exiting and deleting Songfile!';
exit 1;
}
trap onexit SIGINT;
echo -e 'SpotCurrent v1.0.1 by flymia\n';
#User enters the desired interval
echo -n 'Enter your preferred check-interval (Press ENTER for default 5 seconds): ';
read interval;
if [[ -n ${interval//[0-9]/} ]]; then
echo "Contains letters! Exiting..."
exit 1;
fi
#If nothing was read, using the default interval;
if [ -z "$interval" ]; then
interval=5;
fi
#If another instance is running, exit.
if [ -e /tmp/currentsong.txt ]
then
echo 'Another instance is running! Exiting!';
exit 1;
else
echo 'To abort press CTRL+C';
echo -e 'Starting song check...\n';
#Create the currentsong file
touch /tmp/currentsong.txt
#Main loop of the script
while true; do
#Get the current song by listing all windows and grepping the matching window with the PID
currentsong=$(wmctrl -l -p | grep $spotifyid | sed -n 's/.*'$HOSTNAME'//p');
if [ "$(echo $currentsong)" == 'Spotify' ]; then
currentsong='Song paused';
fi
echo -n 'Current song: ';
echo $currentsong;
# use -t to display in two lines
if [ $# -lt 1 ]; then
echo -n $currentsong' ' > /tmp/currentsong.txt
elif [ $1 == "-t" ]; then
split=" - "
artist=${currentsong%%${split}*}
song=${currentsong#*${split}}
printf "%40s\n" "$artist - " > /tmp/currentsong.txt
printf "%40s\n" "$song" >> /tmp/currentsong.txt
fi
# debug
# tail /tmp/currentsong.txt
sleep $interval;
done
echo 'Exiting and deleting songfile';
rm -f /tmp/currentsong.txt
exit 0;
fi