-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomplayed.sh
128 lines (113 loc) · 3.86 KB
/
randomplayed.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
set -e
print_help(){
cat << 'EOF'
A kid3-based utility to add a custom TXXX frame for writing LastPlayedDate data directly to tags.
Generates and applies a random LastPlayedDate to each tag with TIMEVAL of either sql or epoch time.
Usage: randomplayed.sh [option] DIRPATH TIMEVAL
options:
-h display this help file
-m minimum subdirectory depth from top directory of music library to music files (default: 1)
-n specify TXXX frame name (default: Songs-DB_Custom1)
-q quiet - hide terminal output
Modifies tags to enable other utilities to create custom playlists using "LastPlayedDate" history.
Tag version required is ID3v2.3.
Requires kid3. Using the kid3-cli utility, scans all music files in the DIRPATH and checks each
for existence of the frame name identified (default is Songs-DB_Custom1). If it does not exist,
creates a TXXX frame with that name, then assigns a random LastTimePlayed time value to each tag.
Upper date limit will vary, depending on the rating for the given track.
Parameter TIMEVAL must be specified as either sql or epoch.
Time to complete varies by processor and can take time for large libraries. Check tag output
quality more quickly by testing on a subdirectory first.
EOF
}
showdisplay=1
dirdepth=1
framename="Songs-DB_Custom1"
upperlimit=1500
group1low=48 #(popularimeter 48-135)
group2low=136 #(popularimeter 136-165)
group3low=166 #(popularimeter 166-191)
group4low=192 #(popularimeter 192-228)
group5low=229 #(popularimeter 229-255)
# Use getops to set any user-assigned options
while getopts ":hm:n:q" opt; do
case $opt in
h)
print_help
exit 0;;
m)
dirdepth=$OPTARG
;;
n)
framename=$OPTARG
;;
q)
showdisplay=0 >&2
;;
\?)
printf 'Invalid option: -%s\n' "$OPTARG"
exit 1
;;
:)
printf 'Option requires an argument: %s\n' "$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND-1))
## Verify user provided required, valid path and time argument
if [[ -z "$1" ]] || [[ -z "$2" ]]
then
printf '\n%s\n' "Missing positional argument(s)"
print_help
exit 1
fi
# positional variables
libpath=$1
timetype=$2
find "$libpath" -mindepth "$dirdepth" > /tmp/albumdirs;
if [ $showdisplay == 0 ]
then
printf '%s\n' "Locating all subdirectories under this path..." > /dev/null 2>&1
else
printf '%s\n' "Locating all subdirectories under this path..."
fi
# This is for the spinner, to show the program is working
i=1
sp="/-\|"
echo -n ' '
# add random value for LastPlayedDate
while IFS= read -r line; do
popmfound=$(kid3-cli -c "get POPM.Rating" "$line")
if [[ "$popmfound" -ge "$group5low" ]]; then upperlimit=60;
elif [[ "$popmfound" -ge "$group4low" ]]; then upperlimit=120;
elif [[ "$popmfound" -ge "$group3low" ]]; then upperlimit=180;
elif [[ "$popmfound" -ge "$group2low" ]]; then upperlimit=360;
elif [[ "$popmfound" -ge "$group1low" ]]; then upperlimit=730; fi
exists=$(kid3-cli -c "get ""$framename" "$line")
if [ -z "$exists" ]
then
sudo kid3-cli -c "set TXXX.Description ""$framename" "$line"
myrandomval="$(shuf -i 0-"$upperlimit" -n1)"
adjtimedays=$(date +%s --date="-$myrandomval days")
if [ "$timetype" == "sql" ]
then
setsqltime=$(printf "%.6f \n" "$(echo "$adjtimedays/86400 + 25569"| bc -l)")
sudo kid3-cli -c "set ""$framename"" $setsqltime" "$line"
fi
if [ "$timetype" == "epoch" ]
then
sudo kid3-cli -c "set ""$framename"" $adjtimedays" "$line"
fi
else
printf '%s\n' 'There is not a valid time variable or there is already a frame for this value:' "$line"
exit 1
fi
if [ $showdisplay == 0 ]
then
printf '\b%s' "${sp:i++%${#sp}:1}"
else
echo "$line upperlimit: $upperlimit myrandomval: $myrandomval"
fi
done < /tmp/albumdirs