Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch image from Wikimedia via MusicBrainz #49

Merged
merged 8 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"esversion": 6
"esversion": 6,
"curly": false
}
155 changes: 115 additions & 40 deletions lib/sonice/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,163 @@

// Return a random element from an Array
// [3, 9, 8].random() # => 5
Array.prototype.random = function() {
Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)]
}
};

/* jQuery helpers */

// Change CSS background image
$.fn.background = function (bg) {
return $(this).css('backgroundImage', bg ? `url('${bg}')` : 'none');
};

// Assigns keyboard keys to elements and adds them to the title attributes
// Needs the data-keyboard-key attribute on elements and optionally accepts
// the data-keyboard-name attribute
$.fn.keyboardShortcut = function() {
return $(this).each(function() {
$.fn.keyboardShortcut = function () {
return $(this).each(function () {
var button = $(this),
character = $(this).data('key'),
title = $(this).data('key-name') || character
button.attr('title', button.attr('title') + ' ('+title+')')
$(document).keypress(function(e) {
character = $(this).data('key'),
title = $(this).data('key-name') || character;
button.attr('title', button.attr('title') + ' (' + title + ')');
$(document).keypress(function (e) {
if (String.fromCharCode(e.charCode) == character)
button.click()
})
})
button.click();
});
});
}

/* So nice helpers */

// Get a new artist image from Last.fm via jsonp
// When found calls the `callback` with the image url as the first argument
function artistImage(artist, callback) {
if (!artist) return callback();
sunny marked this conversation as resolved.
Show resolved Hide resolved
cache = artistImage.cache;
artist = encodeURI(artist);
const getImageFromCache = () => callback(cache[artist].random());

// Deliver from cache
if (cache.hasOwnProperty(artist)) {
// execute the callback asynchronously to minimize codepaths
dmcneary marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(getImageFromCache, 10);
return;
}

// Load
const lastFmUri = 'http://ws.audioscrobbler.com/2.0/?format=json&method=artist.getinfo&artist=%s&api_key=5636ca9fea36d0323a76638385aab1f3';
$.ajax({
url: lastFmUri.replace('%s', artist),
dataType: 'jsonp',
success: response => {
// thanks to @hugovk!
// (https://github.com/hugovk/now-playing-radiator)
const mbid = response.artist.mbid;
sunny marked this conversation as resolved.
Show resolved Hide resolved
if (!mbid) return;
const url =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

'https://musicbrainz.org/ws/2/artist/' +
mbid +
'?inc=url-rels&fmt=json';
fetch(url)
.then(res => res.json())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

.then(data => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

const relations = data.relations;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

const commonsStub =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

'https://commons.wikimedia.org/wiki/File:';
const fileStub =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

'https://commons.wikimedia.org/wiki/Special:Redirect/file/';
const imagesArr = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

for (let i = 0; i < relations.length; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

if (relations[i].type === 'image') {
let imageUrl = relations[i].url.resource;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

if (imageUrl.startsWith(commonsStub)) {
const filename =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
imageUrl = fileStub + filename;
}
imagesArr.push(imageUrl);
}
}
cache[artist] = imagesArr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'cache' is not defined.

getImageFromCache();
});
}
});
}

$(function() {
artistImage.cache = {};

$(function () {
// Object that contains all the song info
var currentSong = {}
var currentSong = {};

// Update the HTML based on the currentSong object
function updateInformation(obj) {
var artistChange = currentSong.artist != obj.artist,
songChange = currentSong.title != obj.title
currentSong = obj = obj || {}
songChange = currentSong.title != obj.title;
currentSong = obj = obj || {};

var artist = obj.artist || '',
album = obj.album || '',
title = obj.title || '',
connected = !!obj.connected
album = obj.album || '',
title = obj.title || '',
connected = !!obj.connected;

$('#title' ).text(title)
$('#artist').text(artist)
$('#album' ).text(album)
$('[data-sonice-show-if-connected]')[connected ? 'show' : 'hide']()
$('[data-sonice-hide-if-connected]')[connected ? 'hide' : 'show']()
$('#title').text(title);
$('#artist').text(artist);
$('#album').text(album);
$('[data-sonice-show-if-connected]')[connected ? 'show' : 'hide']();
$('[data-sonice-hide-if-connected]')[connected ? 'hide' : 'show']();

if (!title)
$('title').text('So nice')
$('title').text('So nice');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected '{' and instead saw '$'.

else
$('title').text(artist + (artist && title ? ' — ' : '') + title)
$('title').text(artist + (artist && title ? ' — ' : '') + title);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected '{' and instead saw '$'.


if (artistChange || songChange)
$('#vote').removeAttr('disabled').show()
$('#vote').removeAttr('disabled').show();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected '{' and instead saw '$'.


if (artistChange) changeBackground();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected '{' and instead saw 'changeBackground'.

}

// Change background on the body regularly
var changeBackground = function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'changeBackground' was used before it was defined.

artistImage(currentSong.artist, function (url) {
$('body').background(url);
});
}

// XHR updating the text regularly
var update = function() {
var update = function () {
$.ajax({
dataType: 'json',
success: updateInformation,
error: updateInformation
})
error: updateInformation
});
}

// XHR overriding the buttons
$(document).on('click', 'input', function(e) {
e.preventDefault()
$(document).on('click', 'input', function (e) {
e.preventDefault();

$.ajax({
type: 'put',
url: '/player',
data: this.name+'='+encodeURI(this.value),
data: this.name + '=' + encodeURI(this.value),
complete: update
})
})
});
});

// Vote button
$(document).on('click', '#vote', function() {
$(this).attr('disabled', true).fadeOut(500)
})
$(document).on('click', '#vote', function () {
$(this).attr('disabled', true).fadeOut(500);
});

update()
setInterval(update, 500)
update();
setInterval(update, 500);
setInterval(changeBackground, 10000);

// Keyboard shortcuts
$('[data-key]').keyboardShortcut()
$('[data-key]').keyboardShortcut();

})