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

139 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-12-04 21:11:55 +00:00
"use strict";
var parse = require("url").parse;
var Promise = require('bluebird');
var querystring = require('querystring');
2014-12-04 21:11:55 +00:00
var request = require('superagent');
require('superagent-bluebird-promise');
2014-12-04 21:11:55 +00:00
module.exports.id = "itunes";
var apiRoot = "https://itunes.apple.com";
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url) {
2014-12-04 21:11:55 +00:00
var parsed = parse(url);
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;
}
return module.exports.lookupId(id, type, matches[1] || "us");
} else {
throw new Error();
}
2014-12-04 21:11:55 +00:00
};
2014-12-06 21:12:52 +00:00
module.exports.lookupId = function(id, type, cc) {
2014-12-13 12:12:44 +00:00
if (String(id).match(/^[a-z]{2}/)) {
2014-12-06 21:12:52 +00:00
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;
}
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;
throw error;
2014-12-04 21:53:50 +00:00
} else {
var result = data.results[0];
var item = {
service: "itunes",
type: type,
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
};
}
return item;
2014-12-04 21:53:50 +00:00
}
});
};
2014-12-04 21:11:55 +00:00
module.exports.search = function(data) {
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;
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;
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;
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]) {
var matches = album.match(/^[^\(\[]+/);
2014-12-05 22:20:17 +00:00
if (matches[0] && matches[0] != album) {
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();
}
return module.exports.search(cleanedData);
} else {
return {service: "itunes"};
}
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
};
}
return item;
2014-12-04 21:11:55 +00:00
}
});
};