Discogs integration

I decided that it does not make any sense to have Discogs integration on my site anymore. However, I’m sharing it here as a note. Maybe someone will need it.

The integration consisted of two parts: a script to fetch the data and download the images, and a page to display them.

The script

This script uses the @lionralfs/discogs-client to fetch my collection, saves the data to a JSON file, and downloads the cover images to a local directory.

import { DiscogsClient } from "@lionralfs/discogs-client";
import "dotenv/config";
import fs from "fs";
import got from "got";
import { pipeline as streamPipeline } from "node:stream/promises";
import path from "path";
const client = new DiscogsClient({
auth: { userToken: process.env.DISCOGS_TOKEN },
});
const result = await client
.user()
.collection()
.getReleases("eshlox", 0, { page: 0, per_page: 100 });
const collections = result.data.releases.map((release) => ({
id: release.id,
url: `https://www.discogs.com/release/${release.id}`,
year: release.basic_information.year,
title: release.basic_information.title,
image: release.basic_information.cover_image,
image_extension: release.basic_information.cover_image.split(".").pop(),
}));
fs.writeFile(
path.join("./src/content/discography", "index.json"),
JSON.stringify(collections, null, 2),
(err) => {
console.log(err);
},
);
collections.map(async (collection) => {
const directory = path.join(
"./src/images/discography",
collection.year.toString(),
);
const imagePath = `${directory}/${collection.id}.${collection.image_extension}`;
if (!fs.existsSync(imagePath)) {
fs.mkdir(directory, () => {});
await streamPipeline(
got.stream(collection.image),
fs.createWriteStream(imagePath),
);
}
});

Displaying the collection

The following example shows how to display the images in an Astro page. It assumes the data is available via a content collection.

---
import { getCollection } from "astro:content";
import { Image } from "astro:assets";
const discography = await getCollection("discography");
const sorted = discography[0].data; // Simplified sorting
---
<div>
{
sorted.map((item: any) => (
<a href={item.url} title={item.title}>
<Image
src={`/images/discography/${item.year}/${item.id}.${item.image_extension}`}
alt={item.title}
width={500}
height={500}
/>
</a>
))
}
</div>