Files
traxxx/src/scrapers/manyvids.js
T

259 lines
6.3 KiB
JavaScript
Executable File

'use strict';
const unprint = require('unprint');
const slugify = require('../utils/slugify');
function splitTag(tag) {
return tag
.replace(/([a-z])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2') // handle sequence of caps
.toLowerCase()
.trim();
}
function getPoster(url) {
if (!url) {
return null;
}
return {
src: url,
expectType: {
// older ffmpeg-generated screenshots are served as octet-stream, but they're regular JPEGs
'application/octet-stream': 'image/jpeg',
'binary/octet-stream': 'image/jpeg',
},
};
}
async function getTrailer(videoId) {
if (!videoId) {
return null;
}
const res = await unprint.get(`https://api.manyvids.com/store/video/${videoId}/private`);
if (res.ok && res.data?.statusCode === 200) {
return res.data.data.teaser.filepath;
}
return null;
}
/*
function getModelUrl(modelData) {
const slug = modelData.slug || modelData.profileUrl?.match(/\/profile\/\d+\/(\w+)/i)?.[1] || null;
if (slug) {
return `https://${slug}.manyvids.com`;
}
return null;
}
*/
async function scrapeScene(data, context) {
const release = {};
release.url = data.url
? `https://www.manyvids.com${data.url}`
: `https://www.manyvids.com/Video/${data.id}/${data.slug || ''}`;
release.entryId = data.id;
release.attributes = {
price: data.price?.regular,
discount: data.price?.onSale ? data.price.discountedPrice : null, // reported as "0" when not on sale
isFree: data.price?.free,
isCustomOrder: data.isCustomOrder,
isPremium: data.isPremium,
isPrivate: data.isPrivate,
isExclusive: data.isExclusive,
isStreamOnly: data.isStreamOnly,
};
release.title = data.title;
release.description = data.description;
release.date = new Date(data.launchDate);
release.duration = unprint.extractDuration(data.duration || data.videoDuration);
/* many channels operate more like a studio than a performer, so the channel information doesn't represent a person
const model = data.creator || data.model;
release.actors = [{
name: model.stageName || model.displayName,
entryId: model.id,
avatar: model.avatar?.url || model.avatar,
url: getModelUrl(model),
}];
*/
release.tags = [
data.is3D && '3d',
data.isAiGenerated && 'ai',
...data.tagList?.map((tag) => splitTag(tag.label)) || [],
].filter(Boolean);
release.poster = getPoster(data.thumbnail?.url || data.screenshot || data.thumbnail); // thumbnail URL is full size, thumbnail direct is thumb sized
if (data.preview?.url) {
release.trailer = data.preview.url;
} else if (context.includeTrailers) {
release.trailer = await getTrailer(data.id);
}
// the shortest side, so vertical videos aren't reported as 1920p
release.qualities = [data.width && data.height ? Math.min(data.width, data.height) : data.height].filter(Boolean);
return release;
}
const profileIdRegex = /\/profile\/(\d+)/i;
async function getProfileId(channel) {
if (channel.url) {
const { hostname, pathname } = new URL(channel.url);
if (profileIdRegex.test(pathname)) { // full profile URL
const urlProfileId = new URL(channel.url).pathname.match(profileIdRegex)?.[1];
if (urlProfileId) {
return urlProfileId;
}
}
if (/[\w-]+.manyvids.com/.test(hostname)) { // short profile URL
const idRes = await unprint.request(channel.url, {
method: 'head',
followRedirects: 0,
});
const profileId = idRes.headers.get('location')?.match(profileIdRegex)?.[1];
if (profileId) {
return profileId;
}
}
// sadly we can't reliably reconstruct short profile URL from the slug
}
return null;
}
async function fetchLatest(channel, page = 1) {
const profileId = await getProfileId(channel);
if (profileId) {
const res = await unprint.get(`https://api.manyvids.com/store/videos/${profileId}?sort=newest&limit=100&page=${page}`);
if (res.ok && res.data?.statusCode === 200) {
return Promise.all(res.data.data.map(async (data) => scrapeScene(data, channel)));
}
}
return null;
}
async function fetchScene(url, channel, baseRelease, context) {
const videoId = new URL(url).pathname.match(/\/video\/(\d+)/i)?.[1];
if (!videoId) {
return null;
}
const res = await unprint.get(`https://api.manyvids.com/store/video/${videoId}`);
if (res.ok && res.data?.statusCode === 200) {
return scrapeScene(res.data.data, {
channel,
includeTrailers: context.includeTrailers,
isDeep: !!baseRelease.entryId,
});
}
return res.status;
}
function scrapeProfile(data) {
const profile = {};
profile.description = [data.bio, data.description].filter(Boolean).join(' | ') || null;
profile.placeOfResidence = data.location || null;
profile.ethnicity = data.ethnicity || null;
profile.hairColor = data.hairColor || null;
profile.gender = data.identification?.toLowerCase() || null;
profile.orientation = data.orientation;
profile.avatar = data.avatar || null;
profile.photos = [data.portrait].filter(Boolean);
profile.socials = [data.socLinkTwitter, data.socLinkInstagram, data.socLinkReddit].filter(Boolean);
const dateOfBirth = data.dob && unprint.extractDate(data.dob);
if (dateOfBirth && new Date().getFullYear() - dateOfBirth.getFullYear() > 18) {
// date of births that would not be legal are likely bogus
profile.dateOfBirth = dateOfBirth;
}
return profile;
}
async function getActorUrl(actor) {
if (actor.url) {
const idRes = await unprint.request(actor.url, {
method: 'head',
followRedirects: 0,
});
const profileId = idRes.headers.get('location')?.match(profileIdRegex)?.[1];
if (profileId) {
return `https://api.manyvids.com/profile/profile/${profileId}`;
}
}
const res = await unprint.get(`https://api.manyvids.com/search/autocomplete?keywords=${slugify(actor.name, '+')}`);
if (res.ok) {
const model = res.data?.models?.find((modelResult) => modelResult.stage_name === actor.name || modelResult.username === actor.name);
if (model) {
const profileId = model.url?.match(/\/profile\/(\d+)/i)?.[1];
if (profileId) {
return `https://api.manyvids.com/profile/profile/${profileId}`;
}
}
}
return null;
}
async function fetchProfile(actor, entity) {
const actorUrl = await getActorUrl(actor);
if (actorUrl) {
const res = await unprint.get(actorUrl);
if (res.ok && res.data?.username) {
return scrapeProfile(res.data, entity);
}
return res.status;
}
return actorUrl;
}
module.exports = {
fetchLatest,
fetchProfile,
fetchScene,
};