2015-06-03 21:45:54 -07:00
|
|
|
import {parse} from 'url';
|
|
|
|
import querystring from 'querystring';
|
|
|
|
import request from 'superagent';
|
|
|
|
import 'superagent-bluebird-promise';
|
2014-12-04 21:11:55 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
module.exports.id = 'itunes';
|
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
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
module.exports.parseUrl = function* (url) {
|
|
|
|
let parsed = parse(url);
|
|
|
|
let matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
|
|
|
let query = querystring.parse(parsed.query);
|
2014-12-11 19:53:12 +00:00
|
|
|
|
|
|
|
if (matches) {
|
2015-06-03 21:45:54 -07:00
|
|
|
let type = 'album';
|
|
|
|
let id = matches[3].substr(2);
|
2014-12-11 19:53:12 +00:00
|
|
|
if (query.i) {
|
2015-06-03 21:45:54 -07:00
|
|
|
type = 'track';
|
2014-12-11 19:53:12 +00:00
|
|
|
id = query.i;
|
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
return yield module.exports.lookupId(id, type, matches[1] || 'us');
|
2014-12-11 19:53:12 +00:00
|
|
|
} else {
|
2015-06-03 21:45:54 -07:00
|
|
|
return Promise.reject(new Error());
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
2014-12-04 21:11:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
module.exports.lookupId = function* (id, type, cc) {
|
2014-12-13 12:12:44 +00:00
|
|
|
if (String(id).match(/^[a-z]{2}/)) {
|
2015-06-03 21:45:54 -07:00
|
|
|
cc = id.substr(0, 2);
|
2014-12-06 21:12:52 +00:00
|
|
|
id = id.substr(2);
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
let path = '/lookup?id=' + id;
|
2014-12-06 21:12:52 +00:00
|
|
|
if (cc) {
|
2015-06-03 21:45:54 -07:00
|
|
|
path = '/' + cc + path;
|
2014-12-06 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
let response = yield request.get(apiRoot + path);
|
|
|
|
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) {
|
|
|
|
let error = new Error('Not Found');
|
|
|
|
error.status = 404;
|
|
|
|
return Promise.reject(error);
|
|
|
|
} else {
|
|
|
|
result = result.results[0];
|
|
|
|
|
|
|
|
let item = {
|
|
|
|
service: 'itunes',
|
|
|
|
type: type,
|
|
|
|
id: cc + id,
|
|
|
|
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
|
2014-12-04 21:53:50 +00:00
|
|
|
}
|
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 = {
|
|
|
|
name: result.collectionName
|
|
|
|
};
|
2014-12-04 21:53:50 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
|
|
|
|
return Promise.resolve(item);
|
|
|
|
}
|
2014-12-11 19:53:12 +00:00
|
|
|
};
|
2014-12-04 21:11:55 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
module.exports.search = function* (data) {
|
|
|
|
let query, album, entity;
|
|
|
|
let type = data.type;
|
2014-12-04 21:11:55 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
if (type === 'album') {
|
|
|
|
query = data.artist.name + ' ' + data.name;
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.name;
|
2015-06-03 21:45:54 -07:00
|
|
|
entity = 'album';
|
|
|
|
} else if (type === 'track') {
|
|
|
|
query = data.artist.name + ' ' + data.album.name + ' ' + data.name;
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.album.name;
|
2015-06-03 21:45:54 -07:00
|
|
|
entity = 'musicTrack';
|
2014-12-04 21:11:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
let path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
|
|
|
|
let response = yield request.get(apiRoot + path);
|
|
|
|
let result = JSON.parse(response.text);
|
|
|
|
|
|
|
|
if (!result.results[0]) {
|
|
|
|
let matches = album.match(/^[^\(\[]+/);
|
|
|
|
if (matches && matches[0] && matches[0] !== album) {
|
|
|
|
let cleanedData = JSON.parse(JSON.stringify(data));
|
|
|
|
if (type === 'album') {
|
|
|
|
cleanedData.name = matches[0].trim();
|
|
|
|
} else if (type === 'track') {
|
|
|
|
cleanedData.album.name = matches[0].trim();
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
return yield module.exports.search(cleanedData);
|
2014-12-04 21:11:55 +00:00
|
|
|
} else {
|
2015-06-03 21:45:54 -07:00
|
|
|
return Promise.resolve({service: 'itunes'});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = result.results[0];
|
|
|
|
|
|
|
|
let 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
|
2014-12-04 21:53:50 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type === 'track') {
|
|
|
|
item.album = {
|
|
|
|
name: result.collectionName
|
|
|
|
};
|
2014-12-04 21:11:55 +00:00
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
return Promise.resolve(item);
|
|
|
|
}
|
2014-12-11 19:53:12 +00:00
|
|
|
};
|