proxyraye-nextjs/src/utils/scrape/pornhub/url.ts

126 lines
3.9 KiB
TypeScript

import { PORNHUB_BASE_URL, PORNHUB_BASE_URL_GAY, PORNHUB_BASE_URL_GAY_SEARCH } from "@/constants/urls"
import axios, { AxiosHeaders } from "axios"
import { getHeadersWithCookie } from "../common/headers"
import { GalleryData, MindGeekVideoSrcElem, VideoSourceItem } from "@/meta/data"
import { Cookies, Platforms, PornHubOrientations } from "@/meta/settings"
import { getCookie } from "@/utils/cookies/read"
import { encodeUrl } from "@/utils/string"
import { DEFAULT_VIDEO_STREAM_ROUTE_PREFIX } from "@/constants/stream"
export const getPornHubQueryUrl = async (query?: string): Promise<string> => {
const orientation = await getCookie(Cookies.orientation)
if (query) {
return `${orientation && orientation.value == PornHubOrientations.gay ?
PORNHUB_BASE_URL_GAY_SEARCH :
PORNHUB_BASE_URL}/video/search?search=${query}`
}
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[]> => {
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: MindGeekVideoSrcElem) => ({
src: `${DEFAULT_VIDEO_STREAM_ROUTE_PREFIX}/${Platforms.pornhub}/${encodeUrl(elem?.videoUrl)}`,
type: 'video/mp4',
size: elem?.quality
})) as VideoSourceItem[]
return videos
} else {
return []
}
})
.catch(error => console.log(error))
return videos
}
function containsAtLeastThreeSpaces(input: string): boolean {
// Conta il numero di spazi nella stringa
const spaceCount = (input.match(/ /g) || []).length;
// Verifica se ci sono almeno tre spazi
return spaceCount >= 3;
}
export const getPornHubRelatedVideoData = async (url: string, sessionCookie: string): Promise<GalleryData[]> => {
const headersWithCookie = getHeadersWithCookie(PORNHUB_BASE_URL, sessionCookie)
let gallery: GalleryData[] = []
await axios.get(url, headersWithCookie)
.then(async response => {
if (response.data?.related) {
Array(response.data.related).map((related: any[], key) => {
related.map((rel: string[], key) => {
let galleryElem: GalleryData = {
videoUrl: '',
imgUrl: '',
text: '',
platform: Platforms.pornhub
}
rel.map((str, key) => {
if (String(str).includes('.jpg')) {
galleryElem.imgUrl = str;
}
if (String(str).includes('viewkey')) {
galleryElem.videoUrl = str.split('=')[1];
}
if (containsAtLeastThreeSpaces(String(str))) {
galleryElem.text = str;
}
})
gallery.push(galleryElem)
})
})
return gallery;
} else {
return []
}
})
.catch(error => console.log(error))
return gallery
}