Search additional markets for itunes and spotify
This commit is contained in:
parent
00ca82c851
commit
147ad4f3d2
4 changed files with 95 additions and 57 deletions
|
@ -76,6 +76,7 @@ export function* lookupId(id, type, cc) {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function* search(data) {
|
export function* search(data) {
|
||||||
|
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
|
||||||
let query, album, entity;
|
let query, album, entity;
|
||||||
const type = data.type;
|
const type = data.type;
|
||||||
|
|
||||||
|
@ -89,47 +90,48 @@ export function* search(data) {
|
||||||
entity = 'musicTrack';
|
entity = 'musicTrack';
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
|
for (let market of markets) {
|
||||||
const response = yield request.get(apiRoot + path);
|
const path = '/' + market + '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
|
||||||
let result = JSON.parse(response.text);
|
const response = yield request.get(apiRoot + path);
|
||||||
|
|
||||||
if (!result.results[0]) {
|
let result = JSON.parse(response.text);
|
||||||
const matches = album.match(/^[^\(\[]+/);
|
if (!result.results[0]) {
|
||||||
if (matches && matches[0] && matches[0] !== album) {
|
const matches = album.match(/^[^\(\[]+/);
|
||||||
const cleanedData = JSON.parse(JSON.stringify(data));
|
if (matches && matches[0] && matches[0] !== album) {
|
||||||
if (type === 'album') {
|
const cleanedData = JSON.parse(JSON.stringify(data));
|
||||||
cleanedData.name = matches[0].trim();
|
if (type === 'album') {
|
||||||
} else if (type === 'track') {
|
cleanedData.name = matches[0].trim();
|
||||||
cleanedData.albumName = matches[0].trim();
|
} else if (type === 'track') {
|
||||||
|
cleanedData.albumName = matches[0].trim();
|
||||||
|
}
|
||||||
|
return yield search(cleanedData);
|
||||||
}
|
}
|
||||||
return yield search(cleanedData);
|
|
||||||
} else {
|
} else {
|
||||||
return {service: 'itunes'};
|
result = result.results[0];
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = result.results[0];
|
|
||||||
|
|
||||||
const item = {
|
const item = {
|
||||||
service: 'itunes',
|
service: 'itunes',
|
||||||
type: type,
|
type: type,
|
||||||
id: 'us' + result.collectionId,
|
id: 'us' + result.collectionId,
|
||||||
name: result.trackName ? result.trackName : result.collectionName,
|
name: result.trackName ? result.trackName : result.collectionName,
|
||||||
streamUrl: null,
|
streamUrl: null,
|
||||||
purchaseUrl: result.collectionViewUrl,
|
purchaseUrl: result.collectionViewUrl,
|
||||||
artwork: {
|
artwork: {
|
||||||
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
|
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
|
||||||
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
|
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
|
||||||
},
|
},
|
||||||
artist: {
|
artist: {
|
||||||
name: result.artistName
|
name: result.artistName
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if (type === 'track') {
|
|
||||||
item.album = {
|
|
||||||
name: result.collectionName
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (type === 'track') {
|
||||||
|
item.album = {
|
||||||
|
name: result.collectionName
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
return {service: 'itunes'};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { parse } from 'url';
|
import { parse } from 'url';
|
||||||
import bluebird from 'bluebird';
|
import bluebird from 'bluebird';
|
||||||
import spotifyCB from 'spotify';
|
import spotifyCB from 'spotify';
|
||||||
|
import request from 'superagent';
|
||||||
|
import 'superagent-bluebird-promise';
|
||||||
const spotify = bluebird.promisifyAll(spotifyCB);
|
const spotify = bluebird.promisifyAll(spotifyCB);
|
||||||
import { match as urlMatch } from './url';
|
import { match as urlMatch } from './url';
|
||||||
|
|
||||||
|
@ -64,8 +66,9 @@ export function* lookupId(id, type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* search(data) {
|
export function* search(data, original={}) {
|
||||||
var cleanParam = function(str) {
|
const markets = ['US', 'GB', 'JP', 'BR', 'DE', 'ES'];
|
||||||
|
const cleanParam = function(str) {
|
||||||
var chopChars = ['&', '[', '('];
|
var chopChars = ['&', '[', '('];
|
||||||
chopChars.forEach(function(chr) {
|
chopChars.forEach(function(chr) {
|
||||||
if (data.artist.name.indexOf('&') > 0) {
|
if (data.artist.name.indexOf('&') > 0) {
|
||||||
|
@ -74,8 +77,8 @@ export function* search(data) {
|
||||||
})
|
})
|
||||||
return str.replace(/[\:\?]+/, "");
|
return str.replace(/[\:\?]+/, "");
|
||||||
}
|
}
|
||||||
var query, album;
|
let query, album;
|
||||||
var type = data.type;
|
const type = data.type;
|
||||||
|
|
||||||
if (type == "album") {
|
if (type == "album") {
|
||||||
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
|
query = "artist:" + cleanParam(data.artist.name) + " album:" + cleanParam(data.name);
|
||||||
|
@ -85,27 +88,50 @@ export function* search(data) {
|
||||||
album = data.albumName;
|
album = data.albumName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = yield spotify.searchAsync({query: query, type: type});
|
for (let market of markets) {
|
||||||
|
const response = yield request.get('https://api.spotify.com/v1/search?type=' + type + '&q=' + encodeURI(query) + '&market=' + market);
|
||||||
|
const items = response.body[type + 's'].items;
|
||||||
|
|
||||||
if (!results[type + "s"].items[0]) {
|
const name = original.name || data.name;
|
||||||
return {service: "spotify"};
|
|
||||||
} else {
|
let match;
|
||||||
let found;
|
if (!(match = exactMatch(name, items, type))) {
|
||||||
const choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.albumName);
|
match = looseMatch(name, items, type);
|
||||||
if (!choppedAlbum.length) {
|
|
||||||
return yield lookupId(results[type + "s"].items[0].id, type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
results[type + "s"].items.forEach(function(item) {
|
if (match) {
|
||||||
const albumName = data.type == "album" ? item.name : item.album.name;
|
if (type === 'album') {
|
||||||
const matches = cleanParam(albumName).match(/^[^\(\[]+/);
|
return yield lookupId(match.id, type);
|
||||||
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
} else if (type === 'track') {
|
||||||
found = item;
|
return yield lookupId(match.id, type);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
if (!found) {
|
|
||||||
return {service: "spotify"};
|
|
||||||
}
|
}
|
||||||
return yield lookupId(results[type + "s"].items[0].id, type);
|
|
||||||
}
|
}
|
||||||
|
return {service: "spotify"};
|
||||||
|
}
|
||||||
|
|
||||||
|
function exactMatch(needle, haystack, type) {
|
||||||
|
// try to find exact match
|
||||||
|
return haystack.find(function(entry) {
|
||||||
|
if (entry.type !== type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.name === needle) {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function looseMatch(needle, haystack, type) {
|
||||||
|
// try to find exact match
|
||||||
|
return haystack.find(function(entry) {
|
||||||
|
if (entry.type !== type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.name.indexOf(needle) >= 0) {
|
||||||
|
return entry
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,11 @@ describe('iTunes Music', function(){
|
||||||
result.name.should.equal('White Pony');
|
result.name.should.equal('White Pony');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should find awkward album by search', function* (){
|
||||||
|
const result = yield itunes.search({type: 'album', artist: {name: 'Anavitória'}, name: 'Fica'});
|
||||||
|
result.name.should.equal('Fica (feat. Matheus & Kauan) - Single');
|
||||||
|
});
|
||||||
|
|
||||||
it('should find track by search', function* (){
|
it('should find track by search', function* (){
|
||||||
const result = yield itunes.search({type: 'track', artist: {name: 'Deftones'}, albumName: 'Deftones', name: 'Hexagram'});
|
const result = yield itunes.search({type: 'track', artist: {name: 'Deftones'}, albumName: 'Deftones', name: 'Hexagram'});
|
||||||
result.name.should.equal('Hexagram');
|
result.name.should.equal('Hexagram');
|
||||||
|
|
|
@ -20,6 +20,11 @@ describe('Spotify', function(){
|
||||||
result.name.should.equal('Listen (Deluxe)');
|
result.name.should.equal('Listen (Deluxe)');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should find br album by search', function* (){
|
||||||
|
const result = yield spotify.search({type: 'album', artist: {name: 'Anavitória'}, name: 'Fica'});
|
||||||
|
result.name.should.equal('Fica');
|
||||||
|
});
|
||||||
|
|
||||||
it('should find album by various artists by search', function* (){
|
it('should find album by various artists by search', function* (){
|
||||||
const result = yield spotify.search({type: 'album', artist: {name: 'Various Artists'}, name: 'The Get Down Part II: Original Soundtrack From The Netflix Original Series'});
|
const result = yield spotify.search({type: 'album', artist: {name: 'Various Artists'}, name: 'The Get Down Part II: Original Soundtrack From The Netflix Original Series'});
|
||||||
result.name.should.equal('The Get Down Part II: Original Soundtrack From The Netflix Original Series');
|
result.name.should.equal('The Get Down Part II: Original Soundtrack From The Netflix Original Series');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue