add YouPorn related videos

This commit is contained in:
La macchina desiderante 2024-05-25 20:13:59 +02:00
parent 7c5ca07ab4
commit a545a1f4d0
1 changed files with 24 additions and 10 deletions

View File

@ -2,13 +2,14 @@ import { YOUPORN_BASE_URL, YOUPORN_BASE_URL_VIDEO } from "@/constants/urls";
import { FetchParams, GalleryData, VideoData, VideoSourceItem } from "@/meta/data"; import { FetchParams, GalleryData, VideoData, VideoSourceItem } from "@/meta/data";
import { getHeaders } from "../common/headers"; import { getHeaders } from "../common/headers";
import { getDataFromRedis, storeDataIntoRedis } from "@/redis/client"; import { getDataFromRedis, storeDataIntoRedis } from "@/redis/client";
import { DEFAULT_RELATED_VIDEO_KEY_PATH, DEFAULT_YOUPORN_VIDEO_EXPIRY } from "@/constants/redis"; import { DEFAULT_RELATED_VIDEO_KEY_PATH, DEFAULT_YOUPORN_VIDEO_EXPIRY, DEFAULT_YOUPORN_GALLERY_EXPIRY } from "@/constants/redis";
import * as cheerio from "cheerio"; import * as cheerio from "cheerio";
import axios, { AxiosError } from "axios"; import axios, { AxiosError } from "axios";
import { createSessionCookie, findGetMediaUrlInTagblock, findGetRelatedUrlInTagblock } from "../common/mindgeek"; import { createSessionCookie, findGetMediaUrlInTagblock } from "../common/mindgeek";
import { getYouPornMediaUrlList } from "./url"; import { getYouPornMediaUrlList } from "./url";
import { Platforms } from "@/meta/settings";
export const fetchYouPornVideoData = async (videoId: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => { export const fetchYouPornVideoData = async (videoId: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => {
@ -19,7 +20,7 @@ export const fetchYouPornVideoData = async (videoId: string, params?: FetchParam
let relatedData: GalleryData[] = []; let relatedData: GalleryData[] = [];
let mediaUrl, relatedUrl, sessionCookie, convertedData: VideoSourceItem[] let mediaUrl, sessionCookie, convertedData: VideoSourceItem[]
let reqHeaders = getHeaders(YOUPORN_BASE_URL) let reqHeaders = getHeaders(YOUPORN_BASE_URL)
@ -53,13 +54,22 @@ export const fetchYouPornVideoData = async (videoId: string, params?: FetchParam
}) })
scriptTags.map((idx, elem) => { const wrapperId = "#relatedVideos .video-box"
const getRelatedUrl = findGetRelatedUrlInTagblock($(elem).toString()) ?? null
if (getRelatedUrl) { const thumbs = $(wrapperId);
relatedUrl = getRelatedUrl
}
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 && relatedData.push({
videoUrl,
imgUrl,
text,
platform: Platforms.youporn
})
}) })
}).catch((error: AxiosError) => { }).catch((error: AxiosError) => {
@ -73,5 +83,9 @@ export const fetchYouPornVideoData = async (videoId: string, params?: FetchParam
await storeDataIntoRedis(queryUrl, data, DEFAULT_YOUPORN_VIDEO_EXPIRY); await storeDataIntoRedis(queryUrl, data, DEFAULT_YOUPORN_VIDEO_EXPIRY);
} }
if (relatedData.length > 0) {
await storeDataIntoRedis(queryUrl + DEFAULT_RELATED_VIDEO_KEY_PATH, relatedData, DEFAULT_YOUPORN_GALLERY_EXPIRY);
}
return [ data, relatedData ] return [ data, relatedData ]
} }