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

130 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
import querystring from 'querystring';
import moment from 'moment';
import request from 'superagent';
import 'superagent-bluebird-promise';
import { match as urlMatch } from './url';
import freebase from './freebase';
2015-08-21 18:33:50 +01:00
import debuglog from 'debug';
const debug = debuglog('match.audio:youtube');
2015-08-20 23:22:57 +01:00
module.exports.id = 'youtube';
if (!process.env.YOUTUBE_KEY) {
2015-08-20 23:22:57 +01:00
console.warn('YOUTUBE_KEY environment variable not found, deactivating Youtube.');
}
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';
export const match = urlMatch;
2015-08-20 23:22:57 +01:00
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();
}
}
2015-08-20 23:22:57 +01:00
return lookupId(id, 'track');
}
2015-08-20 23:22:57 +01:00
export function* lookupId(id, type) {
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];
if (!item.topicDetails.topicIds) {
return {service: 'youtube'};
}
2015-08-20 23:22:57 +01:00
2015-08-21 18:33:50 +01:00
const match = {
id: id,
service: 'youtube',
name: item.snippet.title,
type: 'track',
album: {name: ''},
streamUrl: 'https://youtu.be/' + id,
purchaseUrl: null,
artwork: {
small: item.snippet.thumbnails.medium.url,
large: item.snippet.thumbnails.high.url,
}
};
2015-08-20 23:22:57 +01:00
2015-08-21 18:33:50 +01:00
for (let topic of yield freebase.get(topicId)) {
const musicalArtist = topic.property['/type/object/type'].values.some((value) => {
return value.text == 'Musical Artist';
});
const musicalRecording = topic.property['/type/object/type'].values.some(function(value) {
return value.text == 'Musical Recording';
});
const musicalAlbum = topic.property['/type/object/type'].values.some(function(value) {
return value.text == 'Musical Album';
})
if (musicalArtist) {
match.artist = {name: topic.property['/type/object/name'].values[0].text};
} else if (musicalRecording) {
match.name = topic.property['/type/object/name'].values[0].text;
if (topic.property['/music/recording/releases']) {
match.type = 'album';
match.albumName = topic.property['/music/recording/releases'].values[0].text;
2015-08-21 18:33:50 +01:00
}
} else if (musicalAlbum) {
match.name = topic.property['/type/object/name'].values[0].text;
2015-08-20 23:22:57 +01:00
match.type = 'album';
}
}
2015-08-21 18:33:50 +01:00
return match;
} catch (e) {
debug(e.body);
return {'service': 'youtube'};
2015-08-20 23:22:57 +01:00
}
};
2015-08-20 23:22:57 +01:00
export function* search(data) {
let query, album;
const type = data.type;
2015-08-20 23:22:57 +01:00
if (type == 'album') {
query = data.artist.name + ' ' + data.name;
album = data.name;
2015-08-20 23:22:57 +01:00
} else if (type == 'track') {
query = data.artist.name + ' ' + data.name;
album = data.albumName
}
2015-08-20 23:22:57 +01:00
const path = '/search?part=snippet&q=' + encodeURIComponent(query) + '&type=video&videoCaption=any&videoCategoryId=10&key=' + credentials.key;
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
if (!item) {
return {service:'youtube', type: 'video'};
} else {
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,
}
};
}
};