Fix Google Play Music and Spotify

This commit is contained in:
Jonathan Cremin 2017-07-20 14:31:07 +01:00
parent 688ec0f2f9
commit 6c29d50f1e
21 changed files with 566 additions and 559 deletions

View file

@ -1,15 +0,0 @@
import { parse } from 'url';
import querystring from 'querystring';
import request from 'superagent';
import 'superagent-bluebird-promise';
const credentials = {
key: process.env.YOUTUBE_KEY,
};
const apiRoot = 'https://www.googleapis.com/freebase/v1/topic';
export function* get(topic) {
const result = yield request.get(apiRoot + topic + '?key=' + credentials.key).promise();
return result.body;
}

View file

@ -1,18 +1,15 @@
import { parse } from 'url';
import querystring from 'querystring';
import moment from 'moment';
import request from 'superagent';
import Nodebrainz from 'nodebrainz';
import 'superagent-bluebird-promise';
import { match as urlMatch } from './url';
import freebase from './freebase';
import debuglog from 'debug';
import urlMatch from './url';
const debug = debuglog('match.audio:youtube');
module.exports.id = 'youtube';
if (!process.env.YOUTUBE_KEY) {
console.warn('YOUTUBE_KEY environment variable not found, deactivating Youtube.');
debug('YOUTUBE_KEY environment variable not found, deactivating Youtube.');
}
const credentials = {
@ -21,7 +18,77 @@ const credentials = {
const apiRoot = 'https://www.googleapis.com/youtube/v3';
export const match = urlMatch;
const nodebrainz = new Nodebrainz({
userAgent: 'match-audio ( https://match.audio )',
defaultLimit: 10,
retryOn: true,
retryDelay: 3000,
retryCount: 10,
});
export function* lookupId(id) {
const path = `/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=${id}&key=${credentials.key}`;
try {
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
nodebrainz.luceneSearch('release', { query: item.snippet.title }, (err, response) => {
response.releases.forEach((release) => {
//console.log(release);
});
});
const match = {
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,
},
};
return match;
} catch (err) {
debug(err);
return { service: 'youtube' };
}
}
export function* search(data) {
let query;
const type = data.type;
if (type === 'album') {
query = `${data.artist.name} ${data.name}`;
} else if (type === 'track') {
query = `${data.artist.name} ${data.name}`;
}
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' };
}
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);
@ -37,93 +104,5 @@ export function parseUrl(url) {
return lookupId(id, 'track');
}
export function* lookupId(id, type) {
const path = '/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=' + id + '&key=' + credentials.key;
try {
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
if (!item.topicDetails.topicIds) {
return {service: 'youtube'};
}
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.albumName = topic.property['/music/recording/releases'].values[0].text;
}
} 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'};
}
};
export function* search(data) {
let query, album;
const type = data.type;
if (type == 'album') {
query = data.artist.name + ' ' + data.name;
album = data.name;
} else if (type == 'track') {
query = data.artist.name + ' ' + data.name;
album = data.albumName
}
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,
}
};
}
};
export const id = 'youtube';
export const match = urlMatch;

View file

@ -1,7 +1,7 @@
import { parse } from 'url';
import querystring from 'querystring';
export function* match(url, type) {
export default function match(url) {
const parsed = parse(url);
if (parsed.host.match(/youtu\.be$/)) {
return true;
@ -10,4 +10,4 @@ export function* match(url, type) {
return !!query.v;
}
return false;
};
}