-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
songi.in
154 lines (150 loc) · 4 KB
/
songi.in
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
#!/bin/sh
#
# $Id: songi.in,v 1.7 2024-04-04 20:51:29+05:30 Cprogrammer Exp mbhangui $
#
get_mpd_conf_value()
{
grep "^$1" @sysconfdir@/mpd.conf|awk '{print $2}' | \
sed -e 's{"{{g'
}
locate_file()
{
dir="$2"
file=$(basename "$dir")
while true
do
t=$(dirname "$dir")
if [ -f $1/"$file" ] ; then
echo "$1/$file" | sed s{"$1"/{{
break
fi
dir="$t"
file="$(basename "$dir")/$file"
done
}
display_info()
{
turi=`echo $1|sed -e "s{'{''{g"`
out=$(mktemp -t mpdevXXXXXXXXXX)
Q="SELECT title,play_count,last_played,"
Q="$Q rating, karma, duration,"
Q="$Q artist, album, track, genre,"
Q="$Q date, composer, performer, disc,"
Q="$Q datetime(date_added, 'unixepoch', 'localtime'),"
Q="$Q datetime(last_modified, 'unixepoch', 'localtime')"
Q="$Q FROM song WHERE uri='$turi';"
sqlite3 -noheader -line -batch $stats_file "$Q" | sed \
-e 's{.*datetime(date_added.*){ Date Added{g' \
-e 's{.*datetime(last_modified.*){Last Updated{g' \
-e 's{ {{g' > $out
t1=$(sed -n 3p < $out | awk '{print $3}')
if [ -z "$t1" ] ; then
t1=0
fi
# play_count
t2=$(sed -n 2p < $out | awk '{print $3}')
if [ -z "$t2" ] ; then
t2=0
fi
if [ $t1 -eq 0 -o $t2 -eq 0 ] ; then
last_played="never"
else
t2=$(date +%s)
diff=$(echo $t1 $t2 | awk '{printf("%d\n", $2-$1)}')
days=$(echo $diff | awk '{printf("%d\n", $1/86400)}')
hours=$(echo $diff | awk '{printf("%d\n", ($1%86400)/3600)}')
mins=$(echo $diff | awk '{printf("%d\n", ($1%3600)/60)}')
secs=$(echo $diff | awk '{printf("%d\n", $1%60)}')
last_played="$(date --date=@"$t1")\n Last Played = $days days $hours hr $mins min $secs sec ago"
fi
# duration
t1=$(sed -n 6p < $out | awk '{print $3}')
hours=$(echo $t1 | awk '{printf("%d\n", ($1%86400)/3600)}')
mins=$(echo $t1 | awk '{printf("%d\n", ($1%3600)/60)}')
secs=$(echo $t1 | awk '{printf("%d\n", $1%60)}')
if [ "$hours" = "0" ] ; then
duration="$mins min $secs sec ($t1)"
else
duration="$hours hr $mins min $secs sec ($t1)"
fi
sed \
-e "s{last_played.*{Last Played = $last_played{g" \
-e "s{duration.*{Duration = $duration{g" < $out
/bin/rm -f $out
}
if [ $# -eq 0 ] ; then
uri=$(mpc current -f %file%)
if [ -z "$uri" ] ; then
echo "No song playing and no options given" 1>&2
exit 1
fi
fi
music_dir=`get_mpd_conf_value music_directory`
if [ $? -ne 0 ] ; then
echo "could not get music directory from mpd.conf" 1>&2
exit 1
fi
sticker_file=$(get_mpd_conf_value sticker_file)
if [ $? -ne 0 ] ; then
echo "sticker database not enabled in @sysconfdir@/mpd.conf" 1>&2
return 1
else
if [ ! -f $sticker_file ] ; then
echo "$sticker_file: No such file or directory" 1>&2
return 1
fi
fi
stats_dir=$(dirname $sticker_file)
stats_file=$stats_dir/stats.db
if [ ! -f $stats_file ] ; then
echo "$stats_file: No such file or directory" 1>&2
return 1
fi
if [ $# -eq 0 ] ; then
t=$(basename "$uri")
echo "---- $t ------------------------"
display_info "$uri"
echo "----------------------------"
exit $?
fi
for i in "$@"
do
uri=$(locate_file "$music_dir" "$i")
if [ -n "$uri" ] ; then
t=$(basename "$uri")
ext=$(echo $t|rev|cut -d. -f1|rev)
case $ext in
dsf|flac|m4a|mp3|MP3|ogg|wma|wv)
echo "---- $t ------------------------"
display_info "$uri"
echo "----------------------------"
;;
*)
echo "Unsupported extension $ext in $t" 1>&2
;;
esac
fi
done
#
# $Log: songi.in,v $
# Revision 1.7 2024-04-04 20:51:29+05:30 Cprogrammer
# fixed last_played when song has never been played
#
# Revision 1.6 2024-03-26 19:24:12+05:30 Cprogrammer
# display last played in days, hours, mins, secs ago
#
# Revision 1.5 2022-06-20 01:07:30+05:30 Cprogrammer
# use directories set in ./configure
#
# Revision 1.4 2021-10-15 18:41:52+05:30 Cprogrammer
# formatted for better display
#
# Revision 1.3 2021-10-14 22:39:24+05:30 Cprogrammer
# bugfix for period in filenames
#
# Revision 1.2 2021-10-01 12:05:56+05:30 Cprogrammer
# removed name column from song table in stats db
#
# Revision 1.1 2021-09-30 20:31:57+05:30 Cprogrammer
# Initial revision
#