Handle broken links better

This commit is contained in:
Jonathan Cremin 2019-03-11 23:26:54 +00:00
parent b5f99b217a
commit 3abe8f1a79
4 changed files with 93 additions and 70 deletions

View file

@ -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 = {}) { export async function search(data, original = {}) {

View file

@ -23,45 +23,53 @@ let ready = pm.initAsync(creds).catch(function(err) {
export async function lookupId(id, type) { export async function lookupId(id, type) {
await ready; await ready;
if (type === 'album') { try {
const album = await pm.getAlbumAsync(id, false); if (type === 'album') {
return { const album = await pm.getAlbumAsync(id, false);
service: 'google', return {
type: 'album', service: 'google',
id: album.albumId, type: 'album',
name: album.name, id: album.albumId,
streamUrl: `https://play.google.com/music/m/${album.albumId}?signup_if_needed=1`, name: album.name,
purchaseUrl: `https://play.google.com/store/music/album?id=${album.albumId}`, streamUrl: `https://play.google.com/music/m/${album.albumId}?signup_if_needed=1`,
artwork: { purchaseUrl: `https://play.google.com/store/music/album?id=${album.albumId}`,
small: album.albumArtRef.replace('http:', 'https:'), artwork: {
large: album.albumArtRef.replace('http:', 'https:'), small: album.albumArtRef.replace('http:', 'https:'),
}, large: album.albumArtRef.replace('http:', 'https:'),
artist: { },
name: album.artist, artist: {
}, name: album.artist,
}; },
} else if (type === 'track') { };
const track = await pm.getAllAccessTrackAsync(id); } else if (type === 'track') {
return { const track = await pm.getAllAccessTrackAsync(id);
service: 'google', return {
type: 'track', service: 'google',
id: track.nid, type: 'track',
name: track.title, id: track.nid,
streamUrl: `https://play.google.com/music/m/${track.nid}?signup_if_needed=1`, name: track.title,
purchaseUrl: `https://play.google.com/store/music/album?id=${track.albumId}`, streamUrl: `https://play.google.com/music/m/${track.nid}?signup_if_needed=1`,
artwork: { purchaseUrl: `https://play.google.com/store/music/album?id=${track.albumId}`,
small: track.albumArtRef[0].url.replace('http:', 'https:'), artwork: {
large: track.albumArtRef[0].url.replace('http:', 'https:'), small: track.albumArtRef[0].url.replace('http:', 'https:'),
}, large: track.albumArtRef[0].url.replace('http:', 'https:'),
album: { },
name: track.album, album: {
}, name: track.album,
artist: { },
name: track.artist, 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) { function exactMatch(needle, haystack, type) {

View file

@ -39,39 +39,44 @@ export async function lookupId(possibleId, type, countrycode) {
if (cc) { if (cc) {
path = `/${cc}${path}`; 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'); const error = new Error('Not Found');
error.status = 404; error.status = 404;
throw error; return Promise.reject(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;
} }
} }

View file

@ -44,6 +44,12 @@ export async function lookupId(id, type) {
data = data.body[`${type}s`][0]; 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]; const artist = data.artists[0];
if (type === 'album') { 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 = {}) { export async function search(data, original = {}) {