64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { PORNHUB_BASE_URL } from "@/constants/urls";
|
|
import { FetchParams, GalleryData } from "@/meta/data";
|
|
import { getHeaders } from "../common/headers";
|
|
import { getDataFromRedis, storeDataIntoRedis } from "@/redis/client";
|
|
import { getPornHubQueryUrl, getPornHubResultsWrapperId } from "./url";
|
|
|
|
import * as cheerio from "cheerio";
|
|
|
|
import axios, { AxiosError } from "axios";
|
|
import { Platforms } from "@/meta/settings";
|
|
|
|
import { DEFAULT_PORNHUB_GALLERY_EXPIRY } from "@/constants/redis";
|
|
|
|
export const fetchPornHubGalleryData = async (params?: FetchParams): Promise<GalleryData[]> => {
|
|
|
|
let data: GalleryData[] = [];
|
|
|
|
const reqHeaders = getHeaders(PORNHUB_BASE_URL)
|
|
|
|
const queryUrl = await getPornHubQueryUrl(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 = await getPornHubResultsWrapperId(params?.query)
|
|
|
|
const thumbs = $(wrapperId);
|
|
|
|
thumbs.map((key, thumb) => {
|
|
|
|
const videoUrl = $(thumb).find(".pcVideoListItem a").attr("href")?.split('=')[1];
|
|
const imgUrl = $(thumb).find(".pcVideoListItem a img").attr("src")
|
|
const text = $(thumb).find(".pcVideoListItem a").attr("title")
|
|
|
|
videoUrl && imgUrl && text && data.push({
|
|
videoUrl,
|
|
imgUrl,
|
|
text,
|
|
platform: Platforms.pornhub
|
|
})
|
|
})
|
|
|
|
if (data.length > 0) {
|
|
await storeDataIntoRedis(queryUrl, data, DEFAULT_PORNHUB_GALLERY_EXPIRY);
|
|
}
|
|
|
|
}).catch((error: AxiosError) => {
|
|
// handle errors
|
|
});
|
|
|
|
|
|
return data
|
|
} |