fix(snapchat): fix merge, clean up code with new utils

This commit is contained in:
Snazzah 2024-05-22 06:49:36 -05:00
parent cb9956f502
commit d725829743
No known key found for this signature in database
GPG Key ID: EA479766A94CEB61
2 changed files with 18 additions and 26 deletions

View File

@ -25,7 +25,6 @@ import twitch from "./services/twitch.js";
import rutube from "./services/rutube.js"; import rutube from "./services/rutube.js";
import dailymotion from "./services/dailymotion.js"; import dailymotion from "./services/dailymotion.js";
import snapchat from "./services/snapchat.js"; import snapchat from "./services/snapchat.js";
import { env } from '../config.js';
let freebind; let freebind;

View File

@ -1,16 +1,16 @@
import { genericUserAgent } from "../../config.js"; import { genericUserAgent } from "../../config.js";
import { getRedirectingURL } from "../../sub/utils.js"; import { getRedirectingURL } from "../../sub/utils.js";
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.?=]+&amp;uc=\d+)" as="video"\/>/; 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>$/; const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/;
async function getSpotlight(pathname) { async function getSpotlight(id) {
const html = await fetch(`https://www.snapchat.com${pathname}`, { 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) return { error: 'ErrorCouldntFetch' }; if (!html) return { error: 'ErrorCouldntFetch' };
const id = pathname.split('/')[2];
const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1]; const videoURL = html.match(SPOTLIGHT_VIDEO_REGEX)?.[1];
if (videoURL) return { if (videoURL) return {
urls: videoURL, urls: videoURL,
@ -19,8 +19,8 @@ async function getSpotlight(pathname) {
} }
} }
async function getStory(pathname) { async function getStory(username, storyId) {
const html = await fetch(`https://www.snapchat.com${pathname}`, { const html = await fetch(`https://www.snapchat.com/add/${username}${storyId ? `/${storyId}` : ''}`, {
headers: { 'User-Agent': genericUserAgent } headers: { 'User-Agent': genericUserAgent }
}).then((r) => r.text()).catch(() => null); }).then((r) => r.text()).catch(() => null);
if (!html) return { error: 'ErrorCouldntFetch' }; if (!html) return { error: 'ErrorCouldntFetch' };
@ -28,10 +28,10 @@ async function getStory(pathname) {
const nextDataString = html.match(NEXT_DATA_REGEX)?.[1]; const nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
if (nextDataString) { if (nextDataString) {
const data = JSON.parse(nextDataString); const data = JSON.parse(nextDataString);
const storyId = data.query.profileParams[1]; const storyIdParam = data.query.profileParams[1];
if (storyId && data.props.pageProps.story) { if (storyIdParam && data.props.pageProps.story) {
const story = data.props.pageProps.story.snapList.find((snap) => snap.snapId.value === storyId); const story = data.props.pageProps.story.snapList.find((snap) => snap.snapId.value === storyIdParam);
if (story) { if (story) {
if (story.snapMediaType === 0) if (story.snapMediaType === 0)
return { return {
@ -48,7 +48,7 @@ async function getStory(pathname) {
} }
const defaultStory = data.props.pageProps.curatedHighlights[0]; const defaultStory = data.props.pageProps.curatedHighlights[0];
if (defaultStory) if (defaultStory) {
return { return {
picker: defaultStory.snapList.map((snap) => ({ picker: defaultStory.snapList.map((snap) => ({
type: snap.snapMediaType === 0 ? 'photo' : 'video', type: snap.snapMediaType === 0 ? 'photo' : 'video',
@ -58,30 +58,23 @@ async function getStory(pathname) {
} }
} }
} }
}
export default async function(obj) { export default async function(obj) {
let pathname; let params = obj;
if (obj.url.hostname === 't.snapchat.com' && obj.shortLink) { if (obj.url.hostname === 't.snapchat.com' && obj.shortLink) {
const link = await getRedirectingURL(`https://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' }; if (!link || !link.startsWith('https://www.snapchat.com/')) return { error: 'ErrorCouldntFetch' };
pathname = new URL(link).pathname; const extractResult = extract(normalizeURL(link));
if (!extractResult || extractResult.host !== 'snapchat') return { error: 'ErrorCouldntFetch' };
params = extractResult.patternMatch;
} }
if (!pathname) { if (params.spotlightId) {
if (obj.username && obj.storyId) { const result = await getSpotlight(params.spotlightId);
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; if (result) return result;
} else if (pathname.startsWith('/add/')) { } else if (params.username) {
const result = await getStory(pathname); const result = await getStory(params.username, params.storyId);
if (result) return result; if (result) return result;
} }