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'; import { getMangaToFetch } from './utils/get-manga-to-fetch'; import { getMangaTitle } from './utils/get-manga-title'; const config = new lazyKv('./config.json'); const mangaHistory = new lazyKv('./mangaHistory.json'); void (async () => { const checkInterval = await config.get('checkInterval', 900); const userAgent = await config.get( 'userAgent', 'https://git.pfy.ch/pfych/chapter-tracker (Missing custom userAgent? User Miss-configured!)', ); const checkForUpdates = async () => { const { uniqueMangaIds, mangaIdsToWebhooks } = await getMangaToFetch(config); for (const mangaId of uniqueMangaIds) { 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); if (lastChapterId !== latestChapter.id) { console.log('Update found for manga:', title); const webhooksForManga = mangaIdsToWebhooks[mangaId]; 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); } } }; setInterval(() => void checkForUpdates(), checkInterval * 1000); await checkForUpdates(); })();