From 481b095f59fa5e370ab82c2c83aaec52604c2bd9 Mon Sep 17 00:00:00 2001 From: Vishal Telangre Date: Wed, 21 Jun 2017 14:05:11 +0530 Subject: [PATCH] Fix: Handle parsing of invalid album/playlist URL link --- lib/providers/gaana.js | 37 +++++++++++++++++++++++-------------- lib/providers/saavn.js | 32 ++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/providers/gaana.js b/lib/providers/gaana.js index a13ca81..92e6654 100644 --- a/lib/providers/gaana.js +++ b/lib/providers/gaana.js @@ -21,20 +21,29 @@ module.exports = class Gaana extends Provider { .set({listTitle: 'h1'}) .set({ list: [osmosis.find('[data-type="playSong"].parentnode')] }) .data((data) => { - const trackList = data.list.map(JSON.parse).map(track => { - // Example artists: "Anuradha Paudwal###212098###anuradha-paudwal-2,Kumar Sanu###1###kumar-sanu" - const artists = `${track.artist}`.split(",") - .map(artist => artist.split('###')[0]); - return { - title: track.title, - album: track.albumtitle, - image: track.albumartwork, - artist: artists.join(", "), - time: track.duration, - year: track.release_date && (new Date(track.release_date)).getFullYear() - }; - }); - resolve({listTitle: data.listTitle, trackList}); + try { + const trackList = data.list.map(JSON.parse).map(track => { + // Example artists: "Anuradha Paudwal###212098###anuradha-paudwal-2,Kumar Sanu###1###kumar-sanu" + const artists = `${track.artist}`.split(",") + .map(artist => artist.split('###')[0]); + return { + title: track.title, + album: track.albumtitle, + image: track.albumartwork, + artist: artists.join(", "), + time: track.duration, + year: track.release_date && (new Date(track.release_date)).getFullYear() + }; + }); + + if (!trackList.length) { + reject("Couldn't find any tracks on the provided link"); + } + + resolve({ listTitle: data.listTitle, trackList }); + } catch (error) { + reject(`Error while parsing the list of tracks on the provided link`); + } }) .log(message => { process.env.DEBUG && cli.debug(message); }) .error(err => { diff --git a/lib/providers/saavn.js b/lib/providers/saavn.js index ac39c85..70a45eb 100644 --- a/lib/providers/saavn.js +++ b/lib/providers/saavn.js @@ -21,19 +21,27 @@ module.exports = class Saavn extends Provider { .find('.page-wrap.album-view, .page-wrap.playlist') .set({ listTitle: 'h1', list: [osmosis.find('.track-list li .song-json')] }) .data((data) => { - const trackList = data.list.map(JSON.parse).map(track => { - return { - title: track.title, - album: track.album, - image: track.image_url, - composer: track.music, - artist: track.singers, - time: track.duration, - year: track.year - }; - }); + try { + const trackList = data.list.map(JSON.parse).map(track => { + return { + title: track.title, + album: track.album, + image: track.image_url, + composer: track.music, + artist: track.singers, + time: track.duration, + year: track.year + }; + }); - resolve({listTitle: data.listTitle, trackList}); + if (!trackList.length) { + reject("Couldn't find any tracks on the provided link"); + } + + resolve({listTitle: data.listTitle, trackList}); + } catch (error) { + reject(`Error while parsing the list of tracks on the provided link`); + } }) .log(message => { process.env.DEBUG && cli.debug(message); }) .error(err => {