mirror of
https://github.com/imputnet/cobalt.git
synced 2025-07-18 19:28:29 +00:00
feat: snapchat support
This commit is contained in:
parent
d98f2bfd87
commit
0dfc59a972
@ -25,6 +25,7 @@ import streamable from "./services/streamable.js";
|
|||||||
import twitch from "./services/twitch.js";
|
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";
|
||||||
|
|
||||||
export default async function(host, patternMatch, url, lang, obj) {
|
export default async function(host, patternMatch, url, lang, obj) {
|
||||||
assert(url instanceof URL);
|
assert(url instanceof URL);
|
||||||
@ -158,6 +159,15 @@ export default async function(host, patternMatch, url, lang, obj) {
|
|||||||
case "dailymotion":
|
case "dailymotion":
|
||||||
r = await dailymotion(patternMatch);
|
r = await dailymotion(patternMatch);
|
||||||
break;
|
break;
|
||||||
|
case "snapchat":
|
||||||
|
r = await snapchat({
|
||||||
|
url,
|
||||||
|
username: patternMatch.username,
|
||||||
|
storyId: patternMatch.storyId,
|
||||||
|
spotlightId: patternMatch.spotlightId,
|
||||||
|
shortLink: patternMatch.shortLink || false
|
||||||
|
});
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) });
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
||||||
}
|
}
|
||||||
|
@ -120,6 +120,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
|
|||||||
case "tumblr":
|
case "tumblr":
|
||||||
case "pinterest":
|
case "pinterest":
|
||||||
case "streamable":
|
case "streamable":
|
||||||
|
case "snapchat":
|
||||||
responseType = 1;
|
responseType = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
56
src/modules/processing/services/snapchat.js
Normal file
56
src/modules/processing/services/snapchat.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { genericUserAgent } from "../../config.js";
|
||||||
|
|
||||||
|
const SPOTLIGHT_VIDEO_REGEX = /<link data-react-helmet="true" rel="preload" href="(https:\/\/cf-st\.sc-cdn\.net\/d\/[\w.?=]+&uc=46)" as="video"\/>/;
|
||||||
|
|
||||||
|
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(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!link && obj.username && obj.storyId) {
|
||||||
|
link = `https://www.snapchat.com/add/${obj.username}/${obj.storyId}`
|
||||||
|
} else if (!link && obj.spotlightId) {
|
||||||
|
link = `https://www.snapchat.com/spotlight/${obj.spotlightId}`
|
||||||
|
} else if (link?.startsWith('https://www.snapchat.com/download')) {
|
||||||
|
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 id = path.split('/')[3];
|
||||||
|
const storyVideoRegex = new RegExp(`"snapId":{"value":"${id}"},"snapMediaType":1,"snapUrls":{"mediaUrl":"(https:\\/\\/bolt-gcdn\\.sc-cdn\\.net\\/3\/[^"]+)","mediaPreviewUrl"`);
|
||||||
|
const videoURL = html.match(storyVideoRegex)?.[1];
|
||||||
|
if (videoURL) return {
|
||||||
|
urls: videoURL,
|
||||||
|
filename: `snapchat_${id}.mp4`,
|
||||||
|
audioFilename: `snapchat_${id}_audio`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return { error: 'ErrorCouldntFetch' };
|
||||||
|
}
|
@ -116,6 +116,12 @@
|
|||||||
"alias": "dailymotion videos",
|
"alias": "dailymotion videos",
|
||||||
"patterns": ["video/:id"],
|
"patterns": ["video/:id"],
|
||||||
"enabled": true
|
"enabled": true
|
||||||
|
},
|
||||||
|
"snapchat": {
|
||||||
|
"alias": "snapchat stories & spotlights",
|
||||||
|
"subdomains": ["t", "story"],
|
||||||
|
"patterns": [":shortLink", "spotlight/:spotlightId", "add/:username/:storyId", "u/:username/:storyId"],
|
||||||
|
"enabled": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,11 @@ export const testers = {
|
|||||||
(patternMatch.author?.length <= 255 && patternMatch.song?.length <= 255)
|
(patternMatch.author?.length <= 255 && patternMatch.song?.length <= 255)
|
||||||
|| patternMatch.shortLink?.length <= 32,
|
|| patternMatch.shortLink?.length <= 32,
|
||||||
|
|
||||||
|
"snapchat": (patternMatch) =>
|
||||||
|
(patternMatch.username?.length <= 32 && patternMatch.storyId?.length <= 255)
|
||||||
|
|| patternMatch.spotlightId?.length <= 255
|
||||||
|
|| patternMatch.shortLink?.length <= 16,
|
||||||
|
|
||||||
"streamable": (patternMatch) =>
|
"streamable": (patternMatch) =>
|
||||||
patternMatch.id?.length === 6,
|
patternMatch.id?.length === 6,
|
||||||
|
|
||||||
|
@ -1115,5 +1115,22 @@
|
|||||||
"code": 200,
|
"code": 200,
|
||||||
"status": "stream"
|
"status": "stream"
|
||||||
}
|
}
|
||||||
|
}],
|
||||||
|
"snapchat": [{
|
||||||
|
"name": "spotlight",
|
||||||
|
"url": "https://www.snapchat.com/spotlight/W7_EDlXWTBiXAEEniNoMPwAAYdWxucG9pZmNqAY46j0a5AY46j0YbAAAAAQ",
|
||||||
|
"params": {},
|
||||||
|
"expected": {
|
||||||
|
"code": 200,
|
||||||
|
"status": "redirect"
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
"name": "shortlinked spotlight",
|
||||||
|
"url": "https://t.snapchat.com/4ZsiBLDi",
|
||||||
|
"params": {},
|
||||||
|
"expected": {
|
||||||
|
"code": 200,
|
||||||
|
"status": "redirect"
|
||||||
|
}
|
||||||
}]
|
}]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user