-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlasttime.js
364 lines (308 loc) · 13.7 KB
/
lasttime.js
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const LASTFM_API_URL = "https://ws.audioscrobbler.com/2.0/?format=json&method=";
const MB_API_URL = "https://musicbrainz.org/ws/2/";
const LASTTIME_VERSION = 0x00000002;
var lastfm_api_key = "";
var from_time = new Date("01 January, 2018").getTime() * 0.001;
var lastfm_username = "";
// artist, album, track
var lastfm_artists = {};
var lastfm_tracks = [];
var lastfm_total_time = 0;
var lastfm_trackinfo_counter = 0;
var lastfm_all_scrobbles_loaded = false;
var lastfm_pages_loaded = 0;
var lastfm_top_artists = [];
var mb_api_request_counter = 0;
var missed_tracks = [];
var track_info_cache = (function() {
try {
if(JSON.parse(localStorage.getItem("lasttime.track_info_cache_version")) == LASTTIME_VERSION)
return JSON.parse(localStorage.getItem("lasttime.track_info_cache"));
throw "Cache version outdated.";
} catch(e) {
console.error(e);
localStorage.setItem("lasttime.track_info_cache", "{}");
localStorage.setItem("lasttime.track_info_cache_version", JSON.stringify(LASTTIME_VERSION));
return {};
}
})();
var $progress_info = $("#progress-info");
var $progress_bar = $(".progress-bar");
function lastfm_api_request(method, params) {
var url = LASTFM_API_URL + method + "&api_key=" + lastfm_api_key;
for (const key in params) {
if (params.hasOwnProperty(key))
url += "&" + key + "=" + params[key];
}
return $.getJSON(url).catch(function() {
return null;
});
}
function mb_api_lookup(entity, mbid) {
var url = MB_API_URL + entity + "/" + mbid + "?fmt=json";
return $.getJSON(url).catch(function() {
return null;
});
}
function mb_api_query(entity, search_fields) {
var url = MB_API_URL + entity + "/" + "?fmt=json&query=";
var first = true;
for (const key in search_fields) {
if (search_fields.hasOwnProperty(key)) {
if (!first)
url += " AND ";
else
first = false;
url += key + ":\"" + search_fields[key].replace(/"+/g, "") + "\"";
}
}
return $.getJSON(url).catch(function() {
return { count: 0 };
});
}
function on_track_get_info(duration, info_index) {
lastfm_tracks[info_index].duration = duration;
if (lastfm_all_scrobbles_loaded) {
let tracks_loaded = lastfm_tracks.length - lastfm_trackinfo_counter;
$progress_bar.attr("aria-valuenow", tracks_loaded).width(tracks_loaded * 100 / lastfm_tracks.length + "%");
$progress_info.text("Retrieving track info... " + tracks_loaded + " / " + lastfm_tracks.length);
}
if (--lastfm_trackinfo_counter == 0 && lastfm_all_scrobbles_loaded)
lastfm_on_all_data();
}
function lastfm_collate_data() {
lastfm_top_artists = [];
lastfm_total_time = 0;
for (const artist_key in lastfm_artists) { // collate artists
if (!lastfm_artists.hasOwnProperty(artist_key))
continue;
let artist_data = lastfm_artists[artist_key];
artist_data.total_time = 0;
for (const album_key in artist_data.albums) { // collate albums
if (!artist_data.albums.hasOwnProperty(album_key))
continue;
let album_data = artist_data.albums[album_key];
album_data.total_time = 0;
for (const track_key in album_data.tracks) { // collate tracks
if (!album_data.tracks.hasOwnProperty(track_key))
continue;
let track_data = album_data.tracks[track_key];
track_data.total_time = lastfm_tracks[track_data.info_index].hasOwnProperty("duration") ?
lastfm_tracks[track_data.info_index].duration * track_data.count : 0;
album_data.total_time += track_data.total_time;
}
artist_data.total_time += album_data.total_time;
}
lastfm_top_artists.push({ name: artist_data.name, duration: artist_data.total_time });
lastfm_total_time += artist_data.total_time;
}
lastfm_top_artists.sort(function(a, b) { return b.duration - a.duration; });
}
function show_statistics() {
$("#minutes-listened").text(Math.floor(lastfm_total_time / 60000).toLocaleString());
$("#hours-listened").text(Math.floor(lastfm_total_time / 3600000).toLocaleString());
var $top_artists_list = $("#top-artists .top-list");
$top_artists_list.empty();
let max = Math.min(10, lastfm_top_artists.length);
for (let i = 0; i < max; i++) {
const elem = lastfm_top_artists[i];
$top_artists_list.append("<div class='list-item'>" + elem.name + " <span>" + Math.floor(elem.duration / 60000).toLocaleString() + " mins</span></div>");
}
$("#statistics").collapse("show");
}
function lastfm_on_all_data() {
$progress_info.text("Collating data...");
lastfm_collate_data();
$progress_info.text("Done!");
setTimeout(function() {
$(".progress").collapse("hide");
$("#progress-update").collapse("hide");
$progress_info.collapse("hide");
}, 2000);
console.log(lastfm_artists);
console.log(Math.floor(lastfm_total_time / 60000) + " mins");
show_statistics();
}
function retrieve_track_info(artist_data, album_data, track_data) {
var artist_id = artist_data.mbid === "" ? artist_data.name : artist_data.mbid;
var album_id = album_data.mbid === "" ? album_data.name : album_data.mbid;
var track_id = track_data.mbid === "" ? track_data.name : track_data.mbid;
var cache_id = artist_id + "#" + album_id + "#" + track_id;
var info_index = track_data.info_index;
if(track_info_cache.hasOwnProperty(cache_id))
{
let cached_track_data = track_info_cache[cache_id];
if(cached_track_data.hasOwnProperty("duration"))
{
if(cached_track_data.duration === 0 || cached_track_data.is_missed_track === true)
missed_tracks.push(cache_id);
else
{
console.log("from cache: ", cached_track_data);
on_track_get_info(cached_track_data.duration, info_index);
return;
}
}
}
track_info_cache[cache_id] = {};
if (track_data.mbid === "") {
setTimeout(function () {
mb_api_query("recording", { recording: track_data.name, artist: artist_data.name }).done(function (data) {
data = data.count ? data.recordings[0] : null;
if (data === null || !data.hasOwnProperty("length")) {
// no length data, try last.fm data.
lastfm_api_request("track.getinfo", { artist: artist_data.name, track: track_data.name }).done(function (data) {
if(data === null)
data = {};
if(!data.hasOwnProperty("track"))
data.track = { duration: "0" };
data = data.track;
console.log("from last.fm: ", data);
let dur = parseInt(data.duration);
if(dur === 0) {
track_info_cache[cache_id].is_missed_track = true;
missed_tracks.push(cache_id);
}
track_info_cache[cache_id].duration = dur;
localStorage["lasttime.track_info_cache"] = JSON.stringify(track_info_cache);
on_track_get_info(dur, info_index);
});
}
else
{
console.log("from musicbrainz: ", data);
track_info_cache[cache_id].duration = data.length;
localStorage["lasttime.track_info_cache"] = JSON.stringify(track_info_cache);
on_track_get_info(data.length, info_index);
}
});
}, mb_api_request_counter * 1000); // 1s delay before each call
}
else {
setTimeout(function () {
mb_api_lookup("recording", track_data.mbid).done(function (data) {
if (data === null || !data.hasOwnProperty("length")) {
// no length data, try last.fm data.
lastfm_api_request("track.getinfo", { mbid: track_data.mbid }).done(function (data) {
if(data === null)
data = {};
if(!data.hasOwnProperty("track"))
data.track = { duration: "0" };
data = data.track;
console.log("from last.fm: ", data);
let dur = parseInt(data.duration);
if(dur === 0) {
track_info_cache[cache_id].is_missed_track = true;
missed_tracks.push(cache_id);
}
track_info_cache[cache_id].duration = dur;
localStorage["lasttime.track_info_cache"] = JSON.stringify(track_info_cache);
on_track_get_info(dur, info_index);
});
}
else
{
console.log("from musicbrainz: ", data);
track_info_cache[cache_id].duration = data.length;
localStorage["lasttime.track_info_cache"] = JSON.stringify(track_info_cache);
on_track_get_info(data.length, info_index);
}
});
}, mb_api_request_counter * 1000); // 1s delay before each call
}
++mb_api_request_counter;
}
function lastfm_on_recent_tracks(data) {
if(data === null)
{
console.error("Loading page " + lastfm_pages_loaded + " failed! Retrying...");
lastfm_api_request("user.getrecenttracks", { user: lastfm_username, from: from_time, limit: 200, page: lastfm_pages_loaded + 1 }).done(lastfm_on_recent_tracks);
}
console.log(data);
data = data.recenttracks;
var attr = data["@attr"];
var tracks = data.track;
var curr_page = parseInt(attr.page);
var total_pages = parseInt(attr.totalPages);
console.log(curr_page + " / " + total_pages);
for (track of tracks) {
let artist_id = track.artist.mbid === "" ? track.artist["#text"] : track.artist.mbid;
let album_id = track.album.mbid === "" ? track.album["#text"] : track.album.mbid;
let track_id = track.mbid === "" ? track.name : track.mbid;
if (!lastfm_artists.hasOwnProperty(artist_id)) {
let artist_data = lastfm_artists[artist_id] = {};
artist_data.name = track.artist["#text"];
artist_data.mbid = track.artist.mbid;
artist_data.count = 0;
artist_data.total_time = 0;
artist_data.albums = {};
}
let artist_data = lastfm_artists[artist_id];
++artist_data.count;
if (!artist_data.albums.hasOwnProperty(album_id)) {
let album_data = artist_data.albums[album_id] = {};
album_data.name = track.album["#text"];
album_data.mbid = track.album.mbid;
album_data.count = 0;
album_data.total_time = 0;
album_data.tracks = {};
}
let album_data = artist_data.albums[album_id];
++album_data.count;
if (!album_data.tracks.hasOwnProperty(track_id)) {
let track_data = album_data.tracks[track_id] = {};
track_data.name = track.name;
track_data.mbid = track.mbid;
track_data.count = 0;
track_data.total_time = 0;
track_data.duration = 0;
var info_index = lastfm_tracks.length;
track_data.info_index = info_index;
lastfm_tracks.push({});
++lastfm_trackinfo_counter;
retrieve_track_info(artist_data, album_data, track_data);
}
let track_data = album_data.tracks[track_id];
++track_data.count;
}
if (curr_page == 1)
$progress_bar.attr("aria-valuemax", total_pages);
lastfm_pages_loaded = curr_page;
$progress_bar.attr("aria-valuenow", curr_page).width(curr_page * 100 / total_pages + "%");
$progress_info.text("Loading scrobbles... " + curr_page + " / " + total_pages);
if (curr_page < total_pages)
lastfm_api_request("user.getrecenttracks", { user: lastfm_username, from: from_time, limit: 200, page: curr_page + 1 }).done(lastfm_on_recent_tracks);
else {
let tracks_loaded = lastfm_tracks.length - lastfm_trackinfo_counter;
$progress_bar.attr({ "aria-valuemax": lastfm_tracks.length, "aria-valuenow": tracks_loaded })
.width(tracks_loaded * 100 / lastfm_tracks.length + "%");
$progress_info.text("Retrieving track info... " + tracks_loaded + " / " + lastfm_tracks.length);
lastfm_all_scrobbles_loaded = true;
// if somehow all tracks loaded before pages loaded... probably due to cache.
if (lastfm_trackinfo_counter == 0)
lastfm_on_all_data();
}
}
$("#run-form").on("submit", function(e) {
e.preventDefault();
if(this.checkValidity() === false)
{
this.classList.add('was-validated');
return;
}
mb_api_request_counter = 0;
var $from_date = $("#date-from-input");
if($from_date.val())
from_time = new Date($from_date.val()).getTime() * 0.001;
$(".progress").collapse("show");
$("#progress-update").collapse("show");
$progress_info.collapse("show");
$(this).collapse("hide");
lastfm_api_key = $("#api-key-input").val();
lastfm_username = $("#username-input").val();
lastfm_api_request("user.getrecenttracks", { user: lastfm_username, from: from_time, limit: 200 }).done(lastfm_on_recent_tracks);
$("#progress-update").on("click", function() {
lastfm_collate_data();
show_statistics();
})
});