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

123 lines
3.3 KiB
JavaScript
Raw Normal View History

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-21 18:33:50 +01:00
import debuglog from 'debug';
const debug = debuglog('match.audio:xbox');
export let id = 'xbox';
2014-12-21 01:19:37 +00:00
if (!process.env.XBOX_CLIENT_ID || !process.env.XBOX_CLIENT_SECRET) {
2015-08-21 18:33:50 +01:00
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-21 18:33:50 +01:00
const apiRoot = 'https://music.xboxlive.com/1/content';
2015-08-20 23:22:57 +01:00
function* getAccessToken() {
2017-05-06 20:30:55 +01:00
const authUrl = 'https://login.live.com/accesstoken.srf';
const scope = 'app.music.xboxlive.com';
2015-08-21 18:33:50 +01:00
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
};
2015-08-21 18:33:50 +01:00
const result = yield request.post(authUrl).timeout(10000).send(data).set('Content-type', 'application/x-www-form-urlencoded').promise();
2015-08-20 23:22:57 +01:00
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-21 18:33:50 +01:00
const item = {
service: 'xbox',
type: res.body.Tracks ? 'track' : 'album',
2014-12-21 01:19:37 +00:00
id: result.Id,
name: result.Name,
streamUrl: result.Link,
purchaseUrl: null,
artwork: {
2015-08-21 18:33:50 +01:00
small: result.ImageUrl.replace('http://', 'https://') + '&w=250&h=250',
large: result.ImageUrl.replace('http://', 'https://') + '&w=500&h=500'
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-21 18:33:50 +01:00
function* apiCall(path) {
const access_token = yield getAccessToken();
return request.get(apiRoot + path).timeout(10000).set('Authorization', 'Bearer ' + access_token).promise();
}
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);
2015-08-21 18:33:50 +01:00
const parts = parsed.path.split('/');
2015-08-20 23:22:57 +01:00
const type = parts[1];
const idMatches = parts[4].match(/[\w\-]+/);
const id = idMatches[0];
if (!id) {
return false;
}
2015-08-21 18:33:50 +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) {
2015-08-21 18:33:50 +01:00
const path = '/' + id + '/lookup';
try {
const result = yield apiCall(path);
return formatResponse(result);
} catch (e) {
if (e.status !== 404) {
debug(e.body);
}
return {service: 'xbox'};
}
2014-12-21 01:19:37 +00:00
};
2015-08-20 23:22:57 +01:00
export function* search(data) {
var cleanParam = function(str) {
2015-08-21 18:33:50 +01:00
return str.replace(/[\:\?\&\(\)\[\]]+/g, '');
}
2015-08-20 23:22:57 +01:00
let query, album;
const type = data.type;
2014-12-21 01:19:37 +00:00
2015-08-21 18:33:50 +01:00
if (type == 'album') {
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;
2015-08-21 18:33:50 +01:00
} else if (type == 'track') {
query = cleanParam(data.artist.name.substring(0, data.artist.name.indexOf('&'))) + ' ' + cleanParam(data.name);
album = data.albumName
2014-12-21 01:19:37 +00:00
}
2015-08-21 18:33:50 +01:00
const path = '/music/search?q=' + encodeURIComponent(query) + '&filters=' + type + 's';
try {
const result = yield apiCall(path);
return formatResponse(result);
} catch (e) {
if (e.status !== 404) {
debug(e.body);
}
return {service: 'xbox'};
}
2014-12-21 01:19:37 +00:00
};