Added can't stream error before adding to queue

This commit is contained in:
RemixDev 2022-08-19 23:49:53 +02:00
parent 59d8222177
commit 12d6cafdd0
No known key found for this signature in database
GPG Key ID: B33962B465BDB51C
3 changed files with 34 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import { v4 as uuidv4 } from 'uuid'
import deemix from 'deemix' import deemix from 'deemix'
import got from 'got' import got from 'got'
import { Settings, Listener } from './types' import { Settings, Listener } from './types'
import { NotLoggedIn } from './helpers/errors' import { NotLoggedIn, CantStream } from './helpers/errors'
import { GUI_PACKAGE } from './helpers/paths' import { GUI_PACKAGE } from './helpers/paths'
import { logger } from './helpers/logger' import { logger } from './helpers/logger'
@ -160,6 +160,11 @@ export class DeemixApp {
async addToQueue(dz: any, url: string[], bitrate: number) { async addToQueue(dz: any, url: string[], bitrate: number) {
if (!dz.logged_in) throw new NotLoggedIn() if (!dz.logged_in) throw new NotLoggedIn()
if (
!this.settings.feelingLucky &&
((!dz.current_user.can_stream_lossless && bitrate === 9) || (!dz.current_user.can_stream_hq && bitrate === 3))
)
throw new CantStream(bitrate)
let downloadObjs: any[] = [] let downloadObjs: any[] = []
const downloadErrors: any[] = [] const downloadErrors: any[] = []

View File

@ -1,3 +1,16 @@
const { TrackFormats } = require('deezer-js')
const bitrateLabels = {
[TrackFormats.MP4_RA3]: '360 HQ',
[TrackFormats.MP4_RA2]: '360 MQ',
[TrackFormats.MP4_RA1]: '360 LQ',
[TrackFormats.FLAC]: 'FLAC',
[TrackFormats.MP3_320]: '320kbps',
[TrackFormats.MP3_128]: '128kbps',
[TrackFormats.DEFAULT]: '128kbps',
[TrackFormats.LOCAL]: 'MP3'
}
export class BadRequestError extends Error { export class BadRequestError extends Error {
constructor() { constructor() {
super() super()
@ -31,3 +44,12 @@ export class NotLoggedIn extends QueueError {
this.name = 'NotLoggedIn' this.name = 'NotLoggedIn'
} }
} }
export class CantStream extends QueueError {
bitrate: number
constructor(bitrate: number) {
super(`Your account can't stream at ${bitrateLabels[bitrate]}.`)
this.name = 'CantStream'
this.bitrate = bitrate
}
}

View File

@ -14,19 +14,22 @@ const handler: ApiHandler['handler'] = async (req, res) => {
const url = req.body.url.split(/[\s;]+/) const url = req.body.url.split(/[\s;]+/)
let bitrate = req.body.bitrate let bitrate = req.body.bitrate
if (bitrate === 'null' || bitrate === null) bitrate = deemix.getSettings().settings.maxBitrate if (bitrate === 'null' || bitrate === null) bitrate = deemix.getSettings().settings.maxBitrate
bitrate = Number(bitrate)
let obj: any let obj: any
try { try {
obj = await deemix.addToQueue(dz, url, bitrate) obj = await deemix.addToQueue(dz, url, bitrate)
} catch (e) { } catch (e) {
res.send({ result: false, errid: e.name, data: { url, bitrate } })
switch (e.name) { switch (e.name) {
case 'NotLoggedIn': case 'NotLoggedIn':
res.send({ result: false, errid: e.name, data: { url, bitrate } }) deemix.listener.send('queueError' + e.name)
deemix.listener.send('loginNeededToDownload') break
case 'CantStream':
deemix.listener.send('queueError' + e.name, e.bitrate)
break break
default: default:
logger.error(e) logger.error(e)
res.send({ result: false, errid: e.name, data: { url, bitrate } })
break break
} }
return return