api/snapchat: fix spotlight url extraction

This commit is contained in:
wukko 2024-09-09 18:40:59 +06:00
parent e2c8723c0e
commit 498e6f4419
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -3,19 +3,21 @@ import { genericUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js"; import { createStream } from "../../stream/manage.js";
import { getRedirectingURL } from "../../misc/utils.js"; import { getRedirectingURL } from "../../misc/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 SPOTLIGHT_VIDEO_REGEX = /<link data-react-helmet="true" rel="preload" href="(.+)" as="video"\/>/;
const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/; const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/;
async function getSpotlight(id) { async function getSpotlight(id) {
const html = await fetch(`https://www.snapchat.com/spotlight/${id}`, { const html = await fetch(`https://www.snapchat.com/spotlight/${id}`, {
headers: { 'user-agent': genericUserAgent } headers: { 'user-agent': genericUserAgent }
}).then((r) => r.text()).catch(() => null); }).then((r) => r.text()).catch(() => null);
if (!html) { if (!html) {
return { error: "fetch.fail" }; return { error: "fetch.fail" };
} }
const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1]; const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1];
if (videoURL) {
if (videoURL && new URL(videoURL).hostname.endsWith(".sc-cdn.net")) {
return { return {
urls: videoURL, urls: videoURL,
filename: `snapchat_${id}.mp4`, filename: `snapchat_${id}.mp4`,