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

181 lines
5 KiB
JavaScript
Raw Normal View History

2014-12-02 11:46:39 +00:00
"use strict";
2014-12-01 11:53:25 +00:00
var parse = require('url').parse;
var Promise = require('bluebird');
module.exports.id = "rdio";
2014-12-01 11:53:25 +00:00
if (!process.env.RDIO_API_KEY || !process.env.RDIO_API_SHARED) {
console.warn("RDIO_API_KEY or RDIO_API_SHARED environment variables not found, deactivating Rdio.");
2015-06-03 21:45:54 -07:00
} else {
2014-12-01 11:53:25 +00:00
var rdio = require('rdio')({
rdio_api_key: process.env.RDIO_API_KEY,
rdio_api_shared: process.env.RDIO_API_SHARED,
});
var rdio = Promise.promisifyAll(rdio);
module.exports.match = require('./url').match;
module.exports.lookupId = function(id) {
return rdio.apiAsync("", "", {method: 'getObjectFromShortCode', short_code: id}).then(function(results) {
if (!JSON.parse(results[0]).result) {
2014-12-05 18:08:40 +00:00
var error = new Error("Not Found");
error.status = 404;
throw error;
2014-12-05 18:08:40 +00:00
}
var result = JSON.parse(results[0]).result;
2014-12-01 11:53:25 +00:00
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
2014-12-02 01:35:10 +00:00
var type = result.album ? "track" : "album";
2014-12-13 12:05:47 +00:00
2014-12-06 10:45:58 +00:00
var item = {
service: "rdio",
type: type,
2014-12-01 11:53:25 +00:00
id: id,
name: result.name,
2014-12-04 21:11:55 +00:00
streamUrl: result.shortUrl,
purchaseUrl: null,
2014-12-13 12:05:47 +00:00
artwork: {
small: result.icon.replace("square-200", "square-250").replace("http:", "https:"),
large: result.icon.replace("square-200", "square-600").replace("http:", "https:")
},
2014-12-01 11:53:25 +00:00
artist: {
name: result.artist
}
2014-12-06 10:45:58 +00:00
};
if (type == "track") {
item.album = {
name: result.album
};
}
return item;
2014-12-01 11:53:25 +00:00
});
};
module.exports.parseUrl = function(url) {
2014-12-01 11:53:25 +00:00
var parsed = parse(url);
var data;
if (parsed.host == "rd.io") {
data = {
method: 'getObjectFromShortCode',
short_code: parsed.path.replace("/x/", "").replace("/", ""),
};
} else if (parsed.host.match(/rdio\.com$/)) {
data = {
method: 'getObjectFromUrl',
url: parsed.path,
};
} else {
2014-12-07 18:33:55 +00:00
var error = new Error("Not Found");
error.status = 404;
throw error;
2014-12-01 11:53:25 +00:00
}
return rdio.apiAsync("", "", data).then(function(results) {
var results = JSON.parse(results[0]);
2014-12-07 18:33:55 +00:00
var result = results.result;
if (!result || results.status != "ok") {
var error = new Error("Not Found");
error.status = 404;
throw error;
2014-12-07 18:33:55 +00:00
} else {
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
var type = result.album ? "track" : "album";
var item = {
service: "rdio",
type: type,
id: id,
name: result.name,
streamUrl: result.shortUrl,
purchaseUrl: null,
2014-12-13 12:05:47 +00:00
artwork: {
small: result.icon.replace("square-200", "square-250").replace("http:", "https:"),
large: result.icon.replace("square-200", "square-600").replace("http:", "https:")
},
2014-12-07 18:33:55 +00:00
artist: {
name: result.artist
}
2014-12-06 10:45:58 +00:00
};
2014-12-07 18:33:55 +00:00
if (type == "track") {
item.album = {
name: result.album
};
}
return item;
2014-12-06 10:45:58 +00:00
}
2014-12-01 11:53:25 +00:00
});
};
module.exports.search = function(data) {
var query, albumClean;
var type = data.type;
if (type == "album") {
query = data.artist.name + " " + data.name;
albumClean = data.name.match(/([^\(\[]+)/)[0];
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
try {
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
} catch(e) {
albumClean = "";
}
}
2014-12-04 00:17:35 +00:00
return rdio.apiAsync("", "", {query: query, method: 'search', types: type}).then(function(results) {
var results = JSON.parse(results[0]).result.results;
2014-12-04 00:17:35 +00:00
var result = results.filter(function(result) {
if (type == "album" && result.name.match(/([^\(\[]+)/)[0] == albumClean) {
2014-12-04 00:17:35 +00:00
return result;
} else if (type == "track" && (result.album.match(/([^\(\[]+)/)[0] == albumClean || !albumClean)) {
2014-12-04 00:17:35 +00:00
return result;
}
}).shift();
2014-12-04 15:45:52 +00:00
2014-12-02 01:35:10 +00:00
if (!result) {
2014-12-05 21:14:03 +00:00
var matches = albumClean.match(/^[^\(\[]+/);
if (matches && matches[0] && matches[0] != albumClean) {
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: "rdio"};
}
} else {
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
2014-12-06 10:45:58 +00:00
var item = {
service: "rdio",
type: type,
id: id,
name: result.name,
streamUrl: result.shortUrl,
purchaseUrl: null,
2014-12-13 13:03:43 +00:00
artwork: {
small: result.icon.replace("square-200", "square-250").replace("http:", "https:"),
large: result.icon.replace("square-200", "square-600").replace("http:", "https:")
},
artist: {
name: result.artist
}
2014-12-06 10:45:58 +00:00
};
if (type == "track") {
item.album = {
name: result.album
};
}
return item;
}
2014-12-01 11:53:25 +00:00
});
};
2015-06-03 21:45:54 -07:00
}