-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrobble_edit.sh
313 lines (260 loc) · 11.5 KB
/
scrobble_edit.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/bash
source "utils.sh"
# Requires following global variables to be set:
# timestamp, silent, verbose
userAgent="lfmedit/1.0.7 +https://github.com/arcctgx/lfmedit"
handleApiErrors() {
local -r httpCode="${1}"
if [[ ${httpCode} -ne 200 ]]; then
logError "got HTTP error ${httpCode} while sending last.fm API request!"
fi
if ! jq -e . "${apiResponsePath}" &> /dev/null; then
logError "last.fm API response is not valid JSON!"
rm -f "${verbose}" "${apiResponsePath}"
return 1
fi
if [[ ${debugLevel} -ge 2 ]]; then
jq --monochrome-output . "${apiResponsePath}"
fi
if [[ "$(jq 'has("error")' "${apiResponsePath}")" == "true" ]]; then
local -r errcode=$(jq '.error' "${apiResponsePath}")
local -r message=$(jq -r '.message' "${apiResponsePath}")
logError "last.fm error ${errcode}: ${message}"
rm -f "${verbose}" "${apiResponsePath}"
return 2
fi
}
requestOriginalScrobbleData() {
# I'm not sure if scrobbles submitted before Feb 13 2005, 10:20:00 UTC
# (1108290000 Unix time) can be handled by this method.
# Adding 100 seconds to the limit because the scrobbles without reliable
# timestamp information reach 1108290047 in my exported data.
local -r earliest=1108290100
if [[ ${timestamp} -le ${earliest} ]]; then
logError "Editing scrobbles before Unix time ${earliest} is not supported!"
return 1
fi
apiResponsePath="$(mktemp -t lfmquery.json.XXXXXX)"
logDebug "apiResponsePath = ${apiResponsePath}"
local -r apiRoot="http://ws.audioscrobbler.com/2.0/"
local -r apiKey="29a5d6e1ddfce0e472ce9a328ac21ff5"
local -r timeFrom="${timestamp}"
local -r timeTo=$((timeFrom+1)) # a hack to limit results to a single scrobble
local -r perPage=1 # request one scrobble per page...
local -r page=1 # ...and only the first page of results.
local httpCode=""
local curlStatus=""
local url=""
logDebug "username = ${lastfm_username}, timeFrom = ${timeFrom}, timeTo = ${timeTo}"
url+="${apiRoot}?method=user.getrecenttracks&api_key=${apiKey}"
url+="&user=${lastfm_username}"
url+="&from=${timeFrom}&to=${timeTo}&limit=${perPage}&page=${page}&format=json"
# shellcheck disable=SC2086
httpCode=$(curl ${silent} -o "${apiResponsePath}" -w "%{http_code}\n" "${url}" \
--user-agent "${userAgent}")
curlStatus=${?}
if [[ ${curlStatus} -ne 0 ]]; then
logError "failed to send last.fm API request! curl error = ${curlStatus}"
rm -f "${verbose}" "${apiResponsePath}"
return 2
fi
logDebug "last.fm API request was sent, httpCode = ${httpCode}"
handleApiErrors "${httpCode}" || return 3
}
readOriginalScrobbleData() {
# We expect that there are at most two tracks in the response:
# "now playing" (optional), and the one we want (always last).
# The attribute "total" holds the number of returned scrobbles,
# excluding "now playing" one. This should be equal to one.
local -r total=$(jq -r '.recenttracks."@attr".total' "${apiResponsePath}")
logDebug "total = ${total}"
if [[ ${total} -ne 1 ]]; then
logError "unexpected number of scrobbles in API response! (got ${total} instead of 1)"
rm -f "${verbose}" "${apiResponsePath}"
return 1
fi
# Compare timestamps to make sure we got the scrobble we wanted.
local -r uts=$(jq -r '.recenttracks.track[-1].date.uts' "${apiResponsePath}")
if [[ ${uts} -eq ${timestamp} ]]; then
logDebug "got scrobble with matching timestamp: uts = $uts"
else
logError "scrobble timestamp mismatch! (expected: ${timestamp}, received: ${uts})"
rm -f "${verbose}" "${apiResponsePath}"
return 2
fi
originalTitle=$(jq -r '.recenttracks.track[-1].name' "${apiResponsePath}")
originalArtist=$(jq -r '.recenttracks.track[-1].artist["#text"]' "${apiResponsePath}")
originalAlbum=$(jq -r '.recenttracks.track[-1].album["#text"]' "${apiResponsePath}")
logDebug "originalTitle = ${originalTitle}, originalArtist = ${originalArtist}, originalAlbum = ${originalAlbum}"
# There's no way to get original album artist from last.fm API.
# If it was not provided by -Z option, we have to guess.
# In most cases this will be the same as track artist.
if [[ ! -v originalAlbumArtist ]]; then
logDebug "assuming original album artist is the same as original track artist"
originalAlbumArtist="${originalArtist}"
fi
# If there's no original album, then original album artist must be blank too.
if [[ -z "${originalAlbum}" ]]; then
logDebug "no original album set for scrobble, assuming empty original album artist."
originalAlbumArtist=""
fi
logDebug "originalAlbumArtist = ${originalAlbumArtist}"
rm -f "${verbose}" "${apiResponsePath}"
}
setNewAlbumArtist() {
if [[ ! -v newAlbumArtist ]]; then
if [[ "${newArtist}" != "${originalArtist}" ]]; then
logDebug "new album artist not set when changing artist, assuming same as new artist."
newAlbumArtist="${newArtist}"
else
logDebug "new album artist not set, using original album artist."
newAlbumArtist="${originalAlbumArtist}"
fi
fi
if [[ -z "${newAlbum}" ]]; then
logDebug "new album is blank, blanking new album artist to match."
newAlbumArtist=""
fi
if [[ -z "${originalAlbum}" && -n "${newAlbum}" && -z "${newAlbumArtist}" ]]; then
logDebug "assuming new album artist is the same as original artist when adding album information."
newAlbumArtist="${originalArtist}"
fi
}
setNewScrobbleData() {
if [[ ! -v newTitle ]]; then
newTitle="${originalTitle}"
fi
if [[ ! -v newArtist ]]; then
newArtist="${originalArtist}"
fi
if [[ ! -v newAlbum ]]; then
newAlbum="${originalAlbum}"
fi
setNewAlbumArtist
logDebug "newTitle = ${newTitle}, newArtist = ${newArtist}, newAlbum = ${newAlbum}, newAlbumArtist = ${newAlbumArtist}"
}
detectInvalidChange() {
if [[ "${originalTitle}" == "${newTitle}" && \
"${originalArtist}" == "${newArtist}" && \
"${originalAlbum}" == "${newAlbum}" && \
"${originalAlbumArtist}" == "${newAlbumArtist}" ]]; then
logError "Edit does not change anything!"
return 1
fi
if [[ "${originalTitle,,}" == "${newTitle,,}" && \
"${originalArtist,,}" == "${newArtist,,}" && \
"${originalAlbum,,}" == "${newAlbum,,}" && \
"${originalAlbumArtist,,}" == "${newAlbumArtist,,}" ]]; then
logError "Case-only changes cannot be applied!"
return 2
fi
if [[ -z "${newTitle}" || -z "${newArtist}" ]]; then
logError "Can't erase title or artist!"
return 3
fi
}
printEditData() {
echo -e "\e[94m-${timestamp} ${originalTitle} ${originalArtist} ${originalAlbum} ${originalAlbumArtist}\e[0m"
echo -e "\e[92m+${timestamp} ${newTitle} ${newArtist} ${newAlbum} ${newAlbumArtist}\e[0m"
}
requestScrobbleEdit() {
setNewScrobbleData
logInfo "This is the edit that will be applied:"
printEditData
detectInvalidChange || return 1
local -r url="https://www.last.fm/user/${lastfm_username}/library/edit?edited-variation=recent-track"
local -r referer="https://www.last.fm/user/${lastfm_username}"
local -r cookies="csrftoken=${lastfm_csrf}; sessionid=${lastfm_session_id}"
local httpCode=""
local curlStatus=""
requestConfirmation || return 2
# shellcheck disable=SC2086
httpCode=$(curl ${silent} -o /dev/null -w "%{http_code}\n" "${url}" \
--user-agent "${userAgent}" \
--referer "${referer}" \
--cookie "${cookies}" \
--data-urlencode "csrfmiddlewaretoken=${lastfm_csrf}" \
--data-urlencode "track_name=${newTitle}" \
--data-urlencode "artist_name=${newArtist}" \
--data-urlencode "album_name=${newAlbum}" \
--data-urlencode "album_artist_name=${newAlbumArtist}" \
--data-urlencode "timestamp=${timestamp}" \
--data-urlencode "track_name_original=${originalTitle}" \
--data-urlencode "artist_name_original=${originalArtist}" \
--data-urlencode "album_name_original=${originalAlbum}" \
--data-urlencode "album_artist_name_original=${originalAlbumArtist}" \
--data-urlencode "submit=edit-scrobble")
curlStatus="${?}"
if [[ ${curlStatus} -ne 0 ]]; then
logError "failed to send last.fm edit request! curl error = ${curlStatus}"
return 3
fi
logDebug "last.fm edit request was sent, httpCode = ${httpCode}"
if [[ ${httpCode} -ne 200 ]]; then
case "${httpCode}" in
403)
logError "HTTP error ${httpCode}: check last.fm CSRF token"
;;
404)
logError "HTTP error ${httpCode}: check last.fm session ID"
;;
406)
logError "HTTP error ${httpCode}: throttling detected, try again later"
;;
500)
logError "HTTP error ${httpCode}: check edit parameters (wrong original album artist?)"
;;
*)
logError "HTTP error ${httpCode}"
;;
esac
return 4
fi
}
verifyScrobbleEdit() {
# Method: send a "pre-edit" POST request with parameters containing new
# scrobble data (expected to be set on last.fm side after successful edit).
# In case of unsuccessful edit last.fm will still have original data, and the
# request will fail with HTTP error 500. Otherwise the request will be successful,
# and it will contain new scrobble data in its body. For now I'm assuming I can
# rely on the HTTP status codes, and don't parse the response.
# It turns out false positives are possible, so don't enable verification by default.
if [[ ! -v enableVerification || "${enableVerification}" != "yes" ]]; then
return 0
fi
local -r url="https://www.last.fm/user/${lastfm_username}/library/edit?edited-variation=recent-track"
local -r referer="https://www.last.fm/user/${lastfm_username}"
local -r cookies="csrftoken=${lastfm_csrf}; sessionid=${lastfm_session_id}"
local httpCode=""
local curlStatus=""
# shellcheck disable=SC2086
httpCode=$(curl ${silent} -o /dev/null -w "%{http_code}\n" "${url}" \
--user-agent "${userAgent}" \
--referer "${referer}" \
--cookie "${cookies}" \
--data-urlencode "csrfmiddlewaretoken=${lastfm_csrf}" \
--data-urlencode "artist_name=${newArtist}" \
--data-urlencode "track_name=${newTitle}" \
--data-urlencode "album_name=${newAlbum}" \
--data-urlencode "album_artist_name=${newAlbumArtist}" \
--data-urlencode "timestamp=${timestamp}")
curlStatus="${?}"
if [[ ${curlStatus} -ne 0 ]]; then
logError "failed to send last.fm verification request! curl error = ${curlStatus}"
return 1
fi
logDebug "last.fm verification request was sent, httpCode = ${httpCode}"
case "${httpCode}" in
200)
logInfo "\e[1m\e[32mverification passed!\e[0m Scrobble edited successfully"
;;
500)
logError "HTTP error ${httpCode}: last.fm still has old scrobble data, edit failed!"
return 2
;;
*)
logError "HTTP error ${httpCode}: something else went wrong, not sure about edit status"
return 3
;;
esac
}