Code cleanup
This commit is contained in:
parent
a74ccfa575
commit
be00516521
18 changed files with 209 additions and 202 deletions
|
@ -1,8 +1,7 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var Promise = require('bluebird');
|
||||
var request = require('superagent');
|
||||
require('superagent-bluebird-promise');
|
||||
var parse = require("url").parse;
|
||||
var request = require("superagent");
|
||||
require("superagent-bluebird-promise");
|
||||
|
||||
module.exports.id = "beats";
|
||||
|
||||
|
@ -29,10 +28,10 @@ module.exports.parseUrl = function(url) {
|
|||
} else {
|
||||
throw new Error("Url does not match");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
if (type == "album") {
|
||||
if (type === "album") {
|
||||
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) {
|
||||
|
@ -59,7 +58,7 @@ module.exports.lookupId = function(id, type) {
|
|||
});
|
||||
});
|
||||
});
|
||||
} else if (type == "track") {
|
||||
} else if (type === "track") {
|
||||
return request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key).promise().then(function(res) {
|
||||
if (!res.body.data) {
|
||||
var error = new Error("Not Found");
|
||||
|
@ -99,16 +98,20 @@ module.exports.lookupId = function(id, type) {
|
|||
module.exports.search = function(data) {
|
||||
var cleanParam = function(str) {
|
||||
return str.replace(/[\:\?\&]+/, "");
|
||||
}
|
||||
};
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = '"' + cleanParam(data.artist.name) + '" "' + cleanParam(data.name) + '"';
|
||||
if (type === "album") {
|
||||
query = "'" + cleanParam(data.artist.name) + "' '" + cleanParam(data.name) + "'";
|
||||
album = data.name;
|
||||
} else if (type == "track") {
|
||||
query = '"' + cleanParam(data.artist.name) + '" "' + cleanParam(data.name) + '"';
|
||||
album = data.album.name
|
||||
} else if (type === "track") {
|
||||
query = "'" + cleanParam(data.artist.name) + "' '" + cleanParam(data.name) + "'";
|
||||
if (data.album) {
|
||||
album = data.album.name;
|
||||
} else {
|
||||
album = "";
|
||||
}
|
||||
}
|
||||
|
||||
var path = "/search?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key;
|
||||
|
@ -118,20 +121,20 @@ module.exports.search = function(data) {
|
|||
return {service: "beats"};
|
||||
} else {
|
||||
var found;
|
||||
var choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
||||
var choppedAlbum = data.type === "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
||||
var choppedArtist = cleanParam(data.artist.name);
|
||||
|
||||
|
||||
res.body.data.forEach(function(item) {
|
||||
var matches = item.detail.match(/^[^\(\[]+/);
|
||||
if(choppedArtist.indexOf(matches[0]) >= 0) {
|
||||
found = item;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (!found && !choppedAlbum.length) {
|
||||
return module.exports.lookupId(res.body.data[0].id, type);
|
||||
}
|
||||
|
||||
|
||||
res.body.data.forEach(function(item) {
|
||||
var matches = item.related.display.match(/^[^\(\[]+/);
|
||||
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var parse = require("url").parse;
|
||||
|
||||
module.exports.match = function(url) {
|
||||
var parsed = parse(url);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue