ref(snapchat): rewrite service, new test, split redirects into a util

This commit is contained in:
Snazzah 2024-05-15 20:11:53 -05:00
parent 92e9941de5
commit 6d27a0bd73
No known key found for this signature in database
GPG Key ID: EA479766A94CEB61
3 changed files with 87 additions and 61 deletions

View File

@ -1,82 +1,94 @@
import { genericUserAgent } from "../../config.js";
import { getRedirectingURL } from "../../sub/utils.js";
const SPOTLIGHT_VIDEO_REGEX = /<link data-react-helmet="true" rel="preload" href="(https:\/\/cf-st\.sc-cdn\.net\/d\/[\w.?=]+&amp;uc=\d+)" as="video"\/>/;
const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/;
export default async function(obj) {
let link;
if (obj.url.hostname === 't.snapchat.com' && obj.shortLink) {
link = await fetch(`https://t.snapchat.com/${obj.shortLink}`, { redirect: "manual" }).then((r) => {
if (r.status === 303 && r.headers.get("location").startsWith("https://www.snapchat.com/")) {
return r.headers.get("location").split('?', 1)[0]
}
}).catch(() => {});
async function getSpotlight(pathname) {
const html = await fetch(`https://www.snapchat.com${pathname}`, {
headers: { 'User-Agent': genericUserAgent }
}).then((r) => r.text()).catch(() => null);
if (!html) return { error: 'ErrorCouldntFetch' };
const id = pathname.split('/')[2];
const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1];
if (videoURL) return {
urls: videoURL,
filename: `snapchat_${id}.mp4`,
audioFilename: `snapchat_${id}_audio`
}
}
if (!link && obj.username && obj.storyId) {
link = `https://www.snapchat.com/add/${obj.username}/${obj.storyId}`
} if (!link && obj.username) {
link = `https://www.snapchat.com/add/${obj.username}`
} else if (!link && obj.spotlightId) {
link = `https://www.snapchat.com/spotlight/${obj.spotlightId}`
}
async function getStory(pathname) {
const html = await fetch(`https://www.snapchat.com${pathname}`, {
headers: { 'User-Agent': genericUserAgent }
}).then((r) => r.text()).catch(() => null);
if (!html) return { error: 'ErrorCouldntFetch' };
const path = new URL(link).pathname;
if (path.startsWith('/spotlight/')) {
const html = await fetch(link, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
if (!html) return { error: 'ErrorCouldntFetch' };
const id = path.split('/')[2];
const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1];
if (videoURL) return {
urls: videoURL,
filename: `snapchat_${id}.mp4`,
audioFilename: `snapchat_${id}_audio`
}
} else if (path.startsWith('/add/')) {
const html = await fetch(link, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
if (!html) return { error: 'ErrorCouldntFetch' };
const nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
if (nextDataString) {
const data = JSON.parse(nextDataString);
const storyId = data.query.profileParams[1];
if (storyId && data.props.pageProps.story) {
const story = data.props.pageProps.story.snapList.find((snap) => snap.snapId.value === storyId);
if (story) {
if (story.snapMediaType === 0)
return {
urls: story.snapUrls.mediaUrl,
isPhoto: true
}
const nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
if (nextDataString) {
const data = JSON.parse(nextDataString);
const storyId = data.query.profileParams[1];
if (storyId && data.props.pageProps.story) {
const story = data.props.pageProps.story.snapList.find((snap) => snap.snapId.value === storyId);
if (story) {
if (story.snapMediaType === 0)
return {
urls: story.snapUrls.mediaUrl,
filename: `snapchat_${storyId}.mp4`,
audioFilename: `snapchat_${storyId}_audio`
isPhoto: true
}
return {
urls: story.snapUrls.mediaUrl,
filename: `snapchat_${storyId}.mp4`,
audioFilename: `snapchat_${storyId}_audio`
}
}
}
const defaultStory = data.props.pageProps.curatedHighlights[0];
if (defaultStory)
return {
picker: defaultStory.snapList.map((snap) => ({
type: snap.snapMediaType === 0 ? "photo" : "video",
url: snap.snapUrls.mediaUrl,
thumb: snap.snapUrls.mediaPreviewUrl.value
}))
}
const defaultStory = data.props.pageProps.curatedHighlights[0];
if (defaultStory)
return {
picker: defaultStory.snapList.map((snap) => ({
type: snap.snapMediaType === 0 ? 'photo' : 'video',
url: snap.snapUrls.mediaUrl,
thumb: snap.snapUrls.mediaPreviewUrl.value
}))
}
}
}
export default async function(obj) {
try {
let pathname;
if (obj.url.hostname === 't.snapchat.com' && obj.shortLink) {
const link = await getRedirectingURL(`https://t.snapchat.com/${obj.shortLink}`);
if (link && !link.startsWith('https://www.snapchat.com/')) return { error: 'ErrorCouldntFetch' };
pathname = new URL(link).pathname;
}
if (!pathname) {
if (obj.username && obj.storyId) {
pathname = `/add/${obj.username}/${obj.storyId}`;
} else if (obj.username) {
pathname = `/add/${obj.username}`;
} else if (obj.spotlightId) {
pathname = `/spotlight/${obj.spotlightId}`;
}
}
if (pathname.startsWith('/spotlight/')) {
const result = await getSpotlight(pathname);
if (result) return result;
} else if (pathname.startsWith('/add/')) {
const result = await getStory(pathname);
if (result) return result;
}
} catch (e) {
console.log(e)
}
return { error: 'ErrorCouldntFetch' };
}

View File

@ -127,3 +127,9 @@ export function cleanHTML(html) {
clean = clean.replace(/\n/g, '');
return clean
}
export async function getRedirectingURL(url) {
return await fetch(url, { redirect: 'manual' }).then((r) => {
if ([301, 302, 303].includes(r.status) && r.headers.has('location'))
return r.headers.get('location');
}).catch(() => null);
}

View File

@ -1132,5 +1132,13 @@
"code": 200,
"status": "redirect"
}
}, {
"name": "story",
"url": "https://www.snapchat.com/add/bazerkmakane",
"params": {},
"expected": {
"code": 200,
"status": "picker"
}
}]
}