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

151 lines
5 KiB
JavaScript
Raw Normal View History

2014-12-04 15:45:52 +00:00
"use strict";
2015-05-17 19:10:30 +01:00
var parse = require("url").parse;
var request = require("superagent");
require("superagent-bluebird-promise");
module.exports.id = "beats";
2014-12-04 15:45:52 +00:00
if (!process.env.BEATS_KEY || !process.env.BEATS_SECRET) {
console.warn("BEATS_KEY or BEATS_SECRET environment variables not found, deactivating Beats.");
return;
2014-12-04 15:45:52 +00:00
}
var credentials = {
key: process.env.BEATS_KEY,
secret: process.env.BEATS_SECRET
};
var apiRoot = "https://partner.api.beatsmusic.com/v1/api";
module.exports.match = require("./url").match;
module.exports.parseUrl = function(url) {
var matches = parse(url).path.match(/\/albums[\/]+([^\/]+)(\/tracks\/)?([^\/]+)?/);
if (matches && matches[3]) {
2014-12-12 19:31:58 +00:00
return module.exports.lookupId(matches[3], "track");
} else if (matches && matches[1]) {
2014-12-12 19:31:58 +00:00
return module.exports.lookupId(matches[1], "album");
} else {
2014-12-12 19:31:58 +00:00
throw new Error("Url does not match");
}
2015-05-17 19:10:30 +01:00
};
module.exports.lookupId = function(id, type) {
2015-05-17 19:10:30 +01:00
if (type === "album") {
2014-12-13 12:05:47 +00:00
return request.get(apiRoot + "/albums/" + id + "/images/default?size=large&client_id=" + credentials.key).redirects(0).promise().then(function(res) {
var artwork = {large: res.headers.location.replace("http:", "https:")};
return request.get(apiRoot + "/albums/" + id + "/images/default?client_id=" + credentials.key).redirects(0).promise().then(function(res) {
artwork.small = res.headers.location.replace("http:", "https:");
return request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key).promise().then(function(res) {
if (!res.body.data) {
var error = new Error("Not Found");
error.status = 404;
throw error;
2014-12-04 15:45:52 +00:00
}
2014-12-13 12:05:47 +00:00
var result = res.body.data;
return {
service: "beats",
type: "album",
id: result.id,
name: result.title,
streamUrl: "https://listen.beatsmusic.com/albums/" + result.id,
purchaseUrl: null,
artwork: artwork,
artist: {
name: result.artist_display_name
}
};
});
2014-12-04 15:45:52 +00:00
});
});
2015-05-17 19:10:30 +01:00
} else if (type === "track") {
2014-12-12 19:31:58 +00:00
return request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key).promise().then(function(res) {
if (!res.body.data) {
var error = new Error("Not Found");
error.status = 404;
2014-12-12 19:31:58 +00:00
throw error;
}
2014-12-04 15:45:52 +00:00
var result = res.body.data;
2014-12-13 12:05:47 +00:00
return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=large&client_id=" + credentials.key).redirects(0).promise().then(function(res) {
var artwork = {large: res.headers.location.replace("http:", "https:")};
return request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?client_id=" + credentials.key).redirects(0).promise().then(function(res) {
artwork.small = res.headers.location.replace("http:", "https:");
return {
service: "beats",
type: "track",
id: result.id,
name: result.title,
streamUrl: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id,
purchaseUrl: null,
artwork: artwork,
artist: {
name: result.artist_display_name
},
album: {
name: result.refs.album.display
}
};
});
2014-12-04 15:45:52 +00:00
});
});
2014-12-05 18:08:40 +00:00
} else {
var error = new Error("Not Found");
error.status = 404;
2014-12-12 19:31:58 +00:00
return error;
2014-12-04 15:45:52 +00:00
}
};
module.exports.search = function(data) {
var cleanParam = function(str) {
return str.replace(/[\:\?\&]+/, "");
2015-05-17 19:10:30 +01:00
};
var query, album;
2014-12-04 15:45:52 +00:00
var type = data.type;
2015-05-17 19:10:30 +01:00
if (type === "album") {
query = "'" + cleanParam(data.artist.name) + "' '" + cleanParam(data.name) + "'";
album = data.name;
2015-05-17 19:10:30 +01:00
} else if (type === "track") {
query = "'" + cleanParam(data.artist.name) + "' '" + cleanParam(data.name) + "'";
if (data.album) {
album = data.album.name;
} else {
album = "";
}
2014-12-04 15:45:52 +00:00
}
var path = "/search?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key;
2014-12-12 19:31:58 +00:00
return request.get(apiRoot + path).promise().then(function(res) {
2014-12-04 15:45:52 +00:00
if (!res.body.data[0]) {
return {service: "beats"};
} else {
var found;
2015-05-17 19:10:30 +01:00
var choppedAlbum = data.type === "album" ? cleanParam(data.name) : cleanParam(data.album.name);
var choppedArtist = cleanParam(data.artist.name);
2015-05-17 19:10:30 +01:00
res.body.data.forEach(function(item) {
var matches = item.detail.match(/^[^\(\[]+/);
if(choppedArtist.indexOf(matches[0]) >= 0) {
found = item;
}
});
2015-05-17 19:10:30 +01:00
if (!found && !choppedAlbum.length) {
return module.exports.lookupId(res.body.data[0].id, type);
}
2015-05-17 19:10:30 +01:00
res.body.data.forEach(function(item) {
var matches = item.related.display.match(/^[^\(\[]+/);
if(choppedAlbum.indexOf(matches[0]) >= 0) {
found = item;
}
});
if (!found) {
2014-12-12 19:31:58 +00:00
return {service: "beats"};
}
return module.exports.lookupId(found.id, type);
2014-12-04 15:45:52 +00:00
}
});
};