2021-04-16 04:34:29 +00:00
|
|
|
import ExtendableError from 'es6-error';
|
|
|
|
|
import Client from '../../api/client.js';
|
|
|
|
|
|
|
|
|
|
export class CoverArtArchiveError extends ExtendableError {
|
|
|
|
|
constructor(message, response) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.response = response;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-20 05:21:07 +00:00
|
|
|
|
2017-10-19 08:00:21 +00:00
|
|
|
export default class CoverArtArchiveClient extends Client {
|
2018-08-04 19:04:19 +00:00
|
|
|
constructor({
|
|
|
|
|
baseURL = process.env.COVER_ART_ARCHIVE_BASE_URL ||
|
|
|
|
|
'http://coverartarchive.org/',
|
|
|
|
|
limit = 10,
|
|
|
|
|
period = 1000,
|
|
|
|
|
...options
|
|
|
|
|
} = {}) {
|
2021-04-16 04:34:29 +00:00
|
|
|
super({ baseURL, limit, period, ...options });
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
2017-10-19 08:00:21 +00:00
|
|
|
/**
|
|
|
|
|
* Sinfully attempt to parse HTML responses for the error message.
|
|
|
|
|
*/
|
2021-04-16 04:34:29 +00:00
|
|
|
parseErrorMessage(err) {
|
|
|
|
|
if (err.name === 'HTTPError') {
|
|
|
|
|
const { body } = err.response;
|
|
|
|
|
if (typeof body === 'string' && body.startsWith('<!')) {
|
|
|
|
|
const heading = /<h1>([^<]+)<\/h1>/i.exec(body);
|
|
|
|
|
const message = /<p>([^<]+)<\/p>/i.exec(body);
|
|
|
|
|
return new CoverArtArchiveError(
|
|
|
|
|
`${heading ? heading[1] + ': ' : ''}${message ? message[1] : ''}`,
|
|
|
|
|
err.response
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
2021-04-16 04:34:29 +00:00
|
|
|
return super.parseErrorMessage(err);
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
images(entityType, mbid) {
|
2021-04-16 04:34:29 +00:00
|
|
|
return this.get(`${entityType}/${mbid}`, { resolveBodyOnly: true });
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
async imageURL(entityType, mbid, typeOrID = 'front', size) {
|
|
|
|
|
let url = `${entityType}/${mbid}/${typeOrID}`;
|
2016-12-20 05:21:07 +00:00
|
|
|
if (size != null) {
|
2021-04-16 04:34:29 +00:00
|
|
|
url += `-${size}`;
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
2021-04-16 04:34:29 +00:00
|
|
|
const response = await this.get(url, {
|
|
|
|
|
method: 'HEAD',
|
|
|
|
|
followRedirect: false,
|
|
|
|
|
});
|
|
|
|
|
return response.headers.location;
|
2016-12-20 05:21:07 +00:00
|
|
|
}
|
|
|
|
|
}
|