2014-12-04 21:11:55 +00:00
|
|
|
"use strict";
|
2014-12-13 00:00:49 +00:00
|
|
|
var parse = require("url").parse;
|
|
|
|
var Promise = require('bluebird');
|
2014-12-11 19:53:12 +00:00
|
|
|
var querystring = require('querystring');
|
2014-12-04 21:11:55 +00:00
|
|
|
var request = require('superagent');
|
2014-12-13 00:00:49 +00:00
|
|
|
require('superagent-bluebird-promise');
|
2014-12-04 21:11:55 +00:00
|
|
|
|
|
|
|
module.exports.id = "itunes";
|
|
|
|
|
|
|
|
var apiRoot = "https://itunes.apple.com";
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
|
|
|
|
|
|
|
module.exports.parseUrl = function(url) {
|
2014-12-04 21:11:55 +00:00
|
|
|
var parsed = parse(url);
|
2014-12-11 19:53:12 +00:00
|
|
|
var matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
|
|
|
var query = querystring.parse(parsed.query);
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
var type = "album";
|
|
|
|
var id = matches[3].substr(2);
|
|
|
|
if (query.i) {
|
|
|
|
type = "track";
|
|
|
|
id = query.i;
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(id, type, matches[1] || "us");
|
2014-12-11 19:53:12 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
throw new Error();
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
2014-12-04 21:11:55 +00:00
|
|
|
};
|
|
|
|
|
2014-12-06 21:12:52 +00:00
|
|
|
module.exports.lookupId = function(id, type, cc) {
|
|
|
|
if (id.match(/^[a-z]{2}/)) {
|
|
|
|
cc = id.substr(0,2);
|
|
|
|
id = id.substr(2);
|
|
|
|
}
|
|
|
|
|
2014-12-04 21:53:50 +00:00
|
|
|
var path = "/lookup?id=" + id;
|
2014-12-06 21:12:52 +00:00
|
|
|
if (cc) {
|
|
|
|
path = "/" + cc + path;
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
2014-12-04 21:53:50 +00:00
|
|
|
var data = JSON.parse(res.text);
|
|
|
|
|
2014-12-05 20:44:10 +00:00
|
|
|
if (!data.results || data.resultCount == 0 || !data.results[0].collectionId) {
|
2014-12-05 18:08:40 +00:00
|
|
|
var error = new Error("Not Found");
|
|
|
|
error.status = 404;
|
2014-12-13 00:00:49 +00:00
|
|
|
throw error;
|
2014-12-04 21:53:50 +00:00
|
|
|
} else {
|
|
|
|
var result = data.results[0];
|
|
|
|
|
|
|
|
var item = {
|
|
|
|
service: "itunes",
|
|
|
|
type: type,
|
2014-12-11 19:53:12 +00:00
|
|
|
id: cc + id,
|
2014-12-04 21:53:50 +00:00
|
|
|
name: result.trackName ? result.trackName : result.collectionName,
|
|
|
|
streamUrl: null,
|
|
|
|
purchaseUrl: result.collectionViewUrl,
|
2014-12-13 12:05:47 +00:00
|
|
|
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://", ""),
|
|
|
|
},
|
2014-12-04 21:53:50 +00:00
|
|
|
artist: {
|
|
|
|
name: result.artistName
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type == "track") {
|
|
|
|
item.album = {
|
|
|
|
name: result.collectionName
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return item;
|
2014-12-04 21:53:50 +00:00
|
|
|
}
|
|
|
|
});
|
2014-12-11 19:53:12 +00:00
|
|
|
};
|
2014-12-04 21:11:55 +00:00
|
|
|
|
|
|
|
module.exports.search = function(data) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var query, album, entity;
|
2014-12-04 21:11:55 +00:00
|
|
|
var type = data.type;
|
|
|
|
|
|
|
|
if (type == "album") {
|
|
|
|
query = data.artist.name + " " + data.name;
|
2014-12-05 14:13:47 +00:00
|
|
|
album = data.name;
|
2014-12-04 21:53:50 +00:00
|
|
|
entity = "album";
|
2014-12-04 21:11:55 +00:00
|
|
|
} 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;
|
2014-12-04 21:53:50 +00:00
|
|
|
entity = "musicTrack";
|
2014-12-04 21:11:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 21:53:50 +00:00
|
|
|
var path = "/search?term=" + encodeURIComponent(query) + "&media=music&entity=" + entity;
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
2014-12-05 21:47:34 +00:00
|
|
|
var result = JSON.parse(res.text);
|
2014-12-04 21:53:50 +00:00
|
|
|
|
2014-12-05 21:47:34 +00:00
|
|
|
if (!result.results[0]) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var matches = album.match(/^[^\(\[]+/);
|
2014-12-05 22:20:17 +00:00
|
|
|
if (matches[0] && matches[0] != album) {
|
2014-12-05 14:13:47 +00:00
|
|
|
var cleanedData = JSON.parse(JSON.stringify(data));
|
|
|
|
if (type == "album") {
|
|
|
|
cleanedData.name = matches[0].trim();
|
|
|
|
} else if (type == "track") {
|
2014-12-05 22:26:12 +00:00
|
|
|
cleanedData.album.name = matches[0].trim();
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.search(cleanedData);
|
2014-12-05 14:13:47 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service: "itunes"};
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-04 21:11:55 +00:00
|
|
|
} else {
|
2014-12-05 21:47:34 +00:00
|
|
|
var result = result.results[0];
|
2014-12-04 21:11:55 +00:00
|
|
|
|
2014-12-04 21:53:50 +00:00
|
|
|
var item = {
|
2014-12-04 21:11:55 +00:00
|
|
|
service: "itunes",
|
|
|
|
type: type,
|
2014-12-06 21:12:52 +00:00
|
|
|
id: "us" + result.collectionId,
|
2014-12-04 21:53:50 +00:00
|
|
|
name: result.trackName ? result.trackName : result.collectionName,
|
2014-12-04 21:11:55 +00:00
|
|
|
streamUrl: null,
|
|
|
|
purchaseUrl: result.collectionViewUrl,
|
2014-12-13 12:05:47 +00:00
|
|
|
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://", ""),
|
|
|
|
},
|
2014-12-04 21:11:55 +00:00
|
|
|
artist: {
|
|
|
|
name: result.artistName
|
|
|
|
}
|
2014-12-04 21:53:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type == "track") {
|
|
|
|
item.album = {
|
|
|
|
name: result.collectionName
|
|
|
|
};
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return item;
|
2014-12-04 21:11:55 +00:00
|
|
|
}
|
|
|
|
});
|
2014-12-11 19:53:12 +00:00
|
|
|
};
|