2019-09-02 16:55:02 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
const coreActions = require('../core/actions');
|
|
|
|
|
const uiActions = require('../ui/actions');
|
|
|
|
|
const helpers = require('../../helpers');
|
2019-09-02 16:55:02 +12:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send an ajax request to the LastFM API
|
|
|
|
|
*
|
|
|
|
|
* @param dispatch = obj
|
|
|
|
|
* @param endpoint = String
|
|
|
|
|
* @param params = String
|
2019-09-05 15:36:11 +12:00
|
|
|
* */
|
2019-09-10 06:59:44 +12:00
|
|
|
const sendRequest = (dispatch, getState, endpoint, params) => new Promise((resolve, reject) => {
|
2019-09-05 15:18:16 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
const key = 'CXIwsVMAjrXIVitBWgqd';
|
|
|
|
|
const secret = 'KiEUfwKpebxRnEHlKoXnYIftJxeuqjTK';
|
2019-09-10 06:59:44 +12:00
|
|
|
const url = `https://api.discogs.com/${endpoint}?${params}`;
|
|
|
|
|
|
|
|
|
|
const loader_key = helpers.generateGuid();
|
|
|
|
|
dispatch(uiActions.startLoading(loader_key, `discogs_${endpoint}`));
|
|
|
|
|
|
|
|
|
|
const checkRateLimit = (request) => {
|
|
|
|
|
const rate = {
|
|
|
|
|
limit: request.getResponseHeader('X-Discogs-Ratelimit'),
|
|
|
|
|
remaining: request.getResponseHeader('X-Discogs-Ratelimit-Remaining'),
|
|
|
|
|
used: request.getResponseHeader('X-Discogs-Ratelimit-Used'),
|
|
|
|
|
}
|
|
|
|
|
if (rate.remaining !== undefined && rate.remaining === 0) {
|
|
|
|
|
dispatch(uiActions.createNotification({
|
|
|
|
|
key: "discogs_rate_limit",
|
|
|
|
|
type: "bad",
|
|
|
|
|
content: "Rate limit exceeded",
|
|
|
|
|
description: `Discogs rate limit exceeded, try again in a few minutes.`
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-02 16:55:02 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
const config = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
cache: true,
|
|
|
|
|
timeout: 30000,
|
2019-09-10 06:59:44 +12:00
|
|
|
url: url,
|
|
|
|
|
crossDomain: true,
|
2019-09-05 15:36:11 +12:00
|
|
|
headers: {
|
2019-09-10 06:59:44 +12:00
|
|
|
'User-Agent': 'Iris/1.0',
|
|
|
|
|
'Authorization': `Discogs key=${key}, secret=${secret}`
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
};
|
2019-09-02 16:55:02 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
$.ajax(config).then(
|
2019-09-10 06:59:44 +12:00
|
|
|
(data, textStatus, request) => {
|
2019-09-05 15:36:11 +12:00
|
|
|
dispatch(uiActions.stopLoading(loader_key));
|
2019-09-10 06:59:44 +12:00
|
|
|
checkRateLimit(request);
|
|
|
|
|
resolve(data);
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
(xhr, status, error) => {
|
|
|
|
|
dispatch(uiActions.stopLoading(loader_key));
|
2019-09-10 06:59:44 +12:00
|
|
|
checkRateLimit(xhr);
|
2019-09-02 16:55:02 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
reject({
|
|
|
|
|
config,
|
|
|
|
|
error,
|
|
|
|
|
status,
|
|
|
|
|
xhr,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-09-02 16:55:02 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
export function getArtistImages(uri, artist) {
|
|
|
|
|
return (dispatch, getState) => {
|
2019-09-10 06:59:44 +12:00
|
|
|
sendRequest(dispatch, getState, 'database/search', `type=artist&query=${artist.name}`)
|
2019-09-05 15:36:11 +12:00
|
|
|
.then(
|
|
|
|
|
(response) => {
|
|
|
|
|
if (response) {
|
2019-09-10 06:59:44 +12:00
|
|
|
if (response.results.length > 0 && response.results[0].cover_image !== undefined) {
|
2019-09-05 15:36:11 +12:00
|
|
|
const updated_artist = {
|
|
|
|
|
uri,
|
|
|
|
|
images: [response.results[0].cover_image],
|
|
|
|
|
};
|
|
|
|
|
dispatch(coreActions.artistLoaded(updated_artist));
|
2019-09-10 06:59:44 +12:00
|
|
|
} else {
|
|
|
|
|
console.log(`Discogs: No results for artist ${artist.name}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
const finalise = (cover_image, cache) => {
|
|
|
|
|
const updated_artist = {
|
|
|
|
|
uri,
|
|
|
|
|
images: [cover_image],
|
|
|
|
|
};
|
|
|
|
|
dispatch(coreActions.artistLoaded(updated_artist));
|
|
|
|
|
|
|
|
|
|
if (cache) {
|
|
|
|
|
helpers.cache.set(`discogs_${artist.name}`, cover_image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cached = helpers.cache.get(`discogs_${artist.name}`);
|
|
|
|
|
if (cached) {
|
|
|
|
|
finalise(cached);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendRequest(dispatch, getState, 'database/search', `type=artist&query=${artist.name}`)
|
|
|
|
|
.then(
|
|
|
|
|
(response) => {
|
|
|
|
|
if (response) {
|
|
|
|
|
if (response.results.length > 0 && response.results[0].cover_image !== undefined) {
|
|
|
|
|
finalise(response.results[0].cover_image, true);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Discogs: No results for artist ${artist.name}`);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
2019-09-10 06:59:44 +12:00
|
|
|
console.error(error);
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
);
|
2019-09-10 06:59:44 +12:00
|
|
|
*/
|
2019-09-05 15:36:11 +12:00
|
|
|
};
|
2019-09-02 16:55:02 +12:00
|
|
|
}
|