add PornHub orientations and switching mechanism

This commit is contained in:
La macchina desiderante 2024-05-24 21:45:42 +02:00
parent 6efe6582a5
commit b0710671b3
6 changed files with 75 additions and 21 deletions

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import { Cookies, XVideosOrientations } from '@/meta/settings'; import { Cookies, Platforms, XVideosOrientations, PornHubOrientations } from '@/meta/settings';
import css from './Orientation.module.scss' import css from './Orientation.module.scss'
@ -16,11 +16,21 @@ interface Props {
} }
} }
const getOrientations = (platform: Platforms):Object => {
if ([Platforms.xnxx, Platforms.xvideos].includes(platform)) {
return XVideosOrientations
}
return PornHubOrientations
}
const Orientation: React.FC<Props> = (props) => { const Orientation: React.FC<Props> = (props) => {
const { labels, handleClose } = props const { labels, handleClose } = props
const [cookies] = useCookies([Cookies.orientation]); const [cookies] = useCookies([Cookies.orientation, Cookies.platform]);
const orientationsList = cookies.platform ? getOrientations(cookies.platform ) : XVideosOrientations
const handleChange = async (event: React.ChangeEvent<HTMLSelectElement>) => { const handleChange = async (event: React.ChangeEvent<HTMLSelectElement>) => {
const value = event.target.value; const value = event.target.value;
@ -34,7 +44,7 @@ const Orientation: React.FC<Props> = (props) => {
<div className={css.container}> <div className={css.container}>
<div className={css.title}>{labels.title}</div> <div className={css.title}>{labels.title}</div>
<select defaultValue={ cookies.orientation ?? XVideosOrientations.etero } onChange={handleChange} name={'orientation'} aria-label={labels.title}> <select defaultValue={ cookies.orientation ?? XVideosOrientations.etero } onChange={handleChange} name={'orientation'} aria-label={labels.title}>
{Object.keys(XVideosOrientations).map((elem, key) => { {Object.keys(orientationsList).map((elem, key) => {
return <option className={css.option} key={key} value={elem}>{elem.toUpperCase()}</option> return <option className={css.option} key={key} value={elem}>{elem.toUpperCase()}</option>
})} })}
</select> </select>

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import { Cookies, Platforms } from '@/meta/settings'; import { Cookies, Platforms, PornHubOrientations, XVideosOrientations } from '@/meta/settings';
import css from './Platform.module.scss' import css from './Platform.module.scss'
@ -16,24 +16,41 @@ interface Props {
} }
} }
const mapOrientationToPlatform = (platform: string, orientation: string): string | undefined => {
if ([String(Platforms.xnxx), String(Platforms.xvideos)].includes(platform)) {
return Object.keys(XVideosOrientations).includes(orientation) ? orientation : String(XVideosOrientations.etero)
}
if ([String(Platforms.pornhub)].includes(platform)) {
return Object.keys(PornHubOrientations).includes(orientation) ? orientation : String(PornHubOrientations.generic)
}
}
const Platform: React.FC<Props> = (props) => { const Platform: React.FC<Props> = (props) => {
const { labels, handleClose } = props const { labels, handleClose } = props
const [cookies] = useCookies([Cookies.platform]); const [cookies] = useCookies([Cookies.platform, Cookies.orientation]);
const handleChange = async (event: React.ChangeEvent<HTMLSelectElement>) => { const handleChange = async (event: React.ChangeEvent<HTMLSelectElement>) => {
const value = event.target.value; const value = event.target.value;
await setCookie(Cookies.platform, value) await setCookie(Cookies.platform, value)
if (cookies.orientation) {
const newOrientation = mapOrientationToPlatform(value, cookies.orientation)
newOrientation && await setCookie(Cookies.orientation, newOrientation)
}
handleClose() handleClose()
} }
return ( return (
<div className={css.container}> <div className={css.container}>
<div className={css.title}>{labels.title}</div> <div className={css.title}>{labels.title}</div>
<select defaultValue={ cookies.platform ?? Platforms.xvideos } onChange={handleChange} name={'platform'} aria-label={labels.title}> <select defaultValue={cookies.platform ?? Platforms.xvideos} onChange={handleChange} name={'platform'} aria-label={labels.title}>
{Object.keys(Platforms).map((elem, key) => { {Object.keys(Platforms).map((elem, key) => {
return <option className={css.option} key={key} value={elem}>{elem.toUpperCase()}</option> return <option className={css.option} key={key} value={elem}>{elem.toUpperCase()}</option>
})} })}

View File

@ -18,4 +18,7 @@ export const XNXX_BASE_SEARCH: string = 'https://www.xnxx.com/search'
// PORNHUB // PORNHUB
export const PORNHUB_BASE_URL: string = 'https://www.pornhub.com' export const PORNHUB_BASE_URL: string = 'https://www.pornhub.com'
export const PORNHUB_BASE_URL_VIDEO: string = 'https://www.pornhub.com/view_video.php?viewkey=' export const PORNHUB_BASE_URL_VIDEO: string = 'https://www.pornhub.com/view_video.php?viewkey='
export const PORNHUB_BASE_URL_GAY: string = 'https://www.pornhub.com/gayporn'
export const PORNHUB_BASE_URL_GAY_SEARCH: string = 'https://www.pornhub.com/gay'

View File

@ -22,6 +22,11 @@ export enum XVideosOrientations {
trans= 'trans' trans= 'trans'
} }
export enum PornHubOrientations {
generic= 'generic',
gay= 'gay'
}
export enum Themes { export enum Themes {
light= 'light', light= 'light',
dark= 'dark', dark= 'dark',

View File

@ -2,7 +2,7 @@ import { PORNHUB_BASE_URL } from "@/constants/urls";
import { FetchParams, GalleryData } from "@/meta/data"; import { FetchParams, GalleryData } 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 { getPornHubQueryUrl } from "./url"; import { getPornHubQueryUrl, getPornHubResultsWrapperId } from "./url";
import * as cheerio from "cheerio"; import * as cheerio from "cheerio";
@ -33,7 +33,7 @@ export const fetchPornHubGalleryData = async (params?: FetchParams): Promise<Gal
const $ = cheerio.load(html); const $ = cheerio.load(html);
const wrapperId = params?.query ? "#videoSearchResult li" : "#singleFeedSection li" const wrapperId = await getPornHubResultsWrapperId(params?.query)
const thumbs = $(wrapperId); const thumbs = $(wrapperId);

View File

@ -1,8 +1,9 @@
import { PORNHUB_BASE_URL } from "@/constants/urls" import { PORNHUB_BASE_URL, PORNHUB_BASE_URL_GAY, PORNHUB_BASE_URL_GAY_SEARCH } from "@/constants/urls"
import axios, { AxiosHeaders } from "axios" import axios, { AxiosHeaders } from "axios"
import { getHeadersWithCookie } from "../common/headers" import { getHeadersWithCookie } from "../common/headers"
import { GalleryData, VideoSourceItem } from "@/meta/data" import { GalleryData, VideoSourceItem } from "@/meta/data"
import { Platforms } from "@/meta/settings" import { Cookies, Platforms, PornHubOrientations } from "@/meta/settings"
import { getCookie } from "@/utils/cookies/read"
interface PornHubVideoSrcElem { interface PornHubVideoSrcElem {
videoUrl: string videoUrl: string
@ -10,11 +11,29 @@ interface PornHubVideoSrcElem {
} }
export const getPornHubQueryUrl = async (query?: string): Promise<string> => { export const getPornHubQueryUrl = async (query?: string): Promise<string> => {
const orientation = await getCookie(Cookies.orientation)
if (query) { if (query) {
return `${PORNHUB_BASE_URL}/video/search?search=${query}` return `${orientation && orientation.value == PornHubOrientations.gay ?
PORNHUB_BASE_URL_GAY_SEARCH :
PORNHUB_BASE_URL}/video/search?search=${query}`
} }
return PORNHUB_BASE_URL return orientation && orientation.value == PornHubOrientations.gay ? PORNHUB_BASE_URL_GAY : PORNHUB_BASE_URL
}
export const getPornHubResultsWrapperId = async (query?: string): Promise<string> => {
const orientation = await getCookie(Cookies.orientation)
if (query) {
return "#videoSearchResult li"
}
if (orientation && orientation.value == PornHubOrientations.gay) {
return "#videoCategory li"
}
return "#singleFeedSection li"
} }
export const getPornHubMediaUrlList = async (url: string, sessionCookie: string): Promise<VideoSourceItem[]> => { export const getPornHubMediaUrlList = async (url: string, sessionCookie: string): Promise<VideoSourceItem[]> => {
@ -47,10 +66,10 @@ export const getPornHubMediaUrlList = async (url: string, sessionCookie: string)
} }
function containsAtLeastThreeSpaces(input: string): boolean { function containsAtLeastThreeSpaces(input: string): boolean {
// Conta il numero di spazi nella stringa // Conta il numero di spazi nella stringa
const spaceCount = (input.match(/ /g) || []).length; const spaceCount = (input.match(/ /g) || []).length;
// Verifica se ci sono almeno tre spazi // Verifica se ci sono almeno tre spazi
return spaceCount >= 3; return spaceCount >= 3;
} }
export const getPornHubRelatedVideoData = async (url: string, sessionCookie: string): Promise<GalleryData[]> => { export const getPornHubRelatedVideoData = async (url: string, sessionCookie: string): Promise<GalleryData[]> => {
@ -72,7 +91,7 @@ export const getPornHubRelatedVideoData = async (url: string, sessionCookie: str
videoUrl: '', videoUrl: '',
imgUrl: '', imgUrl: '',
text: '', text: '',
platform: Platforms.pornhub platform: Platforms.pornhub
} }
rel.map((str, key) => { rel.map((str, key) => {
@ -80,11 +99,11 @@ export const getPornHubRelatedVideoData = async (url: string, sessionCookie: str
if (String(str).includes('.jpg')) { if (String(str).includes('.jpg')) {
galleryElem.imgUrl = str; galleryElem.imgUrl = str;
} }
if (String(str).includes('viewkey')) { if (String(str).includes('viewkey')) {
galleryElem.videoUrl = str.split('=')[1]; galleryElem.videoUrl = str.split('=')[1];
} }
if (containsAtLeastThreeSpaces(String(str))) { if (containsAtLeastThreeSpaces(String(str))) {
galleryElem.text = str; galleryElem.text = str;
} }