add VideoAgent class

This commit is contained in:
La macchina desiderante 2024-05-12 15:39:58 +02:00
parent a58cc663d6
commit a77f009b2a
10 changed files with 70 additions and 27 deletions

View File

@ -1,13 +1,16 @@
import Layout from "@/components/Layout"; import Layout from "@/components/Layout";
import Home from "@/components/Pages/Home"; import Home from "@/components/Pages/Home";
import { VideoAgent } from "@/utils/agent";
import { fetchGalleryData } from '@/utils/scrape/xvideos/gallery'; import { getPlatformCookie } from "@/utils/cookies/read";
export default async function HomePage() { export default async function HomePage() {
const data = await fetchGalleryData()
const platform = await getPlatformCookie()
const data = await new VideoAgent(platform).getGallery()
return ( return (
<Layout> <Layout>
<Home data={data} /> <Home data={data} />

View File

@ -2,11 +2,14 @@ import Layout from "@/components/Layout";
import Search from "@/components/Pages/Search"; import Search from "@/components/Pages/Search";
import { fetchGalleryData } from "@/utils/scrape/xvideos/gallery"; import { VideoAgent } from "@/utils/agent";
import { getPlatformCookie } from "@/utils/cookies/read";
export default async function SearchPage({ params }: { params: { query: string } }) { export default async function SearchPage({ params }: { params: { query: string } }) {
const data = await fetchGalleryData({ query: params.query }) const platform = await getPlatformCookie()
const data = await new VideoAgent(platform).getGallery({ query: params.query })
return <Layout> return <Layout>
<Search data={data} query={decodeURIComponent(params.query)} /> <Search data={data} query={decodeURIComponent(params.query)} />

View File

@ -6,10 +6,9 @@ import Video from "@/components/Pages/Video";
import { decodeVideoUrlPath } from '@/utils/string'; import { decodeVideoUrlPath } from '@/utils/string';
import { fetchVideoData } from '@/utils/scrape/xvideos/video';
import { useLocale } from 'next-intl'; import { useLocale } from 'next-intl';
import { Platforms } from '@/meta/settings'; import { Platforms } from '@/meta/settings';
import { VideoAgent } from '@/utils/agent';
export default async function VideoPage({ params }: { params: { platform: Platforms, id: string } }) { export default async function VideoPage({ params }: { params: { platform: Platforms, id: string } }) {
@ -23,7 +22,9 @@ export default async function VideoPage({ params }: { params: { platform: Platfo
const decodedId = decodeVideoUrlPath(id) const decodedId = decodeVideoUrlPath(id)
const [data, related] = await fetchVideoData(decodedId) const [data, related] = await new VideoAgent(platform).getVideo(decodedId)
//const [data, related] = await fetchVideoData(decodedId)
if (!data.lowResUrl) { if (!data.lowResUrl) {
redirect(`/${locale}/404`) redirect(`/${locale}/404`)

View File

@ -1,5 +1,10 @@
import { Platforms } from "./settings" import { Platforms } from "./settings"
export interface FetchParams {
baseUrl?: string
query?: string
}
export interface GalleryData { export interface GalleryData {
videoUrl: string videoUrl: string
imgUrl: string imgUrl: string

View File

@ -27,6 +27,7 @@ export enum Themes {
} }
export const DEFAULT_THEME = Themes.light export const DEFAULT_THEME = Themes.light
export const DEFAULT_PLATFORM = Platforms.xvideos
export interface LangOption { export interface LangOption {
label: string; label: string;

33
src/utils/agent.ts Normal file
View File

@ -0,0 +1,33 @@
import { FetchParams, GalleryData, VideoData } from "@/meta/data";
import { Platforms } from "@/meta/settings";
import { fetchXVideosGalleryData } from "./scrape/xvideos/gallery";
import { fetchXvideosVideoData } from "./scrape/xvideos/video";
export class VideoAgent {
platform: Platforms;
constructor(platform: Platforms) {
this.platform = platform
}
public getGallery = async (params?: FetchParams): Promise<GalleryData[]> => {
switch (this.platform) {
case Platforms.xvideos:
return await fetchXVideosGalleryData(params)
default:
return []
}
}
public getVideo = async (id: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => {
switch (this.platform) {
case Platforms.xvideos:
return await fetchXvideosVideoData(id, params)
default:
return [{ lowResUrl: '' }, []]
}
}
}

View File

@ -1,11 +1,17 @@
'use server' 'use server'
import { Cookies as AppCookies, DEFAULT_PLATFORM, Platforms } from '@/meta/settings';
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
export async function getCookie(name: string) { export async function getCookie(name: AppCookies) {
return cookies().get(name); return cookies().get(name);
} }
export async function hasCookie(name: string):Promise<boolean> { export async function hasCookie(name: AppCookies):Promise<boolean> {
return cookies().has(name) return cookies().has(name)
} }
export async function getPlatformCookie(): Promise<Platforms> {
const platformCookie = await getCookie(AppCookies.platform)
return platformCookie && platformCookie.value ? platformCookie.value as Platforms : DEFAULT_PLATFORM
}

View File

@ -1,12 +1,13 @@
'use server' 'use server'
import { Cookies as AppCookies } from '@/meta/settings';
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
export async function deleteCookie(name: string) { export async function deleteCookie(name: AppCookies) {
cookies().delete(name) cookies().delete(name)
} }
export async function setCookie(name: string, value: string) { export async function setCookie(name: AppCookies, value: string) {
cookies().set(name, value, { cookies().set(name, value, {
sameSite: 'lax', sameSite: 'lax',
secure: false secure: false

View File

@ -1,4 +1,4 @@
import { GalleryData } from '@/meta/data'; import { FetchParams, GalleryData } from '@/meta/data';
import axios, { AxiosError } from 'axios'; import axios, { AxiosError } from 'axios';
import * as cheerio from "cheerio"; import * as cheerio from "cheerio";
@ -6,12 +6,7 @@ import { getHeaders } from '../headers';
import { getXVideosQueryUrl } from './url'; import { getXVideosQueryUrl } from './url';
import { Platforms } from '@/meta/settings'; import { Platforms } from '@/meta/settings';
interface FetchParams { export const fetchXVideosGalleryData = async (params?: FetchParams): Promise<GalleryData[]> => {
baseUrl?: string
query?: string
}
export const fetchGalleryData = async (params?: FetchParams): Promise<GalleryData[]> => {
let data: GalleryData[] = []; let data: GalleryData[] = [];

View File

@ -1,5 +1,5 @@
import { XVIDEOS_BASE_URL } from '@/constants/urls'; import { XVIDEOS_BASE_URL } from '@/constants/urls';
import { GalleryData, VideoData } from '@/meta/data'; import { FetchParams, GalleryData, VideoData } from '@/meta/data';
import axios, { AxiosError } from 'axios'; import axios, { AxiosError } from 'axios';
@ -7,12 +7,7 @@ import * as cheerio from "cheerio";
import { findRelatedVideos, findVideoUrlInsideTagStringByFunctionNameAndExtension } from '../../string'; import { findRelatedVideos, findVideoUrlInsideTagStringByFunctionNameAndExtension } from '../../string';
import { getHeaders } from '../headers'; import { getHeaders } from '../headers';
interface FetchParams { export const fetchXvideosVideoData = async (videoId: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => {
baseUrl?: string
query?: string
}
export const fetchVideoData = async (videoId: string, params?: FetchParams): Promise<[VideoData, GalleryData[]]> => {
let data: VideoData = { let data: VideoData = {
lowResUrl: '' lowResUrl: ''
@ -24,7 +19,7 @@ export const fetchVideoData = async (videoId: string, params?: FetchParams): Pro
const queryUrl = `${(params && params.baseUrl) ?? XVIDEOS_BASE_URL}${videoId}` const queryUrl = `${(params && params.baseUrl) ?? XVIDEOS_BASE_URL}${videoId}`
await axios.get(queryUrl, reqHeaders) await axios.get(queryUrl, reqHeaders)
.then(response => { .then(response => {