Compare commits

..

No commits in common. "e11d941a93f142c44dc9b24157f56fd695bf047e" and "040b427d97126d03bd6a25629b777513132e83b8" have entirely different histories.

3 changed files with 60 additions and 33 deletions

View File

@ -1,5 +1,6 @@
import { getAllChapters, getLatestChapter } from './mangadex/chapters'; import { getAllChapters, getLatestChapter } from './mangadex/chapters';
import { getManga } from './mangadex/manga'; import { getManga } from './mangadex/manga';
import { getCover } from './mangadex/cover';
import { lazyKv } from './db/lazyKv'; import { lazyKv } from './db/lazyKv';
import { sendWebhook } from './utils/webhook'; import { sendWebhook } from './utils/webhook';
import { ChapterId } from './types'; import { ChapterId } from './types';
@ -21,37 +22,34 @@ void (async () => {
await getMangaToFetch(config); await getMangaToFetch(config);
for (const mangaId of uniqueMangaIds) { for (const mangaId of uniqueMangaIds) {
try { const lastChapterId = await mangaHistory.get<ChapterId>(mangaId);
const lastChapterId = await mangaHistory.get<ChapterId>(mangaId);
const manga = await getManga(mangaId, userAgent); const manga = await getManga(mangaId, userAgent);
const chapters = await getAllChapters(mangaId, userAgent); const chapters = await getAllChapters(mangaId, userAgent);
const latestChapter = getLatestChapter(chapters); const latestChapter = getLatestChapter(chapters);
const title = getMangaTitle(manga); const cover = await getCover(manga, userAgent);
const title = getMangaTitle(manga);
if (lastChapterId !== latestChapter.id) { if (lastChapterId !== latestChapter.id) {
console.log('Update found for manga:', title); console.log('Update found for manga:', title);
const webhooksForManga = mangaIdsToWebhooks[mangaId]; const webhooksForManga = mangaIdsToWebhooks[mangaId];
await Promise.all( await Promise.all(
webhooksForManga.map( webhooksForManga.map(
async (webhookUrl) => async (webhookUrl) =>
await sendWebhook({ await sendWebhook({
webhookUrl, webhookUrl,
manga, manga,
latestChapter, latestChapter,
}), cover,
), }),
); ),
);
await mangaHistory.set(mangaId, latestChapter.id); await mangaHistory.set(mangaId, latestChapter.id);
} else { } else {
console.log('No Updates found for manga:', title); console.log('No Updates found for manga:', title);
}
} catch (err) {
console.error(`Failed to fetch ${mangaId}!`);
console.error(err);
} }
} }
}; };

25
src/mangadex/cover.ts Normal file
View File

@ -0,0 +1,25 @@
import axios from 'axios';
import { Manga } from '../types';
export const getCover = async (
manga: Manga,
userAgent: string,
): Promise<string> => {
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}`;
};

View File

@ -7,21 +7,25 @@ export const sendWebhook = async (args: {
webhookUrl: string; webhookUrl: string;
manga: Manga; manga: Manga;
latestChapter: Chapter; latestChapter: Chapter;
cover: string;
}) => { }) => {
const { webhookUrl, manga, latestChapter } = args; const { webhookUrl, manga, latestChapter, cover } = args;
const title = getMangaTitle(manga); const title = getMangaTitle(manga);
await axios.post(webhookUrl, { await axios.post(webhookUrl, {
username: 'Manga Updates', username: 'Manga Updates',
avatar_url: 'https://mangadex.org/pwa/icons/icon-180.png', avatar_url: 'https://assets.pfy.ch/icons/manga.png',
content: `[New chapter for ${title}](https://mangadex.org/chapter/${latestChapter.id})`,
embeds: [ embeds: [
{ {
url: `https://mangadex.org/chapter/${latestChapter.id}`, author: {
image: { icon_url: 'https://assets.pfy.ch/icons/manga.png',
url: `https://og.mangadex.org/og-image/chapter/${latestChapter.id}`, name: title,
}, },
title: `${title}`, thumbnail: {
description: ` Ch. ${latestChapter.attributes.chapter} - ${latestChapter.attributes.title} `, url: cover,
},
description: `Chapter ${latestChapter.attributes.chapter}: "${latestChapter.attributes.title}"`,
}, },
], ],
}); });