Moved all logging to listener

This commit is contained in:
RemixDev 2021-06-01 20:11:02 +02:00
parent b23189febf
commit 019e730453
5 changed files with 66 additions and 35 deletions

View file

@ -48,9 +48,21 @@ async function streamTrack(outputStream, track, start=0, downloadObject, listene
} }
if (start != 0){ if (start != 0){
let responseRange = response.headers["content-range"] let responseRange = response.headers["content-range"]
console.log(`${itemName} downloading range ${responseRange}`) if (listener) listener.send('downloadInfo', {
uuid: downloadObject.uuid,
itemName,
state: "downloading",
alreadyStarted: true,
value: responseRange
})
}else { }else {
console.log(`${itemName} downloading ${complete} bytes`) if (listener) listener.send('downloadInfo', {
uuid: downloadObject.uuid,
itemName,
state: "downloading",
alreadyStarted: false,
value: complete
})
} }
}).on('data', function(chunk){ }).on('data', function(chunk){
if (downloadObject.isCanceled) { if (downloadObject.isCanceled) {

View file

@ -82,6 +82,31 @@ async function getPreferredBitrate(track, bitrate, shouldFallback, uuid, listene
formats = {...formats_non_360} formats = {...formats_non_360}
} }
async function testBitrate(track, formatNumber, formatName){
let request
try {
request = got.get(
generateStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber),
{ headers: {'User-Agent': USER_AGENT_HEADER}, timeout: 30000 }
).on("response", (response)=>{
track.filesizes[`FILESIZE_${formatName}`] = response.headers["content-length"]
track.filesizes[`FILESIZE_${formatName}_TESTED`] = true
request.cancel()
}).on("error", (e)=>{
throw e
})
await request
} catch (e){
if (e.isCanceled) { return formatNumber }
if (e instanceof got.ReadError || e instanceof got.TimeoutError){
return await testBitrate(track, formatNumber, formatName)
}
console.trace(e)
throw e
}
}
for (let i = 0; i < Object.keys(formats).length; i++){ for (let i = 0; i < Object.keys(formats).length; i++){
let formatNumber = Object.keys(formats).reverse()[i] let formatNumber = Object.keys(formats).reverse()[i]
let formatName = formats[formatNumber] let formatName = formats[formatNumber]
@ -90,25 +115,7 @@ async function getPreferredBitrate(track, bitrate, shouldFallback, uuid, listene
if (Object.keys(track.filesizes).includes(`FILESIZE_${formatName}`)){ if (Object.keys(track.filesizes).includes(`FILESIZE_${formatName}`)){
if (parseInt(track.filesizes[`FILESIZE_${formatName}`]) != 0) return formatNumber if (parseInt(track.filesizes[`FILESIZE_${formatName}`]) != 0) return formatNumber
if (!track.filesizes[`FILESIZE_${formatName}_TESTED`]){ if (!track.filesizes[`FILESIZE_${formatName}_TESTED`]){
let request return await testBitrate(track, formatNumber, formatName)
try {
request = got.get(
generateStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber),
{ headers: {'User-Agent': USER_AGENT_HEADER}, timeout: 30000 }
).on("response", (response)=>{
track.filesizes[`FILESIZE_${formatName}`] = response.headers["content-length"]
track.filesizes[`FILESIZE_${formatName}_TESTED`] = true
request.cancel()
}).on("error", (e)=>{
throw e
})
await request
} catch (e){
if (e.isCanceled) { return formatNumber }
console.error(e)
throw e
}
} }
} }
@ -147,6 +154,16 @@ class Downloader {
this.playlistURLs = [] this.playlistURLs = []
} }
log(itemName, state){
if (this.listener)
this.listener.send('downloadInfo', { uuid: this.downloadObject.uuid, itemName, state })
}
warn(itemName, state, solution){
if (this.listener)
this.listener.send('downloadWarn', { uuid: this.downloadObject.uuid, itemName, state , solution })
}
async start(){ async start(){
if (!this.downloadObject.isCanceled){ if (!this.downloadObject.isCanceled){
if (this.downloadObject.__type__ === "Single"){ if (this.downloadObject.__type__ === "Single"){
@ -198,7 +215,7 @@ class Downloader {
// Generate track object // Generate track object
if (!track){ if (!track){
track = new Track() track = new Track()
console.log(`${itemName} Getting tags`) this.log(itemName, "getTags")
try{ try{
await track.parseData( await track.parseData(
this.dz, this.dz,
@ -212,9 +229,10 @@ class Downloader {
} catch (e){ } catch (e){
if (e.name === "AlbumDoesntExists") { throw new DownloadFailed('albumDoesntExists') } if (e.name === "AlbumDoesntExists") { throw new DownloadFailed('albumDoesntExists') }
if (e.name === "MD5NotFound") { throw new DownloadFailed('notLoggedIn') } if (e.name === "MD5NotFound") { throw new DownloadFailed('notLoggedIn') }
console.error(e) console.trace(e)
throw e throw e
} }
this.log(itemName, "gotTags")
} }
if (this.downloadObject.isCanceled) throw new DownloadCanceled if (this.downloadObject.isCanceled) throw new DownloadCanceled
@ -224,7 +242,7 @@ class Downloader {
if (track.MD5 === "") throw new DownloadFailed("notEncoded", track) if (track.MD5 === "") throw new DownloadFailed("notEncoded", track)
// Check the target bitrate // Check the target bitrate
console.log(`${itemName} Getting bitrate`) this.log(itemName, "getBitrate")
let selectedFormat let selectedFormat
try{ try{
selectedFormat = await getPreferredBitrate( selectedFormat = await getPreferredBitrate(
@ -236,11 +254,12 @@ class Downloader {
}catch (e){ }catch (e){
if (e.name === "PreferredBitrateNotFound") { throw new DownloadFailed("wrongBitrate", track) } if (e.name === "PreferredBitrateNotFound") { throw new DownloadFailed("wrongBitrate", track) }
if (e.name === "TrackNot360") { throw new DownloadFailed("no360RA") } if (e.name === "TrackNot360") { throw new DownloadFailed("no360RA") }
console.error(e) console.trace(e)
throw e throw e
} }
track.bitrate = selectedFormat track.bitrate = selectedFormat
track.album.bitrate = selectedFormat track.album.bitrate = selectedFormat
this.log(itemName, "gotBitrate")
// Apply Settings // Apply Settings
track.applySettings(this.settings) track.applySettings(this.settings)
@ -273,8 +292,9 @@ class Downloader {
track.album.embeddedCoverPath = `${TEMPDIR}/${track.album.isPlaylist ? 'pl'+track.playlist.id : 'alb'+track.album.id}_${this.settings.embeddedArtworkSize}${ext}` track.album.embeddedCoverPath = `${TEMPDIR}/${track.album.isPlaylist ? 'pl'+track.playlist.id : 'alb'+track.album.id}_${this.settings.embeddedArtworkSize}${ext}`
// Download and cache the coverart // Download and cache the coverart
this.log(itemName, "getAlbumArt")
track.album.embeddedCoverPath = await downloadImage(track.album.embeddedCoverURL, track.album.embeddedCoverPath) track.album.embeddedCoverPath = await downloadImage(track.album.embeddedCoverURL, track.album.embeddedCoverPath)
console.log(`${itemName} Albumart downloaded`) this.log(itemName, "gotAlbumArt")
// Save local album art // Save local album art
if (coverPath){ if (coverPath){
@ -366,7 +386,6 @@ class Downloader {
// Download the track // Download the track
if (!trackAlreadyDownloaded || this.settings.overwriteFile == OverwriteOption.OVERWRITE){ if (!trackAlreadyDownloaded || this.settings.overwriteFile == OverwriteOption.OVERWRITE){
console.log(`${itemName} Downloading`)
track.downloadURL = generateStreamURL(track.id, track.MD5, track.mediaVersion, track.bitrate) track.downloadURL = generateStreamURL(track.id, track.MD5, track.mediaVersion, track.bitrate)
let stream = fs.createWriteStream(writepath) let stream = fs.createWriteStream(writepath)
try { try {
@ -376,20 +395,22 @@ class Downloader {
if (e instanceof got.HTTPError) throw new DownloadFailed('notAvailable', track) if (e instanceof got.HTTPError) throw new DownloadFailed('notAvailable', track)
throw e throw e
} }
this.log(itemName, "downloaded")
} else { } else {
console.log(`${itemName} Skipping track as it's already downloaded`) this.log(itemName, "alreadyDownloaded")
this.downloadObject.completeTrackProgress(this.listener) this.downloadObject.completeTrackProgress(this.listener)
} }
// Adding tags // Adding tags
if (!trackAlreadyDownloaded || [OverwriteOption.ONLY_TAGS, OverwriteOption.OVERWRITE].includes(this.settings.overwriteFile) && !track.local){ if (!trackAlreadyDownloaded || [OverwriteOption.ONLY_TAGS, OverwriteOption.OVERWRITE].includes(this.settings.overwriteFile) && !track.local){
console.log(`${itemName} Tagging file`) this.log(itemName, "tagging")
if (extension == '.mp3'){ if (extension == '.mp3'){
tagID3(writepath, track, this.settings.tags) tagID3(writepath, track, this.settings.tags)
if (this.settings.tags.saveID3v1) tagID3v1(writepath, track, this.settings.tags) if (this.settings.tags.saveID3v1) tagID3v1(writepath, track, this.settings.tags)
} else if (extension == '.flac'){ } else if (extension == '.flac'){
tagFLAC(writepath, track, this.settings.tags) tagFLAC(writepath, track, this.settings.tags)
} }
this.log(itemName, "tagged")
} }
if (track.searched) returnData.searched = true if (track.searched) returnData.searched = true
@ -431,14 +452,14 @@ class Downloader {
if (e.track){ if (e.track){
let track = e.track let track = e.track
if (track.fallbackID != 0){ if (track.fallbackID != 0){
console.warn(`${itemName} ${e.message} Using fallback id.`) this.warn(itemName, e.errid, 'fallback')
let newTrack = await this.dz.gw.get_track_with_fallback(track.fallbackID) let newTrack = await this.dz.gw.get_track_with_fallback(track.fallbackID)
track.parseEssentialData(newTrack) track.parseEssentialData(newTrack)
track.retriveFilesizes(this.dz) track.retriveFilesizes(this.dz)
return await this.downloadWrapper(extraData, track) return await this.downloadWrapper(extraData, track)
} }
if (!track.searched && this.settings.fallbackSearch){ if (!track.searched && this.settings.fallbackSearch){
console.warn(`${itemName} ${e.message} Searching for alternative.`) this.warn(itemName, e.errid, 'search')
let searchedID = this.dz.api.get_track_id_from_metadata(track.mainArtist.name, track.title, track.album.title) let searchedID = this.dz.api.get_track_id_from_metadata(track.mainArtist.name, track.title, track.album.title)
if (searchedID != "0"){ if (searchedID != "0"){
let newTrack = await this.dz.gw.get_track_with_fallback(track.fallbackID) let newTrack = await this.dz.gw.get_track_with_fallback(track.fallbackID)
@ -460,14 +481,12 @@ class Downloader {
e.errid += "NoAlternative" e.errid += "NoAlternative"
e.message = errorMessages[e.errid] e.message = errorMessages[e.errid]
} }
console.error(`${itemName} ${e.message}`)
result = {error:{ result = {error:{
message: e.message, message: e.message,
errid: e.errid, errid: e.errid,
data: tempTrack data: tempTrack
}} }}
} else if (! (e instanceof DownloadCanceled)){ } else if (! (e instanceof DownloadCanceled)){
console.error(`${itemName} ${e.message}`)
console.trace(e) console.trace(e)
result = {error:{ result = {error:{
message: e.message, message: e.message,

View file

@ -292,6 +292,7 @@ class Spotify extends Plugin {
downloadObject.collection.tracks_gw = collection downloadObject.collection.tracks_gw = collection
downloadObject.size = collection.length downloadObject.size = collection.length
downloadObject = new Collection(downloadObject.toDict()) downloadObject = new Collection(downloadObject.toDict())
if (listener) listener.send("finishConversion", downloadObject.getSlimmedDict())
fs.writeFileSync(this.configFolder+'cache.json', JSON.stringify(cache)) fs.writeFileSync(this.configFolder+'cache.json', JSON.stringify(cache))
return downloadObject return downloadObject

View file

@ -87,7 +87,7 @@ class Track {
timeout: 30000 timeout: 30000
}).json() }).json()
}catch (e){ }catch (e){
console.error(e) console.error("Track.retriveFilesizes: ", e.message)
await new Promise(r => setTimeout(r, 2000)) // sleep(2000ms) await new Promise(r => setTimeout(r, 2000)) // sleep(2000ms)
return this.retriveFilesizes(dz) return this.retriveFilesizes(dz)
} }

View file

@ -18,7 +18,6 @@ async function getAccessToken(email, password){
}, },
headers: {"User-Agent": USER_AGENT_HEADER} headers: {"User-Agent": USER_AGENT_HEADER}
}).json() }).json()
console.log(response)
return response.access_token return response.access_token
} }