Search additional markets for itunes and spotify

This commit is contained in:
Jonathan Cremin 2017-05-07 22:16:43 +01:00
parent 00ca82c851
commit 147ad4f3d2
4 changed files with 95 additions and 57 deletions

View file

@ -76,6 +76,7 @@ export function* lookupId(id, type, cc) {
};
export function* search(data) {
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
let query, album, entity;
const type = data.type;
@ -89,47 +90,48 @@ export function* search(data) {
entity = 'musicTrack';
}
const path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
const response = yield request.get(apiRoot + path);
let result = JSON.parse(response.text);
for (let market of markets) {
const path = '/' + market + '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
const response = yield request.get(apiRoot + path);
if (!result.results[0]) {
const matches = album.match(/^[^\(\[]+/);
if (matches && matches[0] && matches[0] !== album) {
const cleanedData = JSON.parse(JSON.stringify(data));
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.albumName = matches[0].trim();
let result = JSON.parse(response.text);
if (!result.results[0]) {
const matches = album.match(/^[^\(\[]+/);
if (matches && matches[0] && matches[0] !== album) {
const cleanedData = JSON.parse(JSON.stringify(data));
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.albumName = matches[0].trim();
}
return yield search(cleanedData);
}
return yield search(cleanedData);
} else {
return {service: 'itunes'};
}
} else {
result = result.results[0];
result = result.results[0];
const item = {
service: 'itunes',
type: type,
id: 'us' + result.collectionId,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
},
artist: {
name: result.artistName
}
};
if (type === 'track') {
item.album = {
name: result.collectionName
const item = {
service: 'itunes',
type: type,
id: 'us' + result.collectionId,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
},
artist: {
name: result.artistName
}
};
if (type === 'track') {
item.album = {
name: result.collectionName
};
}
return item;
}
return item;
}
return {service: 'itunes'};
};