mirror of
https://github.com/imputnet/cobalt.git
synced 2025-07-18 19:28:29 +00:00
ref(snapchat): rewrite service, new test, split redirects into a util
This commit is contained in:
parent
92e9941de5
commit
6d27a0bd73
@ -1,47 +1,29 @@
|
|||||||
import { genericUserAgent } from "../../config.js";
|
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.?=]+&uc=\d+)" as="video"\/>/;
|
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>$/;
|
const NEXT_DATA_REGEX = /<script id="__NEXT_DATA__" type="application\/json">({.+})<\/script><\/body><\/html>$/;
|
||||||
|
|
||||||
export default async function(obj) {
|
async function getSpotlight(pathname) {
|
||||||
let link;
|
const html = await fetch(`https://www.snapchat.com${pathname}`, {
|
||||||
if (obj.url.hostname === 't.snapchat.com' && obj.shortLink) {
|
headers: { 'User-Agent': genericUserAgent }
|
||||||
link = await fetch(`https://t.snapchat.com/${obj.shortLink}`, { redirect: "manual" }).then((r) => {
|
}).then((r) => r.text()).catch(() => null);
|
||||||
if (r.status === 303 && r.headers.get("location").startsWith("https://www.snapchat.com/")) {
|
|
||||||
return r.headers.get("location").split('?', 1)[0]
|
|
||||||
}
|
|
||||||
}).catch(() => {});
|
|
||||||
}
|
|
||||||
|
|
||||||
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}`
|
|
||||||
}
|
|
||||||
|
|
||||||
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' };
|
if (!html) return { error: 'ErrorCouldntFetch' };
|
||||||
|
|
||||||
const id = path.split('/')[2];
|
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,
|
||||||
filename: `snapchat_${id}.mp4`,
|
filename: `snapchat_${id}.mp4`,
|
||||||
audioFilename: `snapchat_${id}_audio`
|
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' };
|
|
||||||
|
|
||||||
|
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 nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
|
const nextDataString = html.match(NEXT_DATA_REGEX)?.[1];
|
||||||
if (nextDataString) {
|
if (nextDataString) {
|
||||||
@ -69,7 +51,7 @@ export default async function(obj) {
|
|||||||
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',
|
||||||
url: snap.snapUrls.mediaUrl,
|
url: snap.snapUrls.mediaUrl,
|
||||||
thumb: snap.snapUrls.mediaPreviewUrl.value
|
thumb: snap.snapUrls.mediaPreviewUrl.value
|
||||||
}))
|
}))
|
||||||
@ -77,6 +59,36 @@ export default async function(obj) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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' };
|
return { error: 'ErrorCouldntFetch' };
|
||||||
}
|
}
|
||||||
|
@ -127,3 +127,9 @@ export function cleanHTML(html) {
|
|||||||
clean = clean.replace(/\n/g, '');
|
clean = clean.replace(/\n/g, '');
|
||||||
return clean
|
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);
|
||||||
|
}
|
||||||
|
@ -1132,5 +1132,13 @@
|
|||||||
"code": 200,
|
"code": 200,
|
||||||
"status": "redirect"
|
"status": "redirect"
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
"name": "story",
|
||||||
|
"url": "https://www.snapchat.com/add/bazerkmakane",
|
||||||
|
"params": {},
|
||||||
|
"expected": {
|
||||||
|
"code": 200,
|
||||||
|
"status": "picker"
|
||||||
|
}
|
||||||
}]
|
}]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user