60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { FetchParams, GalleryData } from "@/meta/data";
|
|
import { getHeaders } from "../common/headers";
|
|
import { REDTUBE_BASE_URL } from "@/constants/urls";
|
|
import { getRedTubeQueryUrl, getRedTubeResultsWrapperId } 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_REDTUBE_GALLERY_EXPIRY } from "@/constants/redis";
|
|
|
|
export const fetchRedTubeGalleryData = async (params?: FetchParams): Promise<GalleryData[]> => {
|
|
|
|
let data: GalleryData[] = [];
|
|
|
|
const reqHeaders = getHeaders(REDTUBE_BASE_URL)
|
|
|
|
const queryUrl = await getRedTubeQueryUrl(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 getRedTubeResultsWrapperId(params?.query)
|
|
|
|
const thumbs = $(wrapperId);
|
|
|
|
thumbs.map((key, thumb) => {
|
|
|
|
const videoUrl = $(thumb).find("a.video_link").attr("href")?.split('/')[1];
|
|
const imgUrl = $(thumb).find("img.js_thumbImageTag").attr("data-src")
|
|
const text = $(thumb).find("a.tm_video_title").attr("title");
|
|
|
|
videoUrl && imgUrl && text && data.push({
|
|
videoUrl,
|
|
imgUrl,
|
|
text,
|
|
platform: Platforms.redtube
|
|
})
|
|
})
|
|
|
|
await storeDataIntoRedis(queryUrl, data, DEFAULT_REDTUBE_GALLERY_EXPIRY);
|
|
|
|
}).catch((error: AxiosError) => {
|
|
// handle errors
|
|
});
|
|
|
|
return data
|
|
} |