-
Notifications
You must be signed in to change notification settings - Fork 0
/
discogs.js
628 lines (518 loc) · 15.8 KB
/
discogs.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*============================
Javascript for Minimal Discogs
Devin Smith - http://devinsmith.work
April - May 2020
============================*/
//-------------------------
// Global vars
var masterData = [];
var currentIndex = 0;
var currentInfoNavState = 0;
//================================
// Testing functions (disabled). For use w/ downloaded JSON from Discogs
//================================
$(document).ready(function() {
// getDiscogsData();
// processDiscogsJSON(demoData);
});
//================================
// Search functions and Discogs API calls
//================================
//-------------------------
// Gets the HTML input values and builds the URL for the Discogs API call
function executeSearch(includeYear) {
let sStyle = $('#searchStyle').val();
let sYear = $('#searchYear').val();
let sPageQ = $('#searchPageQ').val();
let discogsSearch = "https://api.discogs.com/database/search?";
// [GITHUB] Fill these in with your Discogs.com Key and Secret
let key = "";
let secret = "";
let discogsFullURL = "";
if (includeYear) {
discogsFullURL = discogsSearch +
"style=" + sStyle.split(" ").join("+") + "&" +
"year=" + sYear + "&" +
"type=" + "release" + "&" +
"page=1&per_page=" + sPageQ + "&" +
"key=" + key + "&" +
"secret=" + secret;
} else {
discogsFullURL = discogsSearch +
"style=" + sStyle.split(" ").join("+") + "&" +
"type=" + "relase" + "&" +
"page=1&per_page=" + sPageQ + "&" +
"key=" + key + "&" +
"secret=" + secret;
}
// Clear previous master data and images
masterData = [];
$("#imageGrid").empty();
// Passes the URL to the API call function
getDiscogsData(discogsFullURL);
}
//-------------------------
// Calls the API and passes the JSON data for processing
function getDiscogsData(url) {
$.getJSON(url, // url
function (data, textStatus, jqXHR) { // success callback
// console.log(data);
processDiscogsJSON(data);
});
}
//--------------------------------
// Processing the Discogs JSON
// - Add new items to the masterData array
// - Fill the image grid w/ thumbnails
// - Add the "more" button if >1 page
function processDiscogsJSON(data) {
// If the search came back empty, add the "no results" stuff.
if (data.results.length == 0) {
addNoResultsElements();
} else {
// Offset: pick up numbering from the end of the last data.
let offset = masterData.length;
// If we're adding data on the same search (ie: pagination), then concat the new data to masterData.
if (masterData.length == 0) {
masterData = (data.results);
} else {
masterData = masterData.concat(data.results);
$('#moreBtn').remove();
}
// Iterate over data and load images
for (var i=0;i<data.results.length;i++) {
// make thumb image
let myImg = $('<img />', {
class: 'thumb',
src: data.results[i].thumb,
onload:"$(this).css('visibility', 'visible');$(this).css('opacity', '1');"
});
if (data.results[i].thumb == "") {
myImg.attr('src','no-cover-circle.png');
}
if (i == data.results.length-1) {
myImg.attr('onload', "$(this).css('visibility', 'visible');$(this).css('opacity', '1');$('#moreBtn').fadeToggle(500);");
}
// make thumb link
let myLink = $('<a>',{
id: (i + offset),
href: '#',
click: function() {updateInfoDiv(this.id);return false;},
});
// wrap 'em and add to grid
myImg.wrap(myLink);
$('#imageGrid').append(myImg.parent());
}
//add "More" button if needed
if (data.pagination.page < data.pagination.pages) {
let nextPageURL = data.pagination.urls.next;
let moreBtn = $('<button>', {
id: 'moreBtn',
text: '+',
click: function () { getDiscogsData(nextPageURL);return false; }
});
$('#imageGrid').append(moreBtn);
}
}
}
//--------------------------------
// If the Discogs API search comes back empty, add this stuff
function addNoResultsElements() {
let noResults = $('<p>', {
text: "ZERO RESULTS FOUND",
class: 'noResults'
});
// Add the link for no-year search
let noYearSearch = $('<a>',{
text: 'CLICK TO SEARCH W/O YEAR',
href: '#',
click: function() {executeSearch(false);return false;}
});
noResults.append('<br><br>');
noResults.append(noYearSearch);
$('#imageGrid').append(noResults);
}
//--------------------------------
// Called from the Year +/- buttons
function changeYear(x) {
let newYear = $('#searchYear').val();
if (x) {
newYear++;
} else {
newYear--;
}
$('#searchYear').val(newYear);
executeSearch(true);
}
//------------------------
// Selects a random style
function randomStyle() {
let options = $("#searchStyle > option")
options[Math.floor(Math.random() * options.length)].selected = true
}
//================================
// Individual release window stuff
//================================
//-------------------------
// Global var for searches
var searchSitesObj = [
{
"name": "YouTube",
"baseURL" : "https://www.youtube.com/results?search_query="
},
{
"name": "Spotify",
"baseURL" : "https://open.spotify.com/search/"
},
{
"name": "DuckDuckGo",
"baseURL" : "https://duckduckgo.com/?q=",
"postURL" : "&k1=-1&kae=d"
},
{
"name": "SFPL",
"baseURL" : "https://sfpl.org/search-page?keys=",
"postURL" : "&type=all"
},
{
"name": "Bandcamp",
"baseURL" : "https://bandcamp.com/search?q="
},
{
"name": "Soundcloud",
"baseURL" : "https://soundcloud.com/search?q="
},
{
"name": "Google",
"baseURL" : "https://www.google.com/search?q="
}
];
//----------------------------
// Update release info w/ values from Discogs JSON
function updateInfoDiv(x) {
// Gets the info from the specified index (passed via the thumbnail image's ID)
cRelease = masterData[x];
currentIndex = x;
checkInfoNavState();
$('#infoDiv').empty();
// Album cover
if (cRelease.cover_image.indexOf("spacer.gif") == -1) {
let coverDiv, coverLink, cover;
coverDiv = $('<div>',{
id:"coverDiv"
});
coverLink = $('<a>',{
href: cRelease.cover_image,
target: "_blank"
});
cover = $('<img />', {
class: "cover",
src: cRelease.cover_image
// width: 350
});
coverLink.append(cover);
//$('#infoDiv').append(coverLink);
coverDiv.append(coverLink);
$('#infoDiv').append(coverDiv);
} else {
// No album art
$('#infoDiv').append('<br><br><br>');
}
// Album info
let textDiv = $("<div>", {
id:"textDiv"
});
let title = $('<h2>');
let titleLink = $('<a>',{
text: cRelease.title,
href: ("https://discogs.com" + cRelease.uri),
target: "_blank"
});
title.append(titleLink);
textDiv.append(title);
// See next function for details
let topInfo = $('<p>', {
id: "topInfo"
});
if (cRelease.year != "") topInfo.append(cRelease.year);
if (cRelease.country != "") topInfo.append(", " + cRelease.country);
textDiv.append(topInfo);
// More info link
let moreLink = $('<a>', {
text: "More Info...",
id:"moreInfoLink",
href:"#",
click: function() {$(".moreInfoList").fadeToggle();return(false);}
});
textDiv.append(moreLink);
// Makes the "more info" ul
let info = makeInfoText(cRelease);
textDiv.append(info);
// makes the web search ul
let webSearch = makeWebSearchDiv(cRelease);
textDiv.append(webSearch);
$('#infoDiv').append(textDiv);
if ($('#releaseContainer').is(":visible") == false) {
$('#releaseContainer').fadeToggle(80);
}
}
//---------------------------------
// Array of keys and titles for Discogs value
var discogsInfoObj = [
{
"id": "genre",
"title" : "Genre",
},
{
"id": "style",
"title" : "Style",
},
{
"id": "label",
"title" : "Label",
},
{
"id": "format",
"title" : "Format",
},
{
"id": "catno",
"title" : "Cat No."
},
{
"id": "barcode",
"title" : "Barcode",
},
{
"id": "id",
"title" : "Discogs ID",
}
];
//---------------------------------
// Gets the details for the release, hidden by default
function makeInfoText(x) {
let pInfoUL = $('<ul>', {
class: "moreInfoList"
});
for (var i=0;i<discogsInfoObj.length;i++) {
let cDIO = discogsInfoObj[i];
if (x[cDIO.id] != "") {
// If the value is an array
if (Array.isArray(x[cDIO.id])) {
let liText = "";
for (var j=0;j<x[cDIO.id].length;j++) {
liText += x[cDIO.id][j];
if (j<x[cDIO.id].length-1) {
liText += ", ";
}
}
let li = $("<li>", {
text: cDIO.title + " : " + liText
});
pInfoUL.append(li);
} else {
// single value
let li = $("<li>", {
text: cDIO.title + " : " + x[cDIO.id]
});
pInfoUL.append(li);
}
}
}
return(pInfoUL);
}
//--------------------------
// Returns the search list
function makeWebSearchDiv(x) {
let webSearchDiv = $("<div>", {
class: "webSearchDiv"
});
webSearchDiv.append("<h4>WEB SEARCH:</h4>");
let searchUL = $("<ul>", {
class: "searchUL"
});
for (var i=0;i<searchSitesObj.length;i++) {
let searchLI = $("<li>");
let sLink = makeSearchLinks(x.title, searchSitesObj[i]);
searchLI.append(sLink);
searchUL.append(searchLI);
}
webSearchDiv.append(searchUL);
return(webSearchDiv);
}
//--------------------------
// Returns search link objects
function makeSearchLinks(title, siteObj) {
let searchURL = siteObj.baseURL;
searchURL += titleCleanup(title, siteObj.name);
if (siteObj.postURL != undefined) {
searchURL += siteObj.postURL;
}
let searchLink = $('<a>',{
text: siteObj.name,
href: searchURL,
target: "_blank"
});
return(searchLink);
}
//--------------------------
// Title string cleanup w/ regex
function titleCleanup(title,site) {
let s = title;
s = s.replace(/ *\([^)]*\)*/g, "");
switch(site) {
case("Spotify"):
case("Bandcamp"):
case("Soundcloud"):
case("SFPL"):
s = s.split(" - ").join(' ');
break;
case("YouTube"):
case("DuckDuckGo"):
case("Google"):
s = s.split(" - ").join(' ').split(" ").join('+');
break;
case("Hoopla"):
s = s.split(" - ")[1].split(" ").join('+');
break;
}
return(s)
}
//-----------------------------
// Release window: checks state and hides/shows buttons as needed
function checkInfoNavState() {
let newInfoNavState = 0;
if (parseInt(currentIndex) == 0) {
newInfoNavState = 0; // First release
} else if (parseInt(currentIndex) == masterData.length-1) {
if ($('#moreBtn').is(":visible")) {
newInfoNavState = 2; // Last release with more available
} else {
newInfoNavState = 3; // Last release, no more.
}
} else {
newInfoNavState = 1; // Normal: both arrows, no load.
}
if (currentInfoNavState != newInfoNavState) {
currentInfoNavState = newInfoNavState;
switch(currentInfoNavState) {
case(0):
$('#prevRelease').css("display", "none");
$('#nextRelease').css("display", "block");
$('#nextLoad').css("display", "none");
break;
case(1):
$('#prevRelease').css("display", "block");
$('#nextRelease').css("display", "block");
$('#nextLoad').css("display", "none");
break;
case(2):
$('#prevRelease').css("display", "block");
$('#nextRelease').css("display", "none");
$('#nextLoad').css("display", "block");
break;
case(3):
$('#prevRelease').css("display", "block");
$('#nextRelease').css("display", "none");
$('#nextLoad').css("display", "none");
break;
}
}
}
//----------------------------
// Release window: called from next/prev buttons
function loadNextRelease(x) {
if (x) {
if (currentIndex < masterData.length-1) {
currentIndex++;
updateInfoDiv(currentIndex);
$('#nextRelease').blur();
}
} else {
currentIndex--;
updateInfoDiv(currentIndex);
$('#prevRelease').blur();
}
}
//----------------------------
//Release window: Called from the + button
function loadNextReleasePage() {
$("#moreBtn").trigger("click");
}
//===============================
// 4. UI
//===============================
// KEY COMMANDS
window.onkeyup = function (event) {
// console.log(event.keyCode);
switch(event.keyCode) {
// Escape
case(27):
if ($('#releaseContainer').is(":visible")) {
$('#releaseContainer').fadeToggle(80);
} else if ($('#aboutDiv').is(":visible")) {
$('#aboutDiv').fadeToggle(80);
}
break;
// X execute search
case(88):
if (($('#releaseContainer').is(":visible") == false)
&& ($('#aboutDiv').is(":visible") == false)) {
$('#searchBtn').focus();
$('#searchBtn').trigger("click");
}
break;
// plus key, year increment.
case(107):
case(187):
case(61):
if ($('#releaseContainer').is(":visible") == false) {
$("#yearInc").trigger("click");
}
break;
// minus key, year decrement
case(109):
case(189):
case(173):
if ($('#releaseContainer').is(":visible") == false) {
$("#yearDec").trigger("click");
}
break;
// R arrow
case(39):
if ($('#releaseContainer').is(":visible")) {
if ((currentInfoNavState == 0)
|| (currentInfoNavState == 1)) {
$("#nextRelease").trigger("click");
}
} else {
if (masterData.length > 0) {
console.log(currentIndex);
updateInfoDiv(currentIndex);
}
}
break;
// L arrow
case(37):
if ($('#releaseContainer').is(":visible")) {
if ((currentInfoNavState == 1)
|| (currentInfoNavState == 2)) {
$("#prevRelease").trigger("click");
}
}
break;
// M for More!
case(77):
if ($('#moreBtn').is(":visible")) {
$("#moreBtn").trigger("click");
}
break;
// Return executes search when certain fields are focused
case(13):
let actID = document.activeElement.id;
if (actID == "searchYear" || actID == "searchPageQ" || actID == "searchStyle") {
$('#searchBtn').trigger("click");
}
break;
}
}