From 9eb25f706e7c1ed5c3e9054e1d3feaa81e9b4758 Mon Sep 17 00:00:00 2001 From: pfych Date: Wed, 30 Apr 2025 19:26:25 +1000 Subject: [PATCH 1/2] Handle network requests failing more gracefully --- src/index.ts | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index b9f97da..58b6cae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,34 +22,39 @@ void (async () => { await getMangaToFetch(config); for (const mangaId of uniqueMangaIds) { - const lastChapterId = await mangaHistory.get(mangaId); + try { + const lastChapterId = await mangaHistory.get(mangaId); - const manga = await getManga(mangaId, userAgent); - const chapters = await getAllChapters(mangaId, userAgent); - const latestChapter = getLatestChapter(chapters); - const cover = await getCover(manga, userAgent); - const title = getMangaTitle(manga); + const manga = await getManga(mangaId, userAgent); + const chapters = await getAllChapters(mangaId, userAgent); + const latestChapter = getLatestChapter(chapters); + const cover = await getCover(manga, userAgent); + const title = getMangaTitle(manga); - if (lastChapterId !== latestChapter.id) { - console.log('Update found for manga:', title); + if (lastChapterId !== latestChapter.id) { + console.log('Update found for manga:', title); - const webhooksForManga = mangaIdsToWebhooks[mangaId]; + const webhooksForManga = mangaIdsToWebhooks[mangaId]; - await Promise.all( - webhooksForManga.map( - async (webhookUrl) => - await sendWebhook({ - webhookUrl, - manga, - latestChapter, - cover, - }), - ), - ); + await Promise.all( + webhooksForManga.map( + async (webhookUrl) => + await sendWebhook({ + webhookUrl, + manga, + latestChapter, + cover, + }), + ), + ); - await mangaHistory.set(mangaId, latestChapter.id); - } else { - console.log('No Updates found for manga:', title); + await mangaHistory.set(mangaId, latestChapter.id); + } else { + console.log('No Updates found for manga:', title); + } + } catch (err) { + console.error(`Failed to fetch ${mangaId}!`); + console.error(err); } } }; From e11d941a93f142c44dc9b24157f56fd695bf047e Mon Sep 17 00:00:00 2001 From: pfych Date: Wed, 30 Apr 2025 19:36:01 +1000 Subject: [PATCH 2/2] Update embed style to reflect native embeds --- src/index.ts | 3 --- src/mangadex/cover.ts | 25 ------------------------- src/utils/webhook.ts | 18 +++++++----------- 3 files changed, 7 insertions(+), 39 deletions(-) delete mode 100644 src/mangadex/cover.ts diff --git a/src/index.ts b/src/index.ts index 58b6cae..7539ce0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ import { getAllChapters, getLatestChapter } from './mangadex/chapters'; import { getManga } from './mangadex/manga'; -import { getCover } from './mangadex/cover'; import { lazyKv } from './db/lazyKv'; import { sendWebhook } from './utils/webhook'; import { ChapterId } from './types'; @@ -28,7 +27,6 @@ void (async () => { const manga = await getManga(mangaId, userAgent); const chapters = await getAllChapters(mangaId, userAgent); const latestChapter = getLatestChapter(chapters); - const cover = await getCover(manga, userAgent); const title = getMangaTitle(manga); if (lastChapterId !== latestChapter.id) { @@ -43,7 +41,6 @@ void (async () => { webhookUrl, manga, latestChapter, - cover, }), ), ); diff --git a/src/mangadex/cover.ts b/src/mangadex/cover.ts deleted file mode 100644 index 0011834..0000000 --- a/src/mangadex/cover.ts +++ /dev/null @@ -1,25 +0,0 @@ -import axios from 'axios'; -import { Manga } from '../types'; - -export const getCover = async ( - manga: Manga, - userAgent: string, -): Promise => { - const coverId = Object.values(manga.relationships).find( - (relationship) => relationship.type === 'cover_art', - )?.id; - - if (!coverId) { - return ''; - } - - const response = await axios.get<{ - data: { attributes: { fileName: string } }; - }>(`https://api.mangadex.org/cover/${coverId}`, { - headers: { - 'User-Agent': userAgent, - }, - }); - - return `https://mangadex.org/covers/${manga.id}/${response.data.data.attributes.fileName}`; -}; diff --git a/src/utils/webhook.ts b/src/utils/webhook.ts index bd6dbcd..5723fa7 100644 --- a/src/utils/webhook.ts +++ b/src/utils/webhook.ts @@ -7,25 +7,21 @@ export const sendWebhook = async (args: { webhookUrl: string; manga: Manga; latestChapter: Chapter; - cover: string; }) => { - const { webhookUrl, manga, latestChapter, cover } = args; + const { webhookUrl, manga, latestChapter } = args; const title = getMangaTitle(manga); await axios.post(webhookUrl, { username: 'Manga Updates', - avatar_url: 'https://assets.pfy.ch/icons/manga.png', - content: `[New chapter for ${title}](https://mangadex.org/chapter/${latestChapter.id})`, + avatar_url: 'https://mangadex.org/pwa/icons/icon-180.png', embeds: [ { - author: { - icon_url: 'https://assets.pfy.ch/icons/manga.png', - name: title, + url: `https://mangadex.org/chapter/${latestChapter.id}`, + image: { + url: `https://og.mangadex.org/og-image/chapter/${latestChapter.id}`, }, - thumbnail: { - url: cover, - }, - description: `Chapter ${latestChapter.attributes.chapter}: "${latestChapter.attributes.title}"`, + title: `${title}`, + description: ` Ch. ${latestChapter.attributes.chapter} - ${latestChapter.attributes.title} `, }, ], });