Make Youtube matching work a little again

This commit is contained in:
Jonathan Cremin 2017-11-12 00:33:56 +00:00
parent f757152c7d
commit a919d80f64
8 changed files with 49 additions and 29 deletions

View file

@ -2,6 +2,7 @@ import { parse } from 'url';
import querystring from 'querystring';
import request from 'superagent';
import Nodebrainz from 'nodebrainz';
import { toSeconds, parse as ptParse } from 'iso8601-duration';
import 'superagent-bluebird-promise';
import debuglog from 'debug';
import urlMatch from './url';
@ -18,40 +19,54 @@ const credentials = {
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,
});
// const nodebrainz = new Nodebrainz({
// userAgent: 'combine.fm ( https://combine.fm )',
// defaultLimit: 10,
// retryOn: true,
// retryDelay: 3000,
// retryCount: 10,
// });
export function* lookupId(id) {
const path = `/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=${id}&key=${credentials.key}`;
const path = `/videos?part=snippet%2CcontentDetails&id=${id}&key=${credentials.key}`;
try {
const result = yield request.get(apiRoot + path).promise();
const item = result.body.items[0];
const item = result.body.items[0].snippet;
nodebrainz.luceneSearch('release', { query: item.snippet.title }, (err, response) => {
response.releases.forEach((release) => {
//console.log(release);
});
});
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;
// });
const match = {
id,
type: 'album',
service: 'youtube',
name: item.snippet.title,
type: 'track',
album: { name: '' },
name,
artist: { name: artist },
streamUrl: `https://youtu.be/${id}`,
purchaseUrl: null,
artwork: {
small: item.snippet.thumbnails.medium.url,
large: item.snippet.thumbnails.high.url,
small: item.thumbnails.medium.url,
large: item.thumbnails.high.url,
},
};
// Hacky check whether this is long enough to be an album
if (duration < 1200) {
match.type = 'track';
match.album = { name: '' };
}
return match;
} catch (err) {
debug(err);

View file

@ -58,6 +58,7 @@
"ejs": "^2.5.7",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"iso8601-duration": "^1.1.1",
"json-loader": "^0.5.4",
"kcors": "^1.0.1",
"koa": "^1.2.1",

View file

@ -38,7 +38,7 @@ export default {
this.submitting = false;
if (res.status == 200) {
const item = res.body;
this.$router.push(`/${item.service}/${item.albumName ? 'track' : 'album'}/${item.externalId}`);
this.$router.push(`/${item.service}/${item.type}/${item.externalId}`);
} else {
this.error = res.body.message;
}

View file

@ -2,7 +2,7 @@
<div class="container" v-if="item.name">
<social v-bind:name="item.name" v-bind:artist="item.artist.name" v-bind:url="`https://combine.fm/${item.service}/${item.type}/${item.externalId}`"></social>
<div class="share-heading">
<h3 class="title is-3">Matched {{ item.albumName ? 'tracks' : 'albums' }} for</h3>
<h3 class="title is-3">Matched {{ item.type === 'track' ? 'tracks' : 'albums' }} for</h3>
<h2 class="title is-2"><strong>{{ item.name }}</strong> - {{ item.artist.name }}</h2>
</div>
<ul class="columns is-multiline">

View file

@ -13,11 +13,11 @@ const queue = kue.createQueue({
});
export default function* () {
const url = parse(this.request.body.url);
debug(`URL ${url.href}`);
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
try {
const url = parse(this.request.body.url);
debug(`URL ${url.href}`);
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
const music = yield lookup(this.request.body.url);
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
@ -29,7 +29,7 @@ export default function* () {
services.forEach((service) => {
if (service.id !== share.service) {
const job = queue.create('search', { share, service })
const job = queue.create('search', { title: `Matching ${share.name} on ${service.id}`, share, service })
.attempts(3)
.backoff({ type: 'exponential' })
.save((err) => {
@ -58,7 +58,7 @@ export default function* () {
this.body = share;
} catch (e) {
debug(e);
this.throw(500, { error: { message: 'Unexpected error looking up music. Please try again later.' } });
this.throw(400, { error: { message: 'Unexpected error looking up music. Please try again later.' } });
throw e;
}
}

View file

@ -38,7 +38,7 @@ export default function* (serviceId, type, itemId, format) {
services.forEach((service) => {
if (service.id !== share.service) {
const job = queue.create('search', { share, service })
const job = queue.create('search', { title: `Matching ${share.name} on ${service.id}`, share, service })
.attempts(3)
.backoff({ type: 'exponential' })
.save((err) => {

View file

@ -64,7 +64,7 @@ function search(data, done) {
});
}
queue.process('search', (job, done) => {
queue.process('search', 5, (job, done) => {
search(job.data, done);
});

View file

@ -3071,6 +3071,10 @@ isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
iso8601-duration@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/iso8601-duration/-/iso8601-duration-1.1.1.tgz#236379eaf0dd00255377fd73a6d487b34f91f915"
isobject@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/isobject/-/isobject-2.1.0.tgz#f065561096a3f1da2ef46272f815c840d87e0c89"