-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlfmbatchedit.sh
executable file
·163 lines (135 loc) · 4.08 KB
/
lfmbatchedit.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
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
155
156
157
158
159
160
161
162
163
#!/bin/bash
source "utils.sh"
source "scrobble_edit.sh"
burst_size="${LFMEDIT_BURST_SIZE:-15}"
burst_interval="${LFMEDIT_BURST_INTERVAL:-25}"
usage() {
echo "usage: $(basename "$0") [options] <file> [file2 ...]"
echo
echo "Available options:"
echo
echo " -n dry run: print edits, but do not apply them"
echo " -Y do not ask to confirm an edit (ignored when -n is used)"
echo " -V enable verification of edits"
echo " -d increase level of debug prints"
echo " -h display this help message"
echo
echo "Input files are diffs of last.fm library export files"
echo "compatible with the format used by lastscrape tool."
echo
}
parseArguments() {
while getopts ":nYVdh" options; do
case "${options}" in
n)
dryRun="yes"
logDebug "Dry run, changes will not be applied."
;;
Y)
dontAsk="yes"
logDebug "Asking for edit confirmation is disabled."
;;
V)
enableVerification="yes"
logDebug "Verification of edits is enabled."
;;
d)
((debugLevel++))
;;
h)
usage && exit 0
;;
*)
# quietly ignore unsupported options
;;
esac
done
if [[ ${debugLevel} -ge 2 ]]; then
verbose="--verbose"
silent=""
else
verbose=""
silent="--silent"
fi
if [[ ${debugLevel} -ge 3 ]]; then
set -x
fi
logDebug "burst size: ${burst_size}, burst interval: ${burst_interval}"
logDebug "args = ${*}"
shift $((OPTIND-1))
fileList="${*}"
logDebug "file list = ${fileList}"
if [[ ${*} == "" ]]; then
logError "Missing mandatory parameters!"
echo
usage
return 1
fi
}
logAppliedEdit() {
dateStr=$(date -Iseconds)
echo -e "${dateStr}\t+${timestamp}\t${newTitle}\t${newArtist}\t${newAlbum}" >> applied.log
}
logFailedEdit() {
if [[ ! -v dryRun || "${dryRun}" != "yes" ]]; then
dateStr=$(date -Iseconds)
echo -e "${dateStr}\t+${timestamp}\t${newTitle}\t${newArtist}\t${newAlbum}" >> failed.log
fi
}
applyChangesFrom() {
local -r file="${1}"
local -r nChange=$(grep -c -E "^\+[0-9]{10}" "${file}")
local n=0
local remaining=${nChange}
local -r timeStart=$(date +%s)
grep -E "^\+[0-9]{10}" "${file}" | sed "s/^+//" | tr '\011' '\037' |
while IFS=$'\037' read -r -a scrobble; do
((n++))
((remaining--))
unset -v originalAlbumArtist newAlbumArtist
echo
logInfo "editing scrobble ${n} of ${nChange}"
timestamp="${scrobble[0]}"
newTitle="${scrobble[1]}"
newArtist="${scrobble[2]}"
newAlbum="${scrobble[3]}"
logDebug "timestamp = ${timestamp}, newTitle = ${newTitle}, newArtist = ${newArtist}, newAlbum = ${newAlbum}"
if ! requestOriginalScrobbleData; then
logFailedEdit
continue
fi
if ! readOriginalScrobbleData; then
logFailedEdit
continue
fi
if ! requestScrobbleEdit; then
logFailedEdit
continue
fi
if ! verifyScrobbleEdit; then
logFailedEdit
continue
fi
logAppliedEdit
if [[ ! -v dryRun ]] || [[ "${dryRun}" != "yes" ]] &&
(( n % burst_size == 0 )) && [[ ${remaining} -ne 0 ]]; then
echo
pauseEditing "${burst_interval}"
fi
done
echo
logInfo "Processed ${nChange} scrobbles from \"${file}\" in $(($(date +%s)-timeStart)) seconds."
}
processFiles() {
local f
for f in ${fileList}; do
logInfo "processing \"${f}\""
applyChangesFrom "${f}"
done
}
main() {
parseArguments "${@}" || exit 1
checkAuthTokens || exit 2
processFiles
}
main "${@}"