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

109 lines
2.7 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';
2017-07-20 14:31:07 +01:00
import Nodebrainz from 'nodebrainz';
2015-08-20 23:22:57 +01:00
import 'superagent-bluebird-promise';
2015-08-21 18:33:50 +01:00
import debuglog from 'debug';
2017-07-20 14:31:07 +01:00
import urlMatch from './url';
2015-08-21 18:33:50 +01:00
2017-10-23 17:58:23 +01:00
const debug = debuglog('combine.fm:youtube');
if (!process.env.YOUTUBE_KEY) {
2017-07-20 14:31:07 +01:00
debug('YOUTUBE_KEY environment variable not found, deactivating Youtube.');
2015-08-20 23:22:57 +01:00
}
2015-08-20 23:22:57 +01:00
const credentials = {
key: process.env.YOUTUBE_KEY,
};
2015-08-20 23:22:57 +01:00
const apiRoot = 'https://www.googleapis.com/youtube/v3';
2017-07-20 14:31:07 +01:00
const nodebrainz = new Nodebrainz({
2017-10-23 17:58:23 +01:00
userAgent: 'combine.fm ( https://combine.fm )',
2017-07-20 14:31:07 +01:00
defaultLimit: 10,
retryOn: true,
retryDelay: 3000,
retryCount: 10,
});
2017-07-20 14:31:07 +01:00
export function* lookupId(id) {
const path = `/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=${id}&key=${credentials.key}`;
2015-08-21 18:33:50 +01:00
try {
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
2017-07-20 14:31:07 +01:00
nodebrainz.luceneSearch('release', { query: item.snippet.title }, (err, response) => {
response.releases.forEach((release) => {
//console.log(release);
});
});
2015-08-20 23:22:57 +01:00
2015-08-21 18:33:50 +01:00
const match = {
2017-07-20 14:31:07 +01:00
id,
2015-08-21 18:33:50 +01:00
service: 'youtube',
name: item.snippet.title,
type: 'track',
2017-07-20 14:31:07 +01:00
album: { name: '' },
streamUrl: `https://youtu.be/${id}`,
2015-08-21 18:33:50 +01:00
purchaseUrl: null,
artwork: {
small: item.snippet.thumbnails.medium.url,
large: item.snippet.thumbnails.high.url,
2017-07-20 14:31:07 +01:00
},
2015-08-21 18:33:50 +01:00
};
2015-08-20 23:22:57 +01:00
2015-08-21 18:33:50 +01:00
return match;
2017-07-20 14:31:07 +01:00
} catch (err) {
debug(err);
return { service: 'youtube' };
2015-08-20 23:22:57 +01:00
}
2017-07-20 14:31:07 +01:00
}
2015-08-20 23:22:57 +01:00
export function* search(data) {
2017-07-20 14:31:07 +01:00
let query;
2015-08-20 23:22:57 +01:00
const type = data.type;
2017-07-20 14:31:07 +01:00
if (type === 'album') {
query = `${data.artist.name} ${data.name}`;
} else if (type === 'track') {
query = `${data.artist.name} ${data.name}`;
}
2017-07-20 14:31:07 +01:00
const path = `/search?part=snippet&q=${encodeURIComponent(query)}&type=video&videoCaption=any&videoCategoryId=10&key=${credentials.key}`;
2015-08-20 23:22:57 +01:00
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
if (!item) {
2017-07-20 14:31:07 +01:00
return { service: 'youtube', type: 'video' };
2015-08-20 23:22:57 +01:00
}
2017-07-20 14:31:07 +01:00
return {
service: 'youtube',
type: 'video',
id: item.id.videoId,
name: item.snippet.title,
streamUrl: `https://www.youtube.com/watch?v=${item.id.videoId}`,
purchaseUrl: null,
artwork: {
small: item.snippet.thumbnails.medium.url,
large: item.snippet.thumbnails.high.url,
},
};
}
export function parseUrl(url) {
const parsed = parse(url);
const query = querystring.parse(parsed.query);
let id = query.v;
if (!id) {
id = parsed.path.substr(1);
if (!id) {
throw new Error();
}
}
return lookupId(id, 'track');
}
export const id = 'youtube';
export const match = urlMatch;