Updated getPreferredBitrate and added feelingLucky option

feelingLucky tries to use the old cdns to get the tracks, if disabled 
there will be less tracks, but less calls overall
This commit is contained in:
RemixDev 2021-12-23 14:41:30 +01:00
parent f7710975c2
commit ffba5ae7bb
No known key found for this signature in database
GPG key ID: B33962B465BDB51C
3 changed files with 20 additions and 18 deletions

View file

@ -8,6 +8,7 @@ const { generatePath, generateAlbumName, generateArtistName, generateDownloadObj
const { PreferredBitrateNotFound, TrackNot360, DownloadFailed, ErrorMessages, DownloadCanceled} = require('./errors.js') const { PreferredBitrateNotFound, TrackNot360, DownloadFailed, ErrorMessages, DownloadCanceled} = require('./errors.js')
const { TrackFormats } = require('deezer-js') const { TrackFormats } = require('deezer-js')
const { WrongLicense, WrongGeolocation } = require('deezer-js').errors const { WrongLicense, WrongGeolocation } = require('deezer-js').errors
const { map_track } = require('deezer-js').utils
const got = require('got') const got = require('got')
const fs = require('fs') const fs = require('fs')
const { tmpdir } = require('os') const { tmpdir } = require('os')
@ -72,7 +73,7 @@ async function downloadImage(url, path, overwrite = OverwriteOption.DONT_OVERWRI
return path return path
} }
async function getPreferredBitrate(dz, track, preferredBitrate, shouldFallback, uuid, listener){ async function getPreferredBitrate(dz, track, preferredBitrate, shouldFallback, feelingLucky, uuid, listener){
preferredBitrate = parseInt(preferredBitrate) preferredBitrate = parseInt(preferredBitrate)
let falledBack = false let falledBack = false
@ -88,15 +89,14 @@ async function getPreferredBitrate(dz, track, preferredBitrate, shouldFallback,
url, url,
{ headers: {'User-Agent': USER_AGENT_HEADER}, timeout: 30000, https: {rejectUnauthorized: false} } { headers: {'User-Agent': USER_AGENT_HEADER}, timeout: 30000, https: {rejectUnauthorized: false} }
).on("response", (response)=>{ ).on("response", (response)=>{
track.filesizes[`FILESIZE_${formatName}`] = response.statusCode == 403 ? 0 : response.headers["content-length"] track.filesizes[`${formatName.toLowerCase()}`] = response.statusCode == 403 ? 0 : response.headers["content-length"]
track.filesizes[`FILESIZE_${formatName}_TESTED`] = true
request.cancel() request.cancel()
}) })
await request await request
} catch (e){ } catch (e){
if (e.isCanceled) { if (e.isCanceled) {
if (track.filesizes[`FILESIZE_${formatName}`] == 0) return false if (track.filesizes[`${formatName.toLowerCase()}`] == 0) return false
return true return true
} }
if (e instanceof got.ReadError || e instanceof got.TimeoutError){ if (e instanceof got.ReadError || e instanceof got.TimeoutError){
@ -108,19 +108,19 @@ async function getPreferredBitrate(dz, track, preferredBitrate, shouldFallback,
} }
} }
async function getCorrectURL(track, formatName, formatNumber){ async function getCorrectURL(track, formatName, formatNumber, feelingLucky){
// Check the track with the legit method // Check the track with the legit method
let url let url
try { if (track.filesizes[`${formatName.toLowerCase()}`] && track.filesizes[`${formatName.toLowerCase()}`] != "0"){
url = await dz.get_track_url(track.trackToken, formatName) try {
if (await testURL(track, url, formatName, formatNumber)) return url url = await dz.get_track_url(track.trackToken, formatName)
url = undefined } catch (e){
} catch (e){ wrongLicense = (e.name === "WrongLicense")
wrongLicense = (e.name === "WrongLicense") isGeolocked = (e.name === "WrongGeolocation")
isGeolocked = (e.name === "WrongGeolocation") }
} }
// Fallback to old method // Fallback to old method
if (!url){ if (!url && feelingLucky){
url = generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber) url = generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber)
if (await testURL(track, url, formatName, formatNumber)) return url if (await testURL(track, url, formatName, formatNumber)) return url
url = undefined url = undefined
@ -164,16 +164,17 @@ async function getPreferredBitrate(dz, track, preferredBitrate, shouldFallback,
if (formatNumber > preferredBitrate) { continue } if (formatNumber > preferredBitrate) { continue }
let currentTrack = track let currentTrack = track
let url = await getCorrectURL(currentTrack, formatName, formatNumber) let url = await getCorrectURL(currentTrack, formatName, formatNumber, feelingLucky)
let newTrack let newTrack
do { do {
if (!url && hasAlternative){ if (!url && hasAlternative){
newTrack = await dz.gw.get_track_with_fallback(currentTrack.fallbackID) newTrack = await dz.gw.get_track_with_fallback(currentTrack.fallbackID)
newTrack = map_track(newTrack)
currentTrack = new Track() currentTrack = new Track()
currentTrack.parseEssentialData(newTrack) currentTrack.parseEssentialData(newTrack)
hasAlternative = currentTrack.fallbackID != 0 hasAlternative = currentTrack.fallbackID != 0
} }
if (!url) url = await getCorrectURL(currentTrack, formatName, formatNumber) if (!url) url = await getCorrectURL(currentTrack, formatName, formatNumber, feelingLucky)
} while (!url && hasAlternative) } while (!url && hasAlternative)
if (url) { if (url) {
@ -293,9 +294,7 @@ class Downloader {
await track.parseData( await track.parseData(
this.dz, this.dz,
trackAPI.id, trackAPI.id,
null, // trackAPI_gw
trackAPI, trackAPI,
null, // albumAPI_gw
albumAPI, albumAPI,
playlistAPI playlistAPI
) )
@ -326,7 +325,7 @@ class Downloader {
this.dz, this.dz,
track, track,
this.bitrate, this.bitrate,
this.settings.fallbackBitrate, this.settings.fallbackBitrate, this.settings.feelingLucky,
this.downloadObject.uuid, this.listener this.downloadObject.uuid, this.listener
) )
}catch (e){ }catch (e){

View file

@ -38,6 +38,7 @@ const DEFAULTS = {
illegalCharacterReplacer: "_", illegalCharacterReplacer: "_",
queueConcurrency: 3, queueConcurrency: 3,
maxBitrate: String(TrackFormats.MP3_320), maxBitrate: String(TrackFormats.MP3_320),
feelingLucky: false,
fallbackBitrate: false, fallbackBitrate: false,
fallbackSearch: false, fallbackSearch: false,
logErrors: true, logErrors: true,

View file

@ -51,6 +51,7 @@ class Track {
this.artistsString = "" this.artistsString = ""
this.mainArtistsString = "" this.mainArtistsString = ""
this.featArtistsString = "" this.featArtistsString = ""
this.urls = {}
} }
parseEssentialData(trackAPI){ parseEssentialData(trackAPI){
@ -64,6 +65,7 @@ class Track {
this.fallbackID = "0" this.fallbackID = "0"
if (trackAPI.fallback_id) this.fallbackID = trackAPI.fallback_id if (trackAPI.fallback_id) this.fallbackID = trackAPI.fallback_id
this.localTrack = parseInt(this.id) < 0 this.localTrack = parseInt(this.id) < 0
this.urls = {}
} }
async parseData(dz, id, trackAPI, albumAPI, playlistAPI){ async parseData(dz, id, trackAPI, albumAPI, playlistAPI){