60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { YOUPORN_BASE_URL } from "@/constants/urls";
|
|
import { FetchParams, GalleryData } from "@/meta/data";
|
|
import { getHeaders } from "../common/headers";
|
|
import { getYouPornQueryUrl } from "./url";
|
|
import { getDataFromRedis, storeDataIntoRedis } from "@/redis/client";
|
|
|
|
import * as cheerio from "cheerio";
|
|
|
|
import axios, { AxiosError } from "axios";
|
|
import { Platforms } from "@/meta/settings";
|
|
import { DEFAULT_YOUPORN_GALLERY_EXPIRY } from "@/constants/redis";
|
|
|
|
export const fetchYouPornGalleryData = async (params?: FetchParams): Promise<GalleryData[]> => {
|
|
|
|
let data: GalleryData[] = [];
|
|
|
|
const reqHeaders = getHeaders(YOUPORN_BASE_URL)
|
|
|
|
const queryUrl = await getYouPornQueryUrl(params?.query)
|
|
|
|
const cachedData = await getDataFromRedis(queryUrl)
|
|
|
|
if (cachedData) {
|
|
return cachedData as GalleryData[]
|
|
}
|
|
|
|
await axios.get(queryUrl, reqHeaders)
|
|
|
|
.then(async response => {
|
|
|
|
const html = response.data;
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
const wrapperId = params?.query ? ".searchResults .video-box" : ".tm_mostRecent_videos_section .video-box"
|
|
|
|
const thumbs = $(wrapperId);
|
|
|
|
thumbs.map((key, thumb) => {
|
|
|
|
const videoUrl = $(thumb).find("a.tm_video_link").attr("href")?.split('/')[2];
|
|
const imgUrl = $(thumb).find("img.thumb-image").attr("data-src")
|
|
const text = $(thumb).find("a.video-title").text();
|
|
|
|
videoUrl && imgUrl && text && data.push({
|
|
videoUrl,
|
|
imgUrl,
|
|
text,
|
|
platform: Platforms.youporn
|
|
})
|
|
})
|
|
|
|
await storeDataIntoRedis(queryUrl, data, DEFAULT_YOUPORN_GALLERY_EXPIRY);
|
|
|
|
}).catch((error: AxiosError) => {
|
|
// handle errors
|
|
});
|
|
|
|
return data
|
|
} |