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

169 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-08-20 00:23:42 +01:00
import { parse } from 'url';
import bluebird from 'bluebird';
import rdioInit from 'rdio';
import { match as urlMatch } from './url';
2015-08-20 00:23:42 +01:00
export let id = 'rdio';
2014-12-01 11:53:25 +00:00
2015-08-20 23:31:56 +01:00
if (!process.env.RDIO_CLIENT_ID || !process.env.RDIO_CLIENT_SECRET) {
console.warn('RDIO_CLIENT_ID or RDIO_CLIENT_SECRET environment constiables not found, deactivating Rdio.');
2015-08-20 00:23:42 +01:00
}
2014-12-01 11:53:25 +00:00
2015-08-20 00:23:42 +01:00
const Rdio = rdioInit({
rdio: {
clientId: process.env.RDIO_CLIENT_ID,
clientSecret: process.env.RDIO_CLIENT_SECRET,
}
2014-12-01 11:53:25 +00:00
});
2015-08-20 00:23:42 +01:00
const rdio = bluebird.promisifyAll(new Rdio());
2015-08-20 00:23:42 +01:00
export const match = urlMatch;
2015-08-20 00:23:42 +01:00
export function* lookupId(id) {
yield rdio.getClientTokenAsync();
const response = yield rdio.requestAsync({method: 'getObjectFromShortCode', short_code: id}, false);
const result = response.result;
const parsedShortUrl = parse(result.shortUrl);
const rid = parsedShortUrl.path.replace('/x/', '').replace('/', '');
const type = result.album ? 'track' : 'album';
2015-07-06 16:06:52 +01:00
2015-08-20 00:23:42 +01:00
const item = {
2015-07-06 16:06:52 +01:00
service: 'rdio',
type: type,
id: rid,
name: result.name,
streamUrl: result.shortUrl,
purchaseUrl: null,
artwork: {
small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'),
large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:')
},
artist: {
name: result.artist
2014-12-05 18:08:40 +00:00
}
2015-07-06 16:06:52 +01:00
};
if (type === 'track') {
item.album = {
name: result.album
2014-12-06 10:45:58 +00:00
};
2015-07-06 16:06:52 +01:00
}
return item;
2014-12-01 11:53:25 +00:00
};
2015-08-20 00:23:42 +01:00
export function* parseUrl(url) {
const parsedUrl = parse(url);
2014-12-01 11:53:25 +00:00
2015-08-20 00:23:42 +01:00
let query, args;
2014-12-01 11:53:25 +00:00
2015-07-06 16:06:52 +01:00
if (parsedUrl.host === 'rd.io') {
2015-08-20 00:23:42 +01:00
query = {
method: 'getObjectFromShortCode',
short_code: parsedUrl.path.replace('/x/', '').replace('/', '')
};
2015-07-06 16:06:52 +01:00
} else if (parsedUrl.host.match(/rdio\.com$/)) {
2015-08-20 00:23:42 +01:00
query = {
method: 'getObjectFromUrl',
url: parsedUrl.path
};
2014-12-01 11:53:25 +00:00
} else {
2015-08-20 00:23:42 +01:00
const error = new Error('Not Found');
2014-12-07 18:33:55 +00:00
error.status = 404;
throw error;
2014-12-01 11:53:25 +00:00
}
2015-08-20 00:23:42 +01:00
yield rdio.getClientTokenAsync();
const response = yield rdio.requestAsync(query, false);
const result = response.result;
const parsedShortUrl = parse(result.shortUrl);
const id = parsedShortUrl.path.replace('/x/', '').replace('/', '');
const type = result.album ? 'track' : 'album';
const item = {
2015-07-06 16:06:52 +01:00
service: 'rdio',
type: type,
id: id,
name: result.name,
streamUrl: result.shortUrl,
purchaseUrl: null,
artwork: {
small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'),
large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:')
},
artist: {
name: result.artist
2014-12-06 10:45:58 +00:00
}
2015-07-06 16:06:52 +01:00
};
if (type === 'track') {
item.album = {
name: result.album
};
}
return item;
2014-12-01 11:53:25 +00:00
};
2015-08-20 00:23:42 +01:00
export function* search(data) {
let query, albumClean;
const type = data.type;
2015-07-06 16:06:52 +01:00
if (type === 'album') {
query = data.artist.name + ' ' + data.name;
albumClean = data.name.match(/([^\(\[]+)/)[0];
2015-07-06 16:06:52 +01:00
} else if (type === 'track') {
query = data.artist.name + ' ' + data.album.name + ' ' + data.name;
try {
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
} catch(e) {
2015-07-06 16:06:52 +01:00
albumClean = '';
}
}
2014-12-04 00:17:35 +00:00
2015-08-20 00:23:42 +01:00
yield rdio.getClientTokenAsync();
const response = yield rdio.requestAsync({method: 'search', query: query, types: type}, false);
const result = response.result.results.filter(function(item) {
2015-07-06 16:06:52 +01:00
if (type === 'album' && item.name.match(/([^\(\[]+)/)[0] === albumClean) {
return item;
} else if (type === 'track' && (item.album.match(/([^\(\[]+)/)[0] === albumClean || !albumClean)) {
return item;
}
}).shift();
if (!result) {
2015-08-20 00:23:42 +01:00
const matches = albumClean.match(/^[^\(\[]+/);
2015-07-06 16:06:52 +01:00
if (matches && matches[0] && matches[0] !== albumClean) {
2015-08-20 00:23:42 +01:00
const cleanedData = JSON.parse(JSON.stringify(data));
2015-07-06 16:06:52 +01:00
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.album.name = matches[0].trim();
}
2015-07-06 16:06:52 +01:00
return module.exports.search(cleanedData);
} else {
2015-07-06 16:06:52 +01:00
return {service: 'rdio'};
}
} else {
2015-08-20 00:23:42 +01:00
const parsedShortUrl = parse(result.shortUrl);
const id = parsedShortUrl.path.replace('/x/', '').replace('/', '');
const item = {
2015-07-06 16:06:52 +01:00
service: 'rdio',
type: type,
id: id,
name: result.name,
streamUrl: result.shortUrl,
purchaseUrl: null,
artwork: {
small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'),
large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:')
},
artist: {
name: result.artist
2014-12-06 10:45:58 +00:00
}
2015-07-06 16:06:52 +01:00
};
if (type === 'track') {
item.album = {
name: result.album
};
}
2015-07-06 16:06:52 +01:00
return item;
}
2014-12-01 11:53:25 +00:00
};