Files
legica-bot/src/common/getImgTitle.ts
2024-08-16 23:00:39 +02:00

16 lines
488 B
TypeScript

import { Legica } from "@models";
import axios from "axios";
import cheerio from "cheerio";
export async function getImgTitle(href: string): Promise<Legica> {
const response = await axios.get(href);
const html = response.data;
const $ = cheerio.load(html);
const title = $(".article-title-container > h1").text();
const src = $(".image-holder", ".article-content").find("img").attr("src");
if (!src) throw new Error(`Image not found at ${href}.`);
return { title, img: src };
}