Handle broken links better
This commit is contained in:
parent
b5f99b217a
commit
3abe8f1a79
4 changed files with 93 additions and 70 deletions
|
@ -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 = {}) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 = {}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue