diff --git a/src/utils/modifier.ts b/src/utils/modifier.ts index 9805872..bc96c8b 100644 --- a/src/utils/modifier.ts +++ b/src/utils/modifier.ts @@ -1,4 +1,6 @@ import p from "phin"; +import { load } from "cheerio"; +import c from "./options"; function getPururinInfo(value: string) { @@ -22,6 +24,10 @@ function getId(url: string) { return url.replace(/^https?:\/\/[^\\/]+/, "").replace(/\/$/, ""); } +function removeNonNumeric(input: string) { + return input.replace(/[^0-9]/g, ""); +} + function getDate(date: Date) { return date.toLocaleDateString("en-US", { year: "numeric", @@ -66,5 +72,16 @@ export const isNumeric = (val: string) : boolean => { return !isNaN(Number(val)); }; +export async function getIdRandomPururin (): Promise { + const randomNumber = Math.floor(Math.random() * 500) + 1; + const raw = await p(`${c.PURURIN}/browse/random?page=${randomNumber}`); + const $ = load(raw.body); + const gallery = $("img.card-img-top").map((i, el) => $(el).attr("data-src")).get(); + const galleryNumber = gallery.map(el => removeNonNumeric(el)); + const randomgallery = galleryNumber[Math.floor(Math.random() * galleryNumber.length)]; + return parseInt(randomgallery); +} -export { getPururinInfo, getPururinPageCount, getUrl, getId, getDate, timeAgo, mock, getPururinLanguage }; \ No newline at end of file + +export { getPururinInfo, getPururinPageCount, getUrl, getId, getDate, timeAgo, + mock, getPururinLanguage, removeNonNumeric }; \ No newline at end of file