Open read-manga in Script Kit

// Name: Search Manga in Manganato
import "@johnlindquist/kit"
import { Choice } from "@johnlindquist/kit"
import cheerio from 'cheerio'
import axios from 'axios'
type Manga = {
title: string
url: string
imageUrl: string
}
type Chapter = {
title: string
url: string
}
const baseUrl = 'https://mangakakalot.com'
async function loadCheerio(url: string) {
const { data } = await axios.get(url)
const $ = cheerio.load(data)
return $
}
async function searchManga(searchTerm: string): Promise<Manga[]> {
const searchUrl = `${baseUrl}/search/story/${searchTerm.toLowerCase().replaceAll(' ', '_')}`
const $ = await loadCheerio(searchUrl)
const mangas: Manga[] = $('div.story_item').get().map(el => {
return {
title: $(el).find('h3.story_name').text().replaceAll('\n', ''),
imageUrl: $(el).find('img').attr('src'),
url: $(el).find('h3.story_name > a').attr('href')
}
})
return mangas
}
async function getChapterLinks(mangaUrl: string): Promise<Chapter[]> {
const $ = await loadCheerio(mangaUrl)
const chapterList: Chapter[] = $('ul.row-content-chapter > li > a').get().map(chapter => {
return {
title: $(chapter).text().replaceAll('\n', ''),
url: $(chapter).attr('href')
}
})
return chapterList
}
function buildMangaResult(manga: Manga): Choice {
return {
name: manga.title,
value: manga.url,
img: manga.imageUrl,
height: 250,
preview: buildPreview(manga)
}
}
function buildChapterResult(chapter: Chapter): Choice {
return {
name: chapter.title,
value: chapter.url,
description: chapter.url
}
}
function buildPreview(manga: Manga) {
return `
<div class="p-5 prose prose-sm">
<img class="rounded" src="${manga.imageUrl}"/>
</div>
`
}
const mangaURL = await arg('Manga name', async input => {
const mangas: Manga[] = await searchManga(input)
const results = mangas.map(manga => buildMangaResult(manga))
return results
})
const openChapterOrPage: 'Page' | 'Chapter' = await arg('Open manga page or chapter', ['Page', 'Chapter'])
if(openChapterOrPage == 'Page') {
open(mangaURL)
} else {
const chapters = await getChapterLinks(mangaURL)
const targetChapter = await arg('Select chapter', chapters.map(chapter => buildChapterResult(chapter)))
open(targetChapter)
}