Better youtube video prediction

This commit is contained in:
Evert Prants 2019-11-04 22:51:10 +02:00
parent a200575bd8
commit e8274a1a65
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 63 additions and 6 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)
})

View File

@ -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('/')
})