From 06305de9cf65639620eeaee408f1c64cb8610387 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 19 Aug 2022 23:50:05 +0200 Subject: [PATCH] Added retry function --- server/src/app.ts | 4 +- server/src/routes/api/post/index.ts | 4 +- server/src/routes/api/post/retryDownload.ts | 56 +++++++++++++++++++++ webui | 2 +- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 server/src/routes/api/post/retryDownload.ts diff --git a/server/src/app.ts b/server/src/app.ts index 0a5d3fc..0ee763b 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -158,7 +158,7 @@ export class DeemixApp { return result } - async addToQueue(dz: any, url: string[], bitrate: number) { + async addToQueue(dz: any, url: string[], bitrate: number, retry: boolean = false) { if (!dz.logged_in) throw new NotLoggedIn() if ( !this.settings.feelingLucky && @@ -204,7 +204,7 @@ export class DeemixApp { downloadObjs.forEach((downloadObj: any) => { // Check if element is already in queue - if (Object.keys(this.queue).includes(downloadObj.uuid)) { + if (Object.keys(this.queue).includes(downloadObj.uuid) && !retry) { this.listener.send('alreadyInQueue', downloadObj.getEssentialDict()) return } diff --git a/server/src/routes/api/post/index.ts b/server/src/routes/api/post/index.ts index 5d88377..4eab679 100644 --- a/server/src/routes/api/post/index.ts +++ b/server/src/routes/api/post/index.ts @@ -7,6 +7,7 @@ import removeFinishedDownloads from './removeFinishedDownloads' import removeFromQueue from './removeFromQueue' import logout from './logout' import saveSettings from './saveSettings' +import retryDownload from './retryDownload' export default [ changeAccount, @@ -17,5 +18,6 @@ export default [ removeFinishedDownloads, removeFromQueue, logout, - saveSettings + saveSettings, + retryDownload ] diff --git a/server/src/routes/api/post/retryDownload.ts b/server/src/routes/api/post/retryDownload.ts new file mode 100644 index 0000000..6bed9db --- /dev/null +++ b/server/src/routes/api/post/retryDownload.ts @@ -0,0 +1,56 @@ +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' +import { sessionDZ } from '../../../app' +import { logger } from '../../../helpers/logger' + +const path: ApiHandler['path'] = '/retryDownload' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + const deemix = req.app.get('deemix') + const dz = sessionDZ[req.session.id] + + const uuid = req.body.uuid + const data = uuid.split('_') + let url = '' + let bitrate = 0 + if (data.lenght === 4) { + if (data[0] === 'spotify') { + url = `https://open.spotify.com/${data[1]}/${data[2]}` + bitrate = Number(data[3]) + } + } else { + if (data[0] === 'playlist' && data[1].endsWith('_top_track')) { + data[0] = 'artist' + data[1] = data[1].replace('_top_track', '/top_track') + } + url = `https://www.deezer.com/${data[0]}/${data[1]}` + bitrate = Number(data[2]) + } + let obj: any + + try { + obj = await deemix.addToQueue(dz, [url], bitrate, true) + } catch (e) { + res.send({ result: false, errid: e.name, data: { url, bitrate } }) + switch (e.name) { + case 'NotLoggedIn': + deemix.listener.send('queueError' + e.name) + break + case 'CantStream': + deemix.listener.send('queueError' + e.name, e.bitrate) + break + default: + logger.error(e) + break + } + return + } + + res.send({ result: true, data: { url, bitrate, obj } }) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/webui b/webui index 90f52fa..6faf546 160000 --- a/webui +++ b/webui @@ -1 +1 @@ -Subproject commit 90f52faad22e885e3e29139f236a3b62d019012b +Subproject commit 6faf5464ad85fd6af58f311500c3448fa2a7c884