Skip to content

Commit

Permalink
build: allow fallback JSON dump usage in case Discogs fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Feb 1, 2025
1 parent d49204f commit 4905562
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ DISCOGS_SECRET=
# Slack Contact Webhook
# Mandatory
SLACK_CONTACT_WEBHOOK=

# Slack Netlify Webhook
# Mandatory
SLACK_NETLIFY_WEBHOOK=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data/.obsidian/**
dist
src/assets/fonts
src/assets/fonts.tar
src/public/records.json

# os
.DS_Store
Expand Down
54 changes: 51 additions & 3 deletions src/akte/discogs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { DiscogsRelease } from "./types"

import fs from "node:fs/promises"
import path from "node:path"
import process from "node:process"

// @ts-expect-error 11ty doesn't provide TypeScript definitions
import fetch from "@11ty/eleventy-fetch"
import eleventyFetch from "@11ty/eleventy-fetch"

const DISCOGS_API = "https://api.discogs.com"
const FALLBACK_JSON_DUMP = "https://lihbr.com/records.json"

export async function getAllReleases(page = 1): Promise<DiscogsRelease[]> {
const result = await fetch(
async function getAllReleases(page = 1): Promise<DiscogsRelease[]> {
const result = await eleventyFetch(
`${DISCOGS_API}/users/${process.env.DISCOGS_USER}/collection/folders/0/releases?key=${process.env.DISCOGS_KEY}&secret=${process.env.DISCOGS_SECRET}&sort=added&sort_order=desc&per_page=500&page=${page}`,
{
duration: "1d",
Expand All @@ -27,3 +30,48 @@ export async function getAllReleases(page = 1): Promise<DiscogsRelease[]> {
return result.releases
}
}

export async function getAllReleasesSafely(): Promise<DiscogsRelease[]> {
let releases: DiscogsRelease[]

try {
releases = await getAllReleases()
} catch {
(async () => {
try {
await fetch(process.env.SLACK_NETLIFY_WEBHOOK!, {
headers: { "content-type": "application/json" },
method: "POST",
body: JSON.stringify({
text: "Used fallback JSON dump for Discogs releases",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: ":warning: Used fallback JSON dump for Discogs releases",
},
},
],
}),
})
} catch {
// Noop
}
})()

const result = await fetch(FALLBACK_JSON_DUMP)

if (!result.ok) {
throw new Error(`Failed to fetch fallback releases: ${result.statusText}`)
}

releases = await result.json()
}

if (process.env.NODE_ENV === "production") {
await fs.writeFile(path.join(__dirname, "../public/records.json"), JSON.stringify(releases))
}

return releases
}
2 changes: 1 addition & 1 deletion src/assets/js/meteo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Location {
region: string
country: string

timezone?: string = Intl.DateTimeFormat().resolvedOptions().timeZone
timezone: string = Intl.DateTimeFormat().resolvedOptions().timeZone

#forecast: Forecast | null = null

Expand Down
4 changes: 2 additions & 2 deletions src/files/records.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GlobalData } from "../akte/types"

import { defineAkteFile } from "akte"
import { getAllReleases } from "../akte/discogs"
import { getAllReleasesSafely } from "../akte/discogs"

import { heading } from "../components/heading"

Expand All @@ -10,7 +10,7 @@ import { page } from "../layouts/page"
export const records = defineAkteFile<GlobalData>().from({
path: "/records",
async data() {
const releases = await getAllReleases()
const releases = await getAllReleasesSafely()

return { releases }
},
Expand Down

0 comments on commit 4905562

Please sign in to comment.