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

123 lines
3.2 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';
import { toSeconds, parse as ptParse } from 'iso8601-duration';
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';
// const nodebrainz = new Nodebrainz({
// userAgent: 'combine.fm ( https://combine.fm )',
// defaultLimit: 10,
// retryOn: true,
// retryDelay: 3000,
// retryCount: 10,
// });
2018-04-14 00:18:48 +01:00
export async function lookupId(id) {
const path = `/videos?part=snippet%2CcontentDetails&id=${id}&key=${credentials.key}`;
2015-08-21 18:33:50 +01:00
try {
2018-04-14 00:18:48 +01:00
const result = await request.get(apiRoot + path);
const item = result.body.items[0].snippet;
2017-07-20 14:31:07 +01:00
const duration = toSeconds(ptParse(result.body.items[0].contentDetails.duration));
const split = item.title.match(/([^-]+)-(.*)/);
const artist = split[1].trim();
const name = split[2].match(/^[^([]+/)[0].trim();
// nodebrainz.luceneSearch('recording', {artist, query: release }, (err, response) => {
// const recording = response.recordings[0];
// debug(recording.releases[0].media[0].track[0].title)
// artist = recording['artist-credit'].name;
// release = recording.media;
// });
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,
type: 'album',
2015-08-21 18:33:50 +01:00
service: 'youtube',
name,
artist: { name: artist },
2017-07-20 14:31:07 +01:00
streamUrl: `https://youtu.be/${id}`,
2015-08-21 18:33:50 +01:00
purchaseUrl: null,
artwork: {
small: item.thumbnails.medium.url,
large: item.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
// Hacky check whether this is long enough to be an album
if (duration < 1200) {
match.type = 'track';
match.album = { name: '' };
}
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
}
2018-04-14 00:18:48 +01:00
export async 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}`;
2018-04-14 00:18:48 +01:00
const result = await request.get(apiRoot + path);
2015-08-20 23:22:57 +01:00
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;