From e8274a1a65df0ff94ba421560571a8b6a84f925d Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 4 Nov 2019 22:51:10 +0200 Subject: [PATCH] Better youtube video prediction --- src/common/download.js | 20 +++++++++++++++++++- src/lastfm.js | 39 ++++++++++++++++++++++++++++++++++++--- src/server.js | 2 +- src/user.js | 8 +++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/common/download.js b/src/common/download.js index c8d6393..1cde48e 100644 --- a/src/common/download.js +++ b/src/common/download.js @@ -39,7 +39,7 @@ function parseTitle (data) { } function getVideoInfo (arg) { - let yt = spawn('youtube-dl', ['--no-playlist', '--playlist-end', 1, '-j', '-f', 'bestaudio/best', arg]) + let yt = spawn('youtube-dl', ['--no-playlist', '-j', '-f', 'bestaudio/best', arg]) let output = '' yt.stdout.on('data', function (chunk) { @@ -48,6 +48,24 @@ function getVideoInfo (arg) { return new Promise((resolve, reject) => { yt.on('close', function () { + let ftdata = output.split('\n') + console.log(ftdata.length) + if (ftdata.length > 1) { + let composite = [] + for (let i in ftdata) { + let dat + try { + dat = JSON.parse(ftdata[i]) + } catch (e) { + console.error(e) + continue + } + delete dat.formats + composite.push(dat) + } + return resolve(composite) + } + let data = JSON.parse(output) delete data.formats resolve(data) diff --git a/src/lastfm.js b/src/lastfm.js index 8a8a716..eed1602 100644 --- a/src/lastfm.js +++ b/src/lastfm.js @@ -44,15 +44,48 @@ async function getTrackMetaReal (id) { return Object.assign({}, trdata) } - let trsrch = 'ytsearch1:' + trdata.artist + ' - ' + trdata.title + let trsrch = 'ytsearch3:' + trdata.artist + ' - ' + trdata.title let dldata = await dl.getVideoInfo(trsrch) + let bestMatch + if (dldata.length === 1) bestMatch = dldata[0] + + let candidates = [] + for (let i in dldata) { + let obj = dldata[i] + let title = obj.title.toLowerCase() + + // Skip any video with 'video' in it, but keep lyric videos + if (title.indexOf('video') !== -1 && title.indexOf('lyric') === -1) continue + + // If the title has 'audio' in it, it might be the best match + if (title.indexOf('audio') !== -1) { + bestMatch = obj + break + } + + candidates.push(obj) + } + + if (candidates.length) { + // Sort candidates by view count + candidates = candidates.sort(function (a, b) { + return b.view_count - a.view_count + }) + + // Select the one with the most views + bestMatch = candidates[0] + } + + // If there were no suitable candidates, just take the first response + if (!candidates.length) bestMatch = dldata[0] + externalTracks[id] = { id: trdata.id, title: trdata.title, artist: trdata.artist, - file: dldata.url, - duration: dldata.duration, + file: bestMatch.url, + duration: bestMatch.duration, expires: Date.now() + memexpire * 1000, external: true } diff --git a/src/server.js b/src/server.js index 57f0cdf..daf3ebf 100644 --- a/src/server.js +++ b/src/server.js @@ -185,7 +185,7 @@ router.get('/track/:id', userMiddleware, async (req, res, next) => { router.get('/playlists', userMiddleware, async (req, res, next) => { let db = await dbPromise - let playlists = await db.all('SELECT * FROM Playlist') + let playlists = await db.all('SELECT * FROM Playlist WHERE userId = ? OR userId = NULL', req.session.user) res.jsonp(playlists) }) diff --git a/src/user.js b/src/user.js index ec399fa..017d96d 100644 --- a/src/user.js +++ b/src/user.js @@ -87,8 +87,14 @@ export function user (dbPromise, oauth, registrations) { if (!registrations) throw new Error('Registrations are currently closed!') // Create a new user and log in - let newU = await db.get('INSERT INTO User (username,email,image,created) VALUES (?,?,?,?)', userInfo.username, userInfo.email, userInfo.image, new Date()) + await db.run('INSERT INTO User (username,email,image,created) VALUES (?,?,?,?)', userInfo.username, userInfo.email, userInfo.image, new Date()) + + let newU = await db.get('SELECT * FROM User WHERE username = ?', userInfo.username) + + if (!newU) throw new Error('Something went wrong!') + await db.run('INSERT INTO OAuth (userId,remoteId,created) VALUES (?,?,?)', newU.id, userInfo.id, new Date()) + req.session.user = newU.id res.redirect('/') })