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

136 lines
3.6 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';
2015-08-20 23:22:57 +01:00
import { match as urlMatch } from './url';
2014-12-04 21:11:55 +00:00
2015-08-20 23:22:57 +01:00
export let 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
2015-08-20 23:22:57 +01:00
export const match = urlMatch;
2015-08-20 23:22:57 +01:00
export function* parseUrl(url) {
const parsed = parse(url);
const matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
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');
} else {
2015-08-20 23:22:57 +01:00
throw new Error();
}
2014-12-04 21:11:55 +00:00
};
2015-08-20 23:22:57 +01:00
export function* lookupId(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-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];
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
2015-08-20 23:22:57 +01:00
return item;
2015-06-03 21:45:54 -07:00
}
};
2014-12-04 21:11:55 +00:00
2015-08-20 23:22:57 +01:00
export function* search(data) {
2015-06-03 21:45:54 -07:00
let query, album, 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') {
query = data.artist.name + ' ' + data.name;
album = data.name;
2015-06-03 21:45:54 -07:00
entity = 'album';
} else if (type === 'track') {
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
}
2015-08-20 23:22:57 +01:00
const path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
const response = yield request.get(apiRoot + path);
2015-06-03 21:45:54 -07:00
let result = JSON.parse(response.text);
if (!result.results[0]) {
2015-08-20 23:22:57 +01:00
const matches = album.match(/^[^\(\[]+/);
2015-06-03 21:45:54 -07:00
if (matches && matches[0] && matches[0] !== album) {
2015-08-20 23:22:57 +01:00
const cleanedData = JSON.parse(JSON.stringify(data));
2015-06-03 21:45:54 -07:00
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.albumName = matches[0].trim();
}
2015-08-20 23:22:57 +01:00
return yield search(cleanedData);
2014-12-04 21:11:55 +00:00
} else {
2015-08-20 23:22:57 +01:00
return {service: 'itunes'};
2015-06-03 21:45:54 -07:00
}
} else {
result = result.results[0];
2015-08-20 23:22:57 +01:00
const item = {
2015-06-03 21:45:54 -07:00
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-08-20 23:22:57 +01:00
return item;
2015-06-03 21:45:54 -07:00
}
};