2024-07-24 17:06:10 +02:00
|
|
|
import { genericUserAgent } from "../../config.js";
|
2024-08-03 10:47:13 +02:00
|
|
|
import { getRedirectingURL } from "../../misc/utils.js";
|
2024-07-24 17:06:10 +02:00
|
|
|
import { extract, normalizeURL } from "../url.js";
|
|
|
|
|
|
|
|
const SPOTLIGHT_VIDEO_REGEX = /<link data-react-helmet="true" rel="preload" href="(https:\/\/cf-st\.sc-cdn\.net\/d\/[\w.?=]+&uc=\d+)" as="video"\/>/;
|
|
|
|
const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/;
|
|
|
|
|
|
|
|
async function getSpotlight(id) {
|
|
|
|
const html = await fetch(`https://www.snapchat.com/spotlight/${id}`, {
|
|
|
|
headers: { 'User-Agent': genericUserAgent }
|
|
|
|
}).then((r) => r.text()).catch(() => null);
|
2024-07-25 07:58:43 +02:00
|
|
|
if (!html) {
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
2024-07-24 17:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1];
|
|
|
|
if (videoURL) {
|
|
|
|
return {
|
|
|
|
urls: videoURL,
|
|
|
|
filename: `snapchat_${id}.mp4`,
|
|
|
|
audioFilename: `snapchat_${id}_audio`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getStory(username, storyId) {
|
|
|
|
const html = await fetch(`https://www.snapchat.com/add/${username}${storyId ? `/${storyId}` : ''}`, {
|
|
|
|
headers: { 'User-Agent': genericUserAgent }
|
|
|
|
}).then((r) => r.text()).catch(() => null);
|
2024-07-25 07:58:43 +02:00
|
|
|
if (!html) {
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
|
|
|
}
|
2024-07-24 17:06:10 +02:00
|
|
|
|
|
|
|
const nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
|
|
|
|
if (nextDataString) {
|
|
|
|
const data = JSON.parse(nextDataString);
|
|
|
|
const storyIdParam = data.query.profileParams[1];
|
|
|
|
|
|
|
|
if (storyIdParam && data.props.pageProps.story) {
|
|
|
|
const story = data.props.pageProps.story.snapList.find((snap) => snap.snapId.value === storyIdParam);
|
|
|
|
if (story) {
|
|
|
|
if (story.snapMediaType === 0) {
|
|
|
|
return {
|
|
|
|
urls: story.snapUrls.mediaUrl,
|
|
|
|
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 {
|
2024-08-06 17:31:43 +02:00
|
|
|
picker: defaultStory.snapList.map(snap => ({
|
2024-07-24 17:06:10 +02:00
|
|
|
type: snap.snapMediaType === 0 ? 'photo' : 'video',
|
|
|
|
url: snap.snapUrls.mediaUrl,
|
2024-08-06 17:31:43 +02:00
|
|
|
thumb: snap.snapMediaType ==! 0 ? snap.snapUrls.mediaPreviewUrl.value : undefined,
|
2024-07-24 17:06:10 +02:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 07:58:43 +02:00
|
|
|
export default async function (obj) {
|
2024-07-24 17:06:10 +02:00
|
|
|
let params = obj;
|
2024-08-03 12:50:57 +02:00
|
|
|
if (obj.shortLink) {
|
2024-07-24 17:06:10 +02:00
|
|
|
const link = await getRedirectingURL(`https://t.snapchat.com/${obj.shortLink}`);
|
2024-07-25 07:58:43 +02:00
|
|
|
|
|
|
|
if (!link?.startsWith('https://www.snapchat.com/')) {
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
|
|
|
}
|
2024-07-24 17:06:10 +02:00
|
|
|
|
|
|
|
const extractResult = extract(normalizeURL(link));
|
2024-07-25 07:58:43 +02:00
|
|
|
if (extractResult?.host !== 'snapchat') {
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
2024-07-24 17:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
params = extractResult.patternMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.spotlightId) {
|
|
|
|
const result = await getSpotlight(params.spotlightId);
|
|
|
|
if (result) return result;
|
|
|
|
} else if (params.username) {
|
|
|
|
const result = await getStory(params.username, params.storyId);
|
|
|
|
if (result) return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
|
|
|
}
|