From a919d80f64c84d36966595c92f61ef7070e7bec7 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sun, 12 Nov 2017 00:33:56 +0000 Subject: [PATCH] Make Youtube matching work a little again --- lib/services/youtube/index.js | 53 ++++++++++++++++++++------------ package.json | 1 + public/src/components/search.vue | 2 +- public/src/views/share.vue | 2 +- routes/search.js | 12 ++++---- routes/share.js | 2 +- worker.js | 2 +- yarn.lock | 4 +++ 8 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/services/youtube/index.js b/lib/services/youtube/index.js index 088a41f..24cd3ef 100644 --- a/lib/services/youtube/index.js +++ b/lib/services/youtube/index.js @@ -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); diff --git a/package.json b/package.json index ccb222e..a86026e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/public/src/components/search.vue b/public/src/components/search.vue index 579c821..13a82c2 100644 --- a/public/src/components/search.vue +++ b/public/src/components/search.vue @@ -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; } diff --git a/public/src/views/share.vue b/public/src/views/share.vue index b6e6e23..1da3ef5 100644 --- a/public/src/views/share.vue +++ b/public/src/views/share.vue @@ -2,7 +2,7 @@