Prototyping Musicbrainz for supplementary artist images

This commit is contained in:
James Barnsley
2019-09-02 16:55:02 +12:00
parent 87d49156c7
commit 6a7391d042
8 changed files with 256 additions and 19 deletions

View File

@ -1,9 +1,9 @@
var coreActions = require('../core/actions');
var musicbrainzActions = require('../musicbrainz/actions');
var uiActions = require('../ui/actions');
var helpers = require('../../helpers');
export function set(data){
return {
type: 'LASTFM_SET',
@ -262,6 +262,7 @@ export function getArtist(uri, artist, mbid = false){
};
dispatch(coreActions.artistLoaded(artist));
dispatch(musicbrainzActions.getArtist(uri, artist));
}
},
error => {

View File

@ -12,6 +12,7 @@ var spotifyActions = require('../spotify/actions.js');
var pusherActions = require('../pusher/actions.js');
var googleActions = require('../google/actions.js');
var lastfmActions = require('../lastfm/actions.js');
var musicbrainzActions = require('../musicbrainz/actions.js');
const MopidyMiddleware = (function(){
@ -2149,15 +2150,18 @@ const MopidyMiddleware = (function(){
store.dispatch(coreActions.artistLoaded(artist));
// Load supprting information from LastFM
// Note: This excludes artwork as this was removed from their API
// in May 2019
// Load supprting information from LastFM and Musicbrainz
var existing_artist = store.getState().core.artists[artist.uri];
if (existing_artist && !existing_artist.biography){
if (artist.musicbrainz_id){
store.dispatch(lastfmActions.getArtist(artist.uri, false, artist.musicbrainz_id));
} else {
store.dispatch(lastfmActions.getArtist(artist.uri, artist.name));
if (existing_artist) {
// Get biography and other stats from LastFM
if (!existing_artist.biography){
// TODO: Move this condition into lastfmActions
if (artist.musicbrainz_id){
store.dispatch(lastfmActions.getArtist(artist.uri, false, artist.musicbrainz_id));
} else {
store.dispatch(lastfmActions.getArtist(artist.uri, artist.name));
}
}
}
})

View File

@ -0,0 +1,96 @@
var coreActions = require('../core/actions');
var uiActions = require('../ui/actions');
var helpers = require('../../helpers');
/**
* Send an ajax request to the LastFM API
*
* @param dispatch = obj
* @param endpoint = String
* @param params = String
**/
var sendRequest = (dispatch, endpoint, params) => {
return new Promise((resolve, reject) => {
const loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, ',musicbrainz_'+endpoint));
var config = {
method: 'GET',
cache: true,
timeout: 30000,
url: 'https://musicbrainz.org/ws/2/'+endpoint+'?fmt=json&'+params
}
$.ajax(config).then(
response => {
dispatch(uiActions.stopLoading(loader_key));
if (response.error){
reject({
config: config,
error: response
});
} else {
resolve(response);
}
},
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
// Snatch a more meaningful error
var description = null;
if (xhr && xhr.responseJSON && xhr.responseJSON.message){
description = xhr.responseJSON.message;
}
reject({
config: config,
error: error,
description: description,
status: status,
xhr: xhr
});
}
)
})
}
export function findArtist(uri, artist){
return (dispatch, getState) => {
sendRequest(dispatch, 'artist', 'query=artist:'+artist.name)
.then(
response => {
console.log(response);
},
error => {
console.info("Musicbrainz: No results for artist '"+artist.name+"'");
}
)
}
}
export function getArtist(uri, artist){
return (dispatch, getState) => {
sendRequest(dispatch, 'artist/'+artist.mbid, 'inc=url-rels')
.then(
response => {
if (response){
const image_relations = response.relations.filter(rel => (rel['target-type'] === 'image'));
const images = image_relations.map(ir => ir.url.resource);
var artist = {
uri: uri,
images: images,
};
console.log(response);
console.log(image_relations);
dispatch(coreActions.artistLoaded(artist));
}
},
error => {
console.info("Musicbrainz: No results for artist '"+artist.uri+"'");
}
)
}
}

View File

@ -0,0 +1,15 @@
var helpers = require('./../../helpers');
var musicbrainzActions = require('./actions');
const MusicbrainzMiddleware = (function(){
return store => next => action => {
switch(action.type){
default:
return next(action);
}
}
})();
export default MusicbrainzMiddleware

View File

@ -0,0 +1,7 @@
export default function reducer(musicbrainz = {}, action){
switch (action.type){
default:
return lastfm
}
}