Finally finished giant ES6 refactor
This commit is contained in:
parent
c6d48cc424
commit
03e2666958
39 changed files with 553 additions and 635 deletions
|
@ -1,70 +1,70 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var Promise = require('bluebird');
|
||||
var spotify = Promise.promisifyAll(require('spotify'));
|
||||
import { parse } from 'url';
|
||||
import bluebird from 'bluebird';
|
||||
import spotifyCB from 'spotify';
|
||||
const spotify = bluebird.promisifyAll(spotifyCB);
|
||||
import { match as urlMatch } from './url';
|
||||
|
||||
module.exports.id = "spotify";
|
||||
export let id = "spotify";
|
||||
|
||||
module.exports.match = require('./url').match;
|
||||
export const match = urlMatch;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
export function* parseUrl(url) {
|
||||
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
|
||||
if (matches && matches[2]) {
|
||||
return module.exports.lookupId(matches[2], matches[1]);
|
||||
return yield lookupId(matches[2], matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
return spotify.lookupAsync({id: id, type: type}).then(function(data) {
|
||||
if (data.error) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
export function* lookupId(id, type) {
|
||||
const data = yield spotify.lookupAsync({id: id, type: type});
|
||||
if (data.error) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
var artist = data.artists[0];
|
||||
var artist = data.artists[0];
|
||||
|
||||
if (type == "album") {
|
||||
return {
|
||||
service: "spotify",
|
||||
type: type,
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: data.images[1].url.replace("http:", "https:"),
|
||||
large: data.images[0].url.replace("http:", "https:"),
|
||||
},
|
||||
artist: {
|
||||
name: artist.name
|
||||
}
|
||||
};
|
||||
} else if (type == "track") {
|
||||
return {
|
||||
service: "spotify",
|
||||
type: type,
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: data.album.images[1].url.replace("http:", "https:"),
|
||||
large: data.album.images[0].url.replace("http:", "https:"),
|
||||
},
|
||||
artist: {
|
||||
name: artist.name
|
||||
},
|
||||
album: {
|
||||
name: data.album.name
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
if (type == "album") {
|
||||
return {
|
||||
service: "spotify",
|
||||
type: type,
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: data.images[1].url.replace("http:", "https:"),
|
||||
large: data.images[0].url.replace("http:", "https:"),
|
||||
},
|
||||
artist: {
|
||||
name: artist.name
|
||||
}
|
||||
};
|
||||
} else if (type == "track") {
|
||||
return {
|
||||
service: "spotify",
|
||||
type: type,
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
streamUrl: "https://play.spotify.com/" + type + "/" + data.id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: data.album.images[1].url.replace("http:", "https:"),
|
||||
large: data.album.images[0].url.replace("http:", "https:"),
|
||||
},
|
||||
artist: {
|
||||
name: artist.name
|
||||
},
|
||||
album: {
|
||||
name: data.album.name
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.search = function(data) {
|
||||
export function* search(data) {
|
||||
var cleanParam = function(str) {
|
||||
var chopChars = ['&', '[', '('];
|
||||
chopChars.forEach(function(chr) {
|
||||
|
@ -85,28 +85,26 @@ module.exports.search = function(data) {
|
|||
album = data.album.name;
|
||||
}
|
||||
|
||||
return spotify.searchAsync({query: query, type: type}).then(function(results) {
|
||||
if (!results[type + "s"].items[0]) {
|
||||
return {service: "spotify"};
|
||||
} else {
|
||||
var found;
|
||||
var choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
||||
if (!choppedAlbum.length) {
|
||||
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
||||
}
|
||||
|
||||
results[type + "s"].items.forEach(function(item) {
|
||||
var albumName = data.type == "album" ? item.name : item.album.name;
|
||||
var matches = albumName.match(/^[^\(\[]+/);
|
||||
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
||||
found = item;
|
||||
}
|
||||
});
|
||||
if (!found) {
|
||||
return {service: "spotify"};
|
||||
}
|
||||
return module.exports.lookupId(results[type + "s"].items[0].id, type);
|
||||
const results = yield spotify.searchAsync({query: query, type: type});
|
||||
if (!results[type + "s"].items[0]) {
|
||||
return {service: "spotify"};
|
||||
} else {
|
||||
let found;
|
||||
const choppedAlbum = data.type == "album" ? cleanParam(data.name) : cleanParam(data.album.name);
|
||||
if (!choppedAlbum.length) {
|
||||
return yield lookupId(results[type + "s"].items[0].id, type);
|
||||
}
|
||||
|
||||
});
|
||||
results[type + "s"].items.forEach(function(item) {
|
||||
const albumName = data.type == "album" ? item.name : item.album.name;
|
||||
const matches = albumName.match(/^[^\(\[]+/);
|
||||
if(choppedAlbum.indexOf(matches[0]) >= 0) {
|
||||
found = item;
|
||||
}
|
||||
});
|
||||
if (!found) {
|
||||
return {service: "spotify"};
|
||||
}
|
||||
return yield lookupId(results[type + "s"].items[0].id, type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
import { parse } from 'url';
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
export function* match(url, type) {
|
||||
const parsed = parse(url);
|
||||
if (!parsed.host.match(/spotify\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
const matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
return matches && !!matches[2];
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue