Skip to content

Instantly share code, notes, and snippets.

@chrisvoo
Created May 10, 2021 06:21
Show Gist options
  • Save chrisvoo/b8e34fc88b788fbc1ab08344c8a06291 to your computer and use it in GitHub Desktop.
Save chrisvoo/b8e34fc88b788fbc1ab08344c8a06291 to your computer and use it in GitHub Desktop.
An example on how to retrieve an artist's discography with Discogs
/* The API returns way too much results: the example below is the best approximation
* to get a discography. Anyway, there are some errors:
* - another group is present
* - Cheshire cat's album is missing
*/
import { Client } from 'disconnect';
import dotenv from 'dotenv';
import { envSchema } from './utils';
import { showResult } from './utils/terminal';
const result = dotenv.config();
if (result.error) {
console.error(result.error.message);
process.exit(1);
}
const { error } = envSchema.validate(result.parsed);
if (error) {
console.error(error.message);
process.exit(1);
}
const client = new Client(process.env.USER_AGENT, {
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET
}).setConfig({
// see https://github.com/bartve/disconnect/blob/master/lib/client.js#L16
outputFormat: 'plaintext'
});
(async () => {
const db = client.database();
const res = await db.search('blink182', {
format: 'album',
type: 'master'
});
console.log(res.pagination);
const albums = res.results
.filter((result: { format: string[]; }) => {
if (
!result.format.includes('Unofficial Release') &&
!result.format.includes('Compilation')
) {
return result;
}
})
.map((result: any) => ({
country: result.country,
year: result.year || 0,
format: result.format,
genre: result.genre,
style: result.style,
id: result.id, // master id
title: result.title,
}))
.sort((a: { year: string; }, b: { year: string; }) => {
if (parseInt(a.year, 10) < parseInt(b.year, 10) || (!a.year && b.year)) return -1;
if (parseInt(a.year, 10) > parseInt(b.year, 10) || (a.year && !b.year)) return 1;
return 0;
});
showResult(albums);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment