From 3abe8f1a79b034c30a966b705e1c0ea58f2963e6 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Mon, 11 Mar 2019 23:26:54 +0000 Subject: [PATCH] Handle broken links better --- lib/services/deezer/index.js | 4 +- lib/services/google/index.js | 84 +++++++++++++++++++---------------- lib/services/itunes/index.js | 65 ++++++++++++++------------- lib/services/spotify/index.js | 10 ++++- 4 files changed, 93 insertions(+), 70 deletions(-) diff --git a/lib/services/deezer/index.js b/lib/services/deezer/index.js index 99c72d5..69fff3b 100644 --- a/lib/services/deezer/index.js +++ b/lib/services/deezer/index.js @@ -91,7 +91,9 @@ export async function lookupId(id, type) { }, }); } - return Promise.reject(new Error()); + const error = new Error('Not Found'); + error.status = 404; + return Promise.reject(error); } export async function search(data, original = {}) { diff --git a/lib/services/google/index.js b/lib/services/google/index.js index ebe291a..755a00c 100644 --- a/lib/services/google/index.js +++ b/lib/services/google/index.js @@ -23,45 +23,53 @@ let ready = pm.initAsync(creds).catch(function(err) { export async function lookupId(id, type) { await ready; - if (type === 'album') { - const album = await pm.getAlbumAsync(id, false); - return { - service: 'google', - type: 'album', - id: album.albumId, - name: album.name, - streamUrl: `https://play.google.com/music/m/${album.albumId}?signup_if_needed=1`, - purchaseUrl: `https://play.google.com/store/music/album?id=${album.albumId}`, - artwork: { - small: album.albumArtRef.replace('http:', 'https:'), - large: album.albumArtRef.replace('http:', 'https:'), - }, - artist: { - name: album.artist, - }, - }; - } else if (type === 'track') { - const track = await pm.getAllAccessTrackAsync(id); - return { - service: 'google', - type: 'track', - id: track.nid, - name: track.title, - streamUrl: `https://play.google.com/music/m/${track.nid}?signup_if_needed=1`, - purchaseUrl: `https://play.google.com/store/music/album?id=${track.albumId}`, - artwork: { - small: track.albumArtRef[0].url.replace('http:', 'https:'), - large: track.albumArtRef[0].url.replace('http:', 'https:'), - }, - album: { - name: track.album, - }, - artist: { - name: track.artist, - }, - }; + try { + if (type === 'album') { + const album = await pm.getAlbumAsync(id, false); + return { + service: 'google', + type: 'album', + id: album.albumId, + name: album.name, + streamUrl: `https://play.google.com/music/m/${album.albumId}?signup_if_needed=1`, + purchaseUrl: `https://play.google.com/store/music/album?id=${album.albumId}`, + artwork: { + small: album.albumArtRef.replace('http:', 'https:'), + large: album.albumArtRef.replace('http:', 'https:'), + }, + artist: { + name: album.artist, + }, + }; + } else if (type === 'track') { + const track = await pm.getAllAccessTrackAsync(id); + return { + service: 'google', + type: 'track', + id: track.nid, + name: track.title, + streamUrl: `https://play.google.com/music/m/${track.nid}?signup_if_needed=1`, + purchaseUrl: `https://play.google.com/store/music/album?id=${track.albumId}`, + artwork: { + small: track.albumArtRef[0].url.replace('http:', 'https:'), + large: track.albumArtRef[0].url.replace('http:', 'https:'), + }, + album: { + name: track.album, + }, + artist: { + name: track.artist, + }, + }; + } + } catch(e) { + const error = new Error('Not Found'); + error.status = 404; + return Promise.reject(error); } - return { service: 'google' }; + const error = new Error('Not Found'); + error.status = 404; + return Promise.reject(error); } function exactMatch(needle, haystack, type) { diff --git a/lib/services/itunes/index.js b/lib/services/itunes/index.js index eadf7b4..31e06d0 100644 --- a/lib/services/itunes/index.js +++ b/lib/services/itunes/index.js @@ -39,39 +39,44 @@ export async function lookupId(possibleId, type, countrycode) { if (cc) { path = `/${cc}${path}`; } - const response = await request.get(apiRoot + path); - let result = JSON.parse(response.text); - if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) { + try { + const response = await request.get(apiRoot + path); + let result = JSON.parse(response.text); + + if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) { + throw new Error(); + } else { + result = result.results[0]; + + const item = { + service: 'itunes', + type, + id: cc + id, + name: result.trackName ? result.trackName : result.collectionName, + streamUrl: null, + purchaseUrl: result.collectionViewUrl, + artwork: { + small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`, + large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`, + }, + artist: { + name: result.artistName, + }, + }; + + if (type === 'track') { + item.album = { + name: result.collectionName, + }; + } + + return item; + } + } catch(e) { const error = new Error('Not Found'); error.status = 404; - throw error; - } else { - result = result.results[0]; - - const item = { - service: 'itunes', - type, - id: cc + id, - name: result.trackName ? result.trackName : result.collectionName, - streamUrl: null, - purchaseUrl: result.collectionViewUrl, - artwork: { - small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`, - large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`, - }, - artist: { - name: result.artistName, - }, - }; - - if (type === 'track') { - item.album = { - name: result.collectionName, - }; - } - - return item; + return Promise.reject(error); } } diff --git a/lib/services/spotify/index.js b/lib/services/spotify/index.js index 2e92e65..13e4958 100644 --- a/lib/services/spotify/index.js +++ b/lib/services/spotify/index.js @@ -44,6 +44,12 @@ export async function lookupId(id, type) { data = data.body[`${type}s`][0]; + if(!data) { + const error = new Error('Not Found'); + error.status = 404; + return Promise.reject(error); + } + const artist = data.artists[0]; if (type === 'album') { @@ -82,7 +88,9 @@ export async function lookupId(id, type) { }, }; } - return { service: 'spotify' }; + const error = new Error('Not Found'); + error.status = 404; + return Promise.reject(error); } export async function search(data, original = {}) {