combine.fm/lib/services/itunes/index.js

140 lines
4 KiB
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
2015-06-03 21:45:54 -07:00
import querystring from 'querystring';
import request from 'superagent';
import 'superagent-bluebird-promise';
2017-07-20 14:31:07 +01:00
import urlMatch from './url';
2014-12-04 21:11:55 +00:00
2015-06-03 21:45:54 -07:00
const apiRoot = 'https://itunes.apple.com';
2014-12-04 21:11:55 +00:00
2015-08-20 23:22:57 +01:00
export function* parseUrl(url) {
const parsed = parse(url);
2017-07-20 14:31:07 +01:00
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
2015-08-20 23:22:57 +01:00
const query = querystring.parse(parsed.query);
if (matches) {
2015-06-03 21:45:54 -07:00
let type = 'album';
let id = matches[3].substr(2);
if (query.i) {
2015-06-03 21:45:54 -07:00
type = 'track';
id = query.i;
}
2015-06-03 21:45:54 -07:00
return yield module.exports.lookupId(id, type, matches[1] || 'us');
}
2017-07-20 14:31:07 +01:00
throw new Error();
}
export function* lookupId(possibleId, type, countrycode) {
let cc = countrycode;
let id = possibleId;
if (String(possibleId).match(/^[a-z]{2}/)) {
cc = possibleId.substr(0, 2);
id = possibleId.substr(2);
2014-12-06 21:12:52 +00:00
}
2017-07-20 14:31:07 +01:00
let path = `/lookup?id=${id}`;
2014-12-06 21:12:52 +00:00
if (cc) {
2017-07-20 14:31:07 +01:00
path = `/${cc}${path}`;
2014-12-06 21:12:52 +00:00
}
2015-08-20 23:22:57 +01:00
const response = yield request.get(apiRoot + path);
2015-06-03 21:45:54 -07:00
let result = JSON.parse(response.text);
2014-12-04 21:53:50 +00:00
2015-06-03 21:45:54 -07:00
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
2015-08-20 23:22:57 +01:00
const error = new Error('Not Found');
2015-06-03 21:45:54 -07:00
error.status = 404;
2015-08-20 23:22:57 +01:00
throw error;
2015-06-03 21:45:54 -07:00
} else {
result = result.results[0];
2017-07-20 14:31:07 +01:00
const item = {
2015-06-03 21:45:54 -07:00
service: 'itunes',
2017-07-20 14:31:07 +01:00
type,
2015-06-03 21:45:54 -07:00
id: cc + id,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
2017-10-23 17:58:23 +01:00
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '-ssl.mzstatic.com').replace('http://', 'https://')}`,
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '-ssl.mzstatic.com').replace('http://', 'https://')}`,
2015-06-03 21:45:54 -07:00
},
artist: {
2017-07-20 14:31:07 +01:00
name: result.artistName,
},
2015-06-03 21:45:54 -07:00
};
2014-12-04 21:53:50 +00:00
2015-06-03 21:45:54 -07:00
if (type === 'track') {
item.album = {
2017-07-20 14:31:07 +01:00
name: result.collectionName,
2015-06-03 21:45:54 -07:00
};
2014-12-04 21:53:50 +00:00
}
2015-06-03 21:45:54 -07:00
2015-08-20 23:22:57 +01:00
return item;
2015-06-03 21:45:54 -07:00
}
2017-07-20 14:31:07 +01:00
}
2014-12-04 21:11:55 +00:00
2015-08-20 23:22:57 +01:00
export function* search(data) {
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
2017-07-20 14:31:07 +01:00
let query;
let album;
let entity;
2015-08-20 23:22:57 +01:00
const type = data.type;
2014-12-04 21:11:55 +00:00
2015-06-03 21:45:54 -07:00
if (type === 'album') {
2017-07-20 14:31:07 +01:00
query = `${data.artist.name} ${data.name}`;
album = data.name;
2015-06-03 21:45:54 -07:00
entity = 'album';
} else if (type === 'track') {
2017-07-20 14:31:07 +01:00
query = `${data.artist.name} ${data.albumName} ${data.name}`;
album = data.albumName;
2015-06-03 21:45:54 -07:00
entity = 'musicTrack';
2014-12-04 21:11:55 +00:00
}
2017-07-20 14:31:07 +01:00
for (const market of markets) { // eslint-disable-line
const path = `/${market}/search?term=${encodeURIComponent(query)}&media=music&entity=${entity}`;
const response = yield request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results[0]) {
2017-07-20 14:31:07 +01:00
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);
}
2014-12-04 21:11:55 +00:00
} else {
result = result.results[0];
const item = {
service: 'itunes',
2017-07-20 14:31:07 +01:00
type,
id: `us${result.collectionId}`,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
2017-10-23 17:58:23 +01:00
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '-ssl.mzstatic.com').replace('http://', 'https://')}`,
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '-ssl.mzstatic.com').replace('http://', 'https://')}`,
},
artist: {
2017-07-20 14:31:07 +01:00
name: result.artistName,
},
};
2015-06-03 21:45:54 -07:00
if (type === 'track') {
item.album = {
2017-07-20 14:31:07 +01:00
name: result.collectionName,
};
2014-12-04 21:53:50 +00:00
}
return item;
2014-12-04 21:11:55 +00:00
}
2015-06-03 21:45:54 -07:00
}
2017-07-20 14:31:07 +01:00
return { service: 'itunes' };
}
export const id = 'itunes';
export const match = urlMatch;