add expiry time to in-memory external track files

This commit is contained in:
Evert Prants 2019-02-07 20:04:15 +02:00
parent e092f835d5
commit 6bcc874243
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
1 changed files with 9 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import crypto from 'crypto'
const fs = require('fs').promises
const values = require(path.join(process.cwd(), 'values.json'))
const memexpire = 1800
let externalTracks = {}
let downloadQueue = []
@ -37,7 +38,11 @@ async function downloadLocally (id) {
async function getTrackMetaReal (id) {
if (!id || !externalTracks[id]) return null
let trdata = externalTracks[id]
if (trdata.file) return Object.assign({}, trdata)
// Check for expiry
if (trdata.file && trdata.expires > Date.now()) {
return Object.assign({}, trdata)
}
let trsrch = 'ytsearch1:' + trdata.artist + ' - ' + trdata.title
let dldata = await dl.getVideoInfo(trsrch)
@ -48,6 +53,7 @@ async function getTrackMetaReal (id) {
artist: trdata.artist,
file: dldata.url,
duration: dldata.duration,
expires: Date.now() + memexpire * 1000,
external: true
}
@ -76,7 +82,8 @@ async function search (track, limit = 30) {
id: createHash(res),
artist: res.artist,
title: res.name,
external: true
external: true,
mbid: res.mbid
}
if (externalTracks[clean.id]) {