Better feedback when link is wrong

This commit is contained in:
RemixDev 2021-06-05 18:20:22 +02:00
parent 0e3cc11364
commit 8114f497aa
3 changed files with 100 additions and 41 deletions

View file

@ -5,7 +5,9 @@ const {
generatePlaylistItem, generatePlaylistItem,
generateArtistItem, generateArtistItem,
generateArtistDiscographyItem, generateArtistDiscographyItem,
generateArtistTopItem generateArtistTopItem,
LinkNotSupported,
LinkNotRecognized
} = require('./itemgen.js') } = require('./itemgen.js')
async function parseLink(link){ async function parseLink(link){
@ -58,7 +60,8 @@ async function generateDownloadObject(dz, link, bitrate, plugins={}, listener){
item = await currentPlugin.generateDownloadObject(dz, link, bitrate, listener) item = await currentPlugin.generateDownloadObject(dz, link, bitrate, listener)
if (item) break if (item) break
} }
return item if (item) return item
throw new LinkNotRecognized(link)
} }
switch (link_type) { switch (link_type) {
@ -74,6 +77,8 @@ async function generateDownloadObject(dz, link, bitrate, plugins={}, listener){
return generateArtistDiscographyItem(dz, link_id, bitrate, listener) return generateArtistDiscographyItem(dz, link_id, bitrate, listener)
case 'artist_top': case 'artist_top':
return generateArtistTopItem(dz, link_id, bitrate) return generateArtistTopItem(dz, link_id, bitrate)
default:
throw new LinkNotSupported(link)
} }
} }

View file

@ -12,24 +12,25 @@ async function generateTrackItem(dz, id, bitrate, trackAPI, albumAPI){
try { try {
trackAPI = await dz.api.get_track(id) trackAPI = await dz.api.get_track(id)
} catch (e){ } catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/track/${id}`, e.message)
} }
if (trackAPI.id && trackAPI.title){ if (trackAPI.id && trackAPI.title){
id = trackAPI.id id = trackAPI.id
} else { } else {
throw new ISRCnotOnDeezer() throw new ISRCnotOnDeezer(`https://deezer.com/track/${id}`)
} }
} }
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/track/${id}`)
// Get essential track info // Get essential track info
let trackAPI_gw let trackAPI_gw
try { try {
trackAPI_gw = await dz.gw.get_track_with_fallback(id) trackAPI_gw = await dz.gw.get_track_with_fallback(id)
} catch (e){ } catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/track/${id}`, e.message)
} }
let title = trackAPI_gw.SNG_TITLE.trim() let title = trackAPI_gw.SNG_TITLE.trim()
@ -60,11 +61,12 @@ async function generateAlbumItem(dz, id, bitrate, rootArtist){
try{ try{
albumAPI = await dz.api.get_album(id) albumAPI = await dz.api.get_album(id)
} catch (e){ } catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/album/${id}`, e.message)
} }
if (String(id).startsWith('upc')) { id = albumAPI['id'] } if (String(id).startsWith('upc')) { id = albumAPI['id'] }
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/album/${id}`)
// Get extra info about album // Get extra info about album
// This saves extra api calls when downloading // This saves extra api calls when downloading
@ -117,11 +119,12 @@ async function generateAlbumItem(dz, id, bitrate, rootArtist){
async function generatePlaylistItem(dz, id, bitrate, playlistAPI, playlistTracksAPI){ async function generatePlaylistItem(dz, id, bitrate, playlistAPI, playlistTracksAPI){
if (!playlistAPI){ if (!playlistAPI){
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/playlist/${id}`)
// Get essential playlist info // Get essential playlist info
try{ try{
playlistAPI = await dz.api.get_playlist(id) playlistAPI = await dz.api.get_playlist(id)
}catch (e){ }catch (e){
console.error(e) console.trace(e)
playlistAPI = null playlistAPI = null
} }
// Fallback to gw api if the playlist is private // Fallback to gw api if the playlist is private
@ -130,13 +133,13 @@ async function generatePlaylistItem(dz, id, bitrate, playlistAPI, playlistTracks
let userPlaylist = await dz.gw.get_playlist_page(id) let userPlaylist = await dz.gw.get_playlist_page(id)
playlistAPI = map_user_playlist(userPlaylist['DATA']) playlistAPI = map_user_playlist(userPlaylist['DATA'])
}catch (e){ }catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/playlist/${id}`, e.message)
} }
} }
// Check if private playlist and owner // Check if private playlist and owner
if (!playlistAPI.public && playlistAPI.creator.id != dz.current_user.id){ if (!playlistAPI.public && playlistAPI.creator.id != dz.current_user.id){
throw new NotYourPrivatePlaylist() throw new NotYourPrivatePlaylist(`https://deezer.com/playlist/${id}`)
} }
} }
@ -175,13 +178,14 @@ async function generatePlaylistItem(dz, id, bitrate, playlistAPI, playlistTracks
} }
async function generateArtistItem(dz, id, bitrate, listener){ async function generateArtistItem(dz, id, bitrate, listener){
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/artist/${id}`)
// Get essential artist info // Get essential artist info
let artistAPI let artistAPI
try{ try{
artistAPI = await dz.api.get_artist(id) artistAPI = await dz.api.get_artist(id)
}catch (e){ }catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/artist/${id}`, e.message)
} }
const rootArtist = { const rootArtist = {
@ -208,13 +212,14 @@ async function generateArtistItem(dz, id, bitrate, listener){
} }
async function generateArtistDiscographyItem(dz, id, bitrate, listener){ async function generateArtistDiscographyItem(dz, id, bitrate, listener){
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/artist/${id}/discography`)
// Get essential artist info // Get essential artist info
let artistAPI let artistAPI
try{ try{
artistAPI = await dz.api.get_artist(id) artistAPI = await dz.api.get_artist(id)
}catch (e){ }catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/artist/${id}/discography`, e.message)
} }
const rootArtist = { const rootArtist = {
@ -244,13 +249,14 @@ async function generateArtistDiscographyItem(dz, id, bitrate, listener){
} }
async function generateArtistTopItem(dz, id, bitrate){ async function generateArtistTopItem(dz, id, bitrate){
if (!Number.isInteger(id)) throw new InvalidID(`https://deezer.com/artist/${id}/top_track`)
// Get essential artist info // Get essential artist info
let artistAPI let artistAPI
try{ try{
artistAPI = dz.api.get_artist(id) artistAPI = dz.api.get_artist(id)
}catch (e){ }catch (e){
console.error(e) console.trace(e)
throw new GenerationError(e) throw new GenerationError(`https://deezer.com/artist/${id}/top_track`, e.message)
} }
// Emulate the creation of a playlist // Emulate the creation of a playlist
@ -288,37 +294,66 @@ async function generateArtistTopItem(dz, id, bitrate){
} }
class GenerationError extends Error { class GenerationError extends Error {
constructor(message) { constructor(link, message) {
super(message); super(message)
this.name = "GenerationError"; this.link = link
this.name = "GenerationError"
} }
} }
class ISRCnotOnDeezer extends GenerationError { class ISRCnotOnDeezer extends GenerationError {
constructor(message) { constructor(link) {
super(message); super(link, "Track ISRC is not available on deezer")
this.name = "ISRCnotOnDeezer"; this.name = "ISRCnotOnDeezer"
this.errid = "ISRCnotOnDeezer"
} }
} }
class NotYourPrivatePlaylist extends GenerationError { class NotYourPrivatePlaylist extends GenerationError {
constructor(message) { constructor(link) {
super(message); super(link, "You can't download others private playlists.")
this.name = "NotYourPrivatePlaylist"; this.name = "NotYourPrivatePlaylist"
this.errid = "notYourPrivatePlaylist"
} }
} }
class TrackNotOnDeezer extends GenerationError { class TrackNotOnDeezer extends GenerationError {
constructor(message) { constructor(link) {
super(message); super(link, "Track not found on deezer!")
this.name = "TrackNotOnDeezer"; this.name = "TrackNotOnDeezer"
this.errid = "trackNotOnDeezer"
} }
} }
class AlbumNotOnDeezer extends GenerationError { class AlbumNotOnDeezer extends GenerationError {
constructor(message) { constructor(link) {
super(message); super(link, "Album not found on deezer!")
this.name = "AlbumNotOnDeezer"; this.name = "AlbumNotOnDeezer"
this.errid = "albumNotOnDeezer"
}
}
class InvalidID extends GenerationError {
constructor(link) {
super(link, "Link ID is invalid!")
this.name = "InvalidID"
this.errid = "invalidID"
}
}
class LinkNotSupported extends GenerationError {
constructor(link) {
super(link, "Link is not supported.")
this.name = "LinkNotSupported"
this.errid = "unsupportedURL"
}
}
class LinkNotRecognized extends GenerationError {
constructor(link) {
super(link, "Link is not recognized.")
this.name = "LinkNotRecognized"
this.errid = "invalidURL"
} }
} }
@ -334,5 +369,8 @@ module.exports = {
ISRCnotOnDeezer, ISRCnotOnDeezer,
NotYourPrivatePlaylist, NotYourPrivatePlaylist,
TrackNotOnDeezer, TrackNotOnDeezer,
AlbumNotOnDeezer AlbumNotOnDeezer,
InvalidID,
LinkNotSupported,
LinkNotRecognized
} }

View file

@ -4,7 +4,8 @@ const {
generateTrackItem, generateTrackItem,
generateAlbumItem, generateAlbumItem,
TrackNotOnDeezer, TrackNotOnDeezer,
AlbumNotOnDeezer AlbumNotOnDeezer,
InvalidID
} = require('../itemgen.js') } = require('../itemgen.js')
const { Convertable, Collection } = require('../types/DownloadObjects.js') const { Convertable, Collection } = require('../types/DownloadObjects.js')
const { sep } = require('path') const { sep } = require('path')
@ -83,7 +84,7 @@ class Spotify extends Plugin {
if (track_id !== "0"){ if (track_id !== "0"){
return generateTrackItem(dz, track_id, bitrate, trackAPI) return generateTrackItem(dz, track_id, bitrate, trackAPI)
} else { } else {
throw new TrackNotOnDeezer throw new TrackNotOnDeezer(`https://open.spotify.com/track/${link_id}`)
} }
} }
@ -93,7 +94,7 @@ class Spotify extends Plugin {
if (album_id !== "0"){ if (album_id !== "0"){
return generateAlbumItem(dz, album_id, bitrate) return generateAlbumItem(dz, album_id, bitrate)
} else { } else {
throw new AlbumNotOnDeezer throw new AlbumNotOnDeezer(`https://open.spotify.com/album/${link_id}`)
} }
} }
@ -155,7 +156,12 @@ class Spotify extends Plugin {
if (cache.tracks[track_id]){ if (cache.tracks[track_id]){
cachedTrack = cache.tracks[track_id] cachedTrack = cache.tracks[track_id]
} else { } else {
try{
cachedTrack = await this.sp.getTrack(track_id) cachedTrack = await this.sp.getTrack(track_id)
} catch (e){
if (e.body.error.message === "invalid id") throw new InvalidID(`https://open.spotify.com/track/${track_id}`)
throw e
}
cachedTrack = cachedTrack.body cachedTrack = cachedTrack.body
} }
} }
@ -195,7 +201,12 @@ class Spotify extends Plugin {
if (cache.albums[album_id]){ if (cache.albums[album_id]){
cachedAlbum = cache.albums[album_id] cachedAlbum = cache.albums[album_id]
} else { } else {
try{
cachedAlbum = await this.sp.getAlbum(album_id) cachedAlbum = await this.sp.getAlbum(album_id)
} catch (e){
if (e.body.error.message === "invalid id") throw new InvalidID(`https://open.spotify.com/album/${album_id}`)
throw e
}
cachedAlbum = cachedAlbum.body cachedAlbum = cachedAlbum.body
} }
let dz_id = "0" let dz_id = "0"
@ -247,7 +258,12 @@ class Spotify extends Plugin {
if (cache.tracks[track.id].isrc) trackAPI = await dz.api.get_track_by_ISRC(cache.tracks[track.id].isrc) if (cache.tracks[track.id].isrc) trackAPI = await dz.api.get_track_by_ISRC(cache.tracks[track.id].isrc)
} else { } else {
let isrc let isrc
try{
[dz_id, trackAPI, isrc] = await this.convertTrack(dz, "0", settings.fallbackSearch, track) [dz_id, trackAPI, isrc] = await this.convertTrack(dz, "0", settings.fallbackSearch, track)
}catch (e){
console.warn(e.message)
}
cache.tracks[track.id] = { cache.tracks[track.id] = {
id: dz_id, id: dz_id,
isrc isrc