2015-08-20 23:22:57 +01:00
|
|
|
import { parse } from 'url';
|
|
|
|
import querystring from 'querystring';
|
|
|
|
import request from 'superagent';
|
|
|
|
import 'superagent-bluebird-promise';
|
|
|
|
import { match as urlMatch } from './url';
|
2014-12-21 01:19:37 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export let id = "xbox";
|
2014-12-21 01:19:37 +00:00
|
|
|
|
|
|
|
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.");
|
2015-08-20 23:22:57 +01:00
|
|
|
}
|
2014-12-21 01:19:37 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
const credentials = {
|
2014-12-21 01:19:37 +00:00
|
|
|
clientId: process.env.XBOX_CLIENT_ID,
|
|
|
|
clientSecret: process.env.XBOX_CLIENT_SECRET
|
|
|
|
};
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
const apiRoot = "https://music.xboxlive.com/1/content";
|
|
|
|
|
|
|
|
function* getAccessToken() {
|
|
|
|
const authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
|
|
|
|
const scope = "http://music.xboxlive.com";
|
|
|
|
const grantType = "client_credentials";
|
2014-12-21 01:19:37 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
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;
|
2014-12-21 01:19:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
function formatResponse(res) {
|
|
|
|
let result;
|
2014-12-21 01:19:37 +00:00
|
|
|
if (res.body.Tracks) {
|
2015-08-20 23:22:57 +01:00
|
|
|
result = res.body.Tracks.Items[0];
|
2014-12-21 01:19:37 +00:00
|
|
|
} else {
|
2015-08-20 23:22:57 +01:00
|
|
|
result = res.body.Albums.Items[0];
|
2014-12-21 01:19:37 +00:00
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
let item = {
|
2014-12-21 01:19:37 +00:00
|
|
|
service: "xbox",
|
|
|
|
type: res.body.Tracks ? "track" : "album",
|
|
|
|
id: result.Id,
|
|
|
|
name: result.Name,
|
|
|
|
streamUrl: result.Link,
|
|
|
|
purchaseUrl: null,
|
|
|
|
artwork: {
|
2014-12-22 22:38:08 +00:00
|
|
|
small: result.ImageUrl.replace("http://", "https://") + "&w=250&h=250",
|
|
|
|
large: result.ImageUrl.replace("http://", "https://") + "&w=500&h=250"
|
2014-12-21 01:19:37 +00:00
|
|
|
},
|
|
|
|
artist: {
|
|
|
|
name: result.Artists[0].Artist.Name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (result.Album) {
|
|
|
|
item.album = {name: result.Album.Name}
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export const match = urlMatch;
|
2014-12-21 01:19:37 +00:00
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
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];
|
2015-01-07 00:05:43 +00:00
|
|
|
if (!id) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
return yield lookupId("music." + id, type);
|
2014-12-21 01:19:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
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"};
|
2014-12-21 01:19:37 +00:00
|
|
|
};
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
export function* search(data) {
|
2015-01-27 21:32:28 +00:00
|
|
|
var cleanParam = function(str) {
|
|
|
|
return str.replace(/[\:\?\&]+/, "");
|
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
let query, album;
|
|
|
|
const type = data.type;
|
2014-12-21 01:19:37 +00:00
|
|
|
|
|
|
|
if (type == "album") {
|
2015-01-27 21:32:28 +00:00
|
|
|
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + " " + cleanParam(data.name);
|
2014-12-21 01:19:37 +00:00
|
|
|
album = data.name;
|
|
|
|
} else if (type == "track") {
|
2015-01-27 21:32:28 +00:00
|
|
|
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + " " + cleanParam(data.name);
|
2014-12-21 01:19:37 +00:00
|
|
|
album = data.album.name
|
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
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"};
|
2014-12-21 01:19:37 +00:00
|
|
|
};
|