diff --git a/src/app/[locale]/video/[platform]/[id]/page.tsx b/src/app/[locale]/video/[platform]/[id]/page.tsx index 38c0abe..fc0b6f4 100644 --- a/src/app/[locale]/video/[platform]/[id]/page.tsx +++ b/src/app/[locale]/video/[platform]/[id]/page.tsx @@ -24,9 +24,7 @@ export default async function VideoPage({ params }: { params: { platform: Platfo const [data, related] = await new VideoAgent(platform).getVideo(decodedId) - //const [data, related] = await fetchVideoData(decodedId) - - if (!data.lowResUrl) { + if (!data.lowResUrl && (!data.srcSet || data.srcSet.length == 0)) { redirect(`/${locale}/404`) } diff --git a/src/components/Layout/Player/index.tsx b/src/components/Layout/Player/index.tsx index 5ca3312..4b286dd 100644 --- a/src/components/Layout/Player/index.tsx +++ b/src/components/Layout/Player/index.tsx @@ -27,7 +27,7 @@ const Player: React.FC = (props) => { controls: true, responsive: true, fluid: true, - sources: [{ + sources: data.srcSet ?? [{ src: videoSrc, type: videoType }] diff --git a/src/meta/data.ts b/src/meta/data.ts index 98a1d5b..cdac06b 100644 --- a/src/meta/data.ts +++ b/src/meta/data.ts @@ -12,15 +12,17 @@ export interface GalleryData { platform: Platforms } -export interface VideoSrcSet { - +export interface VideoSourceItem { + type: string, + src: string, + label: string } export interface VideoData { lowResUrl?: string, hiResUrl?: string, hlsUrl?: string - srcSet?: VideoSrcSet + srcSet?: VideoSourceItem[] } export interface VideoAgent { diff --git a/src/utils/scrape/pornhub/url.ts b/src/utils/scrape/pornhub/url.ts index 59e7f92..3c52d65 100644 --- a/src/utils/scrape/pornhub/url.ts +++ b/src/utils/scrape/pornhub/url.ts @@ -1,4 +1,12 @@ import { PORNHUB_BASE_URL } from "@/constants/urls" +import axios, { AxiosHeaders } from "axios" +import { getHeadersWithCookie } from "../common/headers" +import { VideoSourceItem } from "@/meta/data" + +interface PornHubVideoSrcElem { + videoUrl: string + quality: string +} export const getPornHubQueryUrl = async (query?: string): Promise => { if (query) { @@ -6,4 +14,33 @@ export const getPornHubQueryUrl = async (query?: string): Promise => { } return PORNHUB_BASE_URL +} + +export const getPornHubMediaUrlList = async (url: string, sessionCookie: string): Promise => { + + const headersWithCookie = getHeadersWithCookie(PORNHUB_BASE_URL, sessionCookie) + + let videos: VideoSourceItem[] = [] + + await axios.get(url, headersWithCookie) + .then(async response => { + + if (response.data) { + + videos = await response.data.map((elem: PornHubVideoSrcElem) => ({ + src: elem?.videoUrl, + type: 'video/mp4', + label: elem?.quality + })) as VideoSourceItem[] + + return videos + + } else { + return [] + } + + }) + .catch(error => console.log(error)) + + return videos } \ No newline at end of file diff --git a/src/utils/scrape/pornhub/video.ts b/src/utils/scrape/pornhub/video.ts index 2fc0c9c..9e949b8 100644 --- a/src/utils/scrape/pornhub/video.ts +++ b/src/utils/scrape/pornhub/video.ts @@ -1,5 +1,5 @@ import { PORNHUB_BASE_URL, PORNHUB_BASE_URL_VIDEO } from "@/constants/urls"; -import { FetchParams, GalleryData, VideoData } from "@/meta/data"; +import { FetchParams, GalleryData, VideoData, VideoSourceItem } from "@/meta/data"; import { getHeaders, getHeadersWithCookie } from "../common/headers"; import { getDataFromRedis, storeDataIntoRedis } from "@/redis/client"; import { DEFAULT_PORNHUB_VIDEO_EXPIRY, DEFAULT_RELATED_VIDEO_KEY_PATH } from "@/constants/redis"; @@ -8,15 +8,19 @@ import * as cheerio from "cheerio"; import axios, { AxiosError } from "axios"; import { createSessionCookie, findGetMediaUrlInTagblock } from "../common/mindgeek"; +import { getPornHubMediaUrlList } from "./url"; export const fetchPornHubVideoData = async (videoId: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => { let data: VideoData = { - lowResUrl: '' + lowResUrl: '', + srcSet: [] } let related: GalleryData[] = []; + let mediaUrl, sessionCookie, convertedData: VideoSourceItem[] + let reqHeaders = getHeaders(PORNHUB_BASE_URL) const queryUrl = `${PORNHUB_BASE_URL_VIDEO}${videoId.replace(/\//g, '')}` @@ -28,11 +32,11 @@ export const fetchPornHubVideoData = async (videoId: string, params?: FetchParam return [cachedVideoData as VideoData, cachedRelatedData as GalleryData[] ?? []] } - await axios.get(queryUrl, reqHeaders) + await axios.get(queryUrl, reqHeaders) .then(async response => { - const sessionCookie = response?.headers["set-cookie"] ? createSessionCookie(response?.headers["set-cookie"]) : ''; + sessionCookie = response?.headers["set-cookie"] ? createSessionCookie(response?.headers["set-cookie"]) : ''; const html = response.data; @@ -40,45 +44,35 @@ export const fetchPornHubVideoData = async (videoId: string, params?: FetchParam const scriptTags = $("script"); - // populate video data object - scriptTags.map(async (idx, elem) => { - const getMediaUrl = findGetMediaUrlInTagblock($(elem).toString()) + scriptTags.map((idx, elem) => { + const getMediaUrl = findGetMediaUrlInTagblock($(elem).toString()) ?? null if (getMediaUrl) { - - const headersWithCookie = getHeadersWithCookie(PORNHUB_BASE_URL, sessionCookie) - - await axios.get(getMediaUrl, headersWithCookie) - - .then(response => { - - console.log(response.data) - - // magic goes here - - }) - - .catch(error => console.log(error)) + mediaUrl = getMediaUrl } + // // populate related gallery + // scriptTags.map((idx, elem) => { + // // magic goes here + + // }) + + // await storeDataIntoRedis(queryUrl + DEFAULT_RELATED_VIDEO_KEY_PATH, related, DEFAULT_PORNHUB_VIDEO_EXPIRY); }) - await storeDataIntoRedis(queryUrl, data, DEFAULT_PORNHUB_VIDEO_EXPIRY); - - // populate related gallery - scriptTags.map((idx, elem) => { - - // magic goes here - - }) - - await storeDataIntoRedis(queryUrl + DEFAULT_RELATED_VIDEO_KEY_PATH, related, DEFAULT_PORNHUB_VIDEO_EXPIRY); - }).catch((error: AxiosError) => { - console.log(queryUrl) - console.log(error.message) + // error handling goes here }); - return [ { lowResUrl: 'https://www.w3schools.com/html/mov_bbb.mp4' } ,[]] + if (sessionCookie && mediaUrl) { + convertedData = await getPornHubMediaUrlList(mediaUrl, sessionCookie) + await storeDataIntoRedis(queryUrl, convertedData, DEFAULT_PORNHUB_VIDEO_EXPIRY); + + data.srcSet = convertedData.reverse() + } + + + return [ data, []] + // return [ { lowResUrl: 'https://www.w3schools.com/html/mov_bbb.mp4' } ,[]] } \ No newline at end of file diff --git a/src/utils/scrape/xvideos/gallery.ts b/src/utils/scrape/xvideos/gallery.ts index 36f2c35..0ddaa4f 100644 --- a/src/utils/scrape/xvideos/gallery.ts +++ b/src/utils/scrape/xvideos/gallery.ts @@ -8,12 +8,13 @@ import { Platforms } from '@/meta/settings'; import { getDataFromRedis, storeDataIntoRedis } from '@/redis/client'; import { DEFAULT_XVIDEOS_CONTENT_EXPIRY } from '@/constants/redis'; +import { XVIDEOS_BASE_URL } from '@/constants/urls'; export const fetchXVideosGalleryData = async (params?: FetchParams): Promise => { let data: GalleryData[] = []; - const reqHeaders = getHeaders() + const reqHeaders = getHeaders(XVIDEOS_BASE_URL) const queryUrl = await getXVideosQueryUrl(params?.query)