Refactor more, fix and design 404
This commit is contained in:
parent
7de374e00b
commit
8521baa6d9
11 changed files with 223 additions and 138 deletions
|
@ -7,9 +7,9 @@ const credentials = {
|
|||
key: process.env.YOUTUBE_KEY,
|
||||
};
|
||||
|
||||
const apiRoot = "https://www.googleapis.com/freebase/v1/topic";
|
||||
const apiRoot = 'https://www.googleapis.com/freebase/v1/topic';
|
||||
|
||||
export function* get(topic) {
|
||||
const result = yield request.get(apiRoot + topic + "?key=" + credentials.key).promise();
|
||||
const result = yield request.get(apiRoot + topic + '?key=' + credentials.key).promise();
|
||||
return result.body;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ import 'superagent-bluebird-promise';
|
|||
import { match as urlMatch } from './url';
|
||||
import freebase from './freebase';
|
||||
|
||||
import debuglog from 'debug';
|
||||
const debug = debuglog('match.audio:youtube');
|
||||
|
||||
module.exports.id = 'youtube';
|
||||
|
||||
if (!process.env.YOUTUBE_KEY) {
|
||||
|
@ -37,55 +40,58 @@ export function parseUrl(url) {
|
|||
export function* lookupId(id, type) {
|
||||
|
||||
const path = '/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=' + id + '&key=' + credentials.key;
|
||||
|
||||
const result = yield request.get(apiRoot + path).promise();
|
||||
const item = res.body.items[0];
|
||||
if (!item.topicDetails.topicIds) {
|
||||
return {service: 'youtube'};
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
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,
|
||||
try {
|
||||
const result = yield request.get(apiRoot + path).promise();
|
||||
const item = result.body.items[0];
|
||||
if (!item.topicDetails.topicIds) {
|
||||
return {service: 'youtube'};
|
||||
}
|
||||
};
|
||||
|
||||
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.album.name = topic.property['/music/recording/releases'].values[0].text;
|
||||
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,
|
||||
}
|
||||
};
|
||||
|
||||
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.album.name = topic.property['/music/recording/releases'].values[0].text;
|
||||
}
|
||||
} else if (musicalAlbum) {
|
||||
match.name = topic.property['/type/object/name'].values[0].text;
|
||||
match.type = 'album';
|
||||
}
|
||||
} else if (musicalAlbum) {
|
||||
match.name = topic.property['/type/object/name'].values[0].text;
|
||||
match.type = 'album';
|
||||
}
|
||||
return match;
|
||||
} catch (e) {
|
||||
debug(e.body);
|
||||
return {'service': 'youtube'};
|
||||
}
|
||||
return match;
|
||||
};
|
||||
|
||||
export function* search(data) {
|
||||
|
@ -101,7 +107,6 @@ export function* search(data) {
|
|||
}
|
||||
|
||||
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];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue