Finally finished giant ES6 refactor
This commit is contained in:
parent
c6d48cc424
commit
03e2666958
39 changed files with 553 additions and 635 deletions
|
@ -1,40 +1,45 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var Promise = require('bluebird');
|
||||
var request = require('superagent');
|
||||
require('superagent-bluebird-promise');
|
||||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
import request from 'superagent';
|
||||
import 'superagent-bluebird-promise';
|
||||
import { match as urlMatch } from './url';
|
||||
|
||||
module.exports.id = "xbox";
|
||||
export let id = "xbox";
|
||||
|
||||
if (!process.env.XBOX_CLIENT_ID || !process.env.XBOX_CLIENT_SECRET) {
|
||||
console.warn("XBOX_CLIENT_ID and XBOX_CLIENT_SECRET environment variables not found, deactivating Xbox Music.");
|
||||
} else {
|
||||
}
|
||||
|
||||
var credentials = {
|
||||
const credentials = {
|
||||
clientId: process.env.XBOX_CLIENT_ID,
|
||||
clientSecret: process.env.XBOX_CLIENT_SECRET
|
||||
};
|
||||
|
||||
var apiRoot = "https://music.xboxlive.com/1/content";
|
||||
const apiRoot = "https://music.xboxlive.com/1/content";
|
||||
|
||||
var getAccessToken = function() {
|
||||
var authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
|
||||
var scope = "http://music.xboxlive.com";
|
||||
var grantType = "client_credentials";
|
||||
|
||||
var data = {client_id: credentials.clientId, client_secret: credentials.clientSecret, scope: scope, grant_type: grantType};
|
||||
return request.post(authUrl).send(data).set('Content-type', 'application/x-www-form-urlencoded').promise().then(function(res) {
|
||||
return res.body.access_token;
|
||||
});
|
||||
function* getAccessToken() {
|
||||
const authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
|
||||
const scope = "http://music.xboxlive.com";
|
||||
const grantType = "client_credentials";
|
||||
|
||||
const data = {
|
||||
client_id: credentials.clientId,
|
||||
client_secret: credentials.clientSecret,
|
||||
scope: scope,
|
||||
grant_type: grantType
|
||||
};
|
||||
const result = yield request.post(authUrl).send(data).set('Content-type', 'application/x-www-form-urlencoded').promise();
|
||||
return result.body.access_token;
|
||||
}
|
||||
|
||||
var formatResponse = function(res) {
|
||||
function formatResponse(res) {
|
||||
let result;
|
||||
if (res.body.Tracks) {
|
||||
var result = res.body.Tracks.Items[0];
|
||||
result = res.body.Tracks.Items[0];
|
||||
} else {
|
||||
var result = res.body.Albums.Items[0];
|
||||
result = res.body.Albums.Items[0];
|
||||
}
|
||||
var item = {
|
||||
let item = {
|
||||
service: "xbox",
|
||||
type: res.body.Tracks ? "track" : "album",
|
||||
id: result.Id,
|
||||
|
@ -55,37 +60,33 @@ var formatResponse = function(res) {
|
|||
return item;
|
||||
}
|
||||
|
||||
module.exports.match = require('./url').match;
|
||||
export const match = urlMatch;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var parsed = parse(url);
|
||||
var parts = parsed.path.split("/");
|
||||
var type = parts[1];
|
||||
var idMatches = parts[4].match(/[\w\-]+/);
|
||||
var id = idMatches[0];
|
||||
export function* parseUrl(url) {
|
||||
const parsed = parse(url);
|
||||
const parts = parsed.path.split("/");
|
||||
const type = parts[1];
|
||||
const idMatches = parts[4].match(/[\w\-]+/);
|
||||
const id = idMatches[0];
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
return module.exports.lookupId("music." + id, type);
|
||||
return yield lookupId("music." + id, type);
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
return getAccessToken().then(function(access_token){
|
||||
var path = "/" + id + "/lookup";
|
||||
return request.get(apiRoot + path).set("Authorization", "Bearer " + access_token).promise().then(function(res) {
|
||||
return formatResponse(res);
|
||||
}, function(res) {
|
||||
return {service: "xbox"};
|
||||
});
|
||||
});
|
||||
export function* lookupId(id, type) {
|
||||
const access_token = yield getAccessToken();
|
||||
const path = "/" + id + "/lookup";
|
||||
const result = yield request.get(apiRoot + path).set("Authorization", "Bearer " + access_token).promise();
|
||||
return result ? formatResponse(result) : {service: "xbox"};
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
export function* search(data) {
|
||||
var cleanParam = function(str) {
|
||||
return str.replace(/[\:\?\&]+/, "");
|
||||
}
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
let query, album;
|
||||
const type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + " " + cleanParam(data.name);
|
||||
|
@ -94,14 +95,8 @@ module.exports.search = function(data) {
|
|||
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + " " + cleanParam(data.name);
|
||||
album = data.album.name
|
||||
}
|
||||
return getAccessToken().then(function(access_token){
|
||||
var path = "/music/search?q=" + encodeURIComponent(query) + "&filters=" + type + "s";
|
||||
return request.get(apiRoot + path).set("Authorization", "Bearer " + access_token).promise().then(function(res) {
|
||||
return formatResponse(res);
|
||||
}, function(res) {
|
||||
return {service: "xbox"};
|
||||
});
|
||||
});
|
||||
const access_token = yield getAccessToken();
|
||||
const path = "/music/search?q=" + encodeURIComponent(query) + "&filters=" + type + "s";
|
||||
const result = yield request.get(apiRoot + path).set("Authorization", "Bearer " + access_token).promise()
|
||||
return result ? formatResponse(result) : {service: "xbox"};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
import { parse } from 'url';
|
||||
|
||||
export function* match(url, type) {
|
||||
const parsed = parse(url);
|
||||
if (!parsed.host.match(/music.xbox.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var parts = parsed.path.split("/");
|
||||
|
||||
const parts = parsed.path.split("/");
|
||||
return (parts[1] == "album" || parts[1] == "track") && parts[4];
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue