mirror of
https://github.com/imputnet/cobalt.git
synced 2025-01-01 12:46:07 +00:00
reddit: add support for user post links & clean up (#484)
This commit is contained in:
parent
080fc043ea
commit
a5a01cc0c6
|
@ -80,7 +80,8 @@ export default async function(host, patternMatch, url, lang, obj) {
|
||||||
case "reddit":
|
case "reddit":
|
||||||
r = await reddit({
|
r = await reddit({
|
||||||
sub: patternMatch.sub,
|
sub: patternMatch.sub,
|
||||||
id: patternMatch.id
|
id: patternMatch.id,
|
||||||
|
user: patternMatch.user
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "tiktok":
|
case "tiktok":
|
||||||
|
|
|
@ -48,43 +48,73 @@ async function getAccessToken() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function(obj) {
|
export default async function(obj) {
|
||||||
const url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}.json`);
|
let url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}.json`);
|
||||||
|
|
||||||
|
if (obj.user) {
|
||||||
|
url.pathname = `/user/${obj.user}/comments/${obj.id}.json`;
|
||||||
|
}
|
||||||
|
|
||||||
const accessToken = await getAccessToken();
|
const accessToken = await getAccessToken();
|
||||||
if (accessToken) url.hostname = 'oauth.reddit.com';
|
if (accessToken) url.hostname = 'oauth.reddit.com';
|
||||||
|
|
||||||
let data = await fetch(
|
let data = await fetch(
|
||||||
url, { headers: accessToken && { authorization: `Bearer ${accessToken}` } }
|
url, {
|
||||||
).then((r) => { return r.json() }).catch(() => { return false });
|
headers: accessToken && { authorization: `Bearer ${accessToken}` }
|
||||||
if (!data) return { error: 'ErrorCouldntFetch' };
|
}
|
||||||
|
).then(r => r.json() ).catch(() => {});
|
||||||
|
|
||||||
data = data[0]["data"]["children"][0]["data"];
|
if (!data || !Array.isArray(data)) return { error: 'ErrorCouldntFetch' };
|
||||||
|
|
||||||
if (data.url.endsWith('.gif')) return { typeId: 1, urls: data.url };
|
data = data[0]?.data?.children[0]?.data;
|
||||||
|
|
||||||
if (!("reddit_video" in data["secure_media"])) return { error: 'ErrorEmptyDownload' };
|
if (data?.url?.endsWith('.gif')) return {
|
||||||
if (data["secure_media"]["reddit_video"]["duration"] * 1000 > maxVideoDuration) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
|
typeId: 1,
|
||||||
|
urls: data.url
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.secure_media?.reddit_video)
|
||||||
|
return { error: 'ErrorEmptyDownload' };
|
||||||
|
|
||||||
|
if (data.secure_media?.reddit_video?.duration * 1000 > maxVideoDuration)
|
||||||
|
return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
|
||||||
|
|
||||||
let audio = false,
|
let audio = false,
|
||||||
video = data["secure_media"]["reddit_video"]["fallback_url"].split('?')[0],
|
video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0],
|
||||||
audioFileLink = video.match('.mp4') ? `${video.split('_')[0]}_audio.mp4` : `${data["secure_media"]["reddit_video"]["fallback_url"].split('DASH')[0]}audio`;
|
audioFileLink = `${data.secure_media?.reddit_video?.fallback_url?.split('DASH')[0]}audio`;
|
||||||
|
|
||||||
await fetch(audioFileLink, { method: "HEAD" }).then((r) => { if (Number(r.status) === 200) audio = true }).catch(() => { audio = false });
|
if (video.match('.mp4')) {
|
||||||
|
audioFileLink = `${video.split('_')[0]}_audio.mp4`
|
||||||
|
}
|
||||||
|
|
||||||
|
// test the existence of audio
|
||||||
|
await fetch(audioFileLink, { method: "HEAD" }).then((r) => {
|
||||||
|
if (Number(r.status) === 200) {
|
||||||
|
audio = true
|
||||||
|
}
|
||||||
|
}).catch(() => {})
|
||||||
|
|
||||||
// fallback for videos with variable audio quality
|
// fallback for videos with variable audio quality
|
||||||
if (!audio) {
|
if (!audio) {
|
||||||
audioFileLink = `${video.split('_')[0]}_AUDIO_128.mp4`
|
audioFileLink = `${video.split('_')[0]}_AUDIO_128.mp4`
|
||||||
await fetch(audioFileLink, { method: "HEAD" }).then((r) => { if (Number(r.status) === 200) audio = true }).catch(() => { audio = false });
|
await fetch(audioFileLink, { method: "HEAD" }).then((r) => {
|
||||||
|
if (Number(r.status) === 200) {
|
||||||
|
audio = true
|
||||||
|
}
|
||||||
|
}).catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = video.split('/')[3];
|
let id = video.split('/')[3];
|
||||||
|
|
||||||
if (!audio) return { typeId: 1, urls: video };
|
if (!audio) return {
|
||||||
|
typeId: 1,
|
||||||
|
urls: video
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
typeId: 2,
|
typeId: 2,
|
||||||
type: "render",
|
type: "render",
|
||||||
urls: [video, audioFileLink],
|
urls: [video, audioFileLink],
|
||||||
audioFilename: `reddit_${id}_audio`,
|
audioFilename: `reddit_${id}_audio`,
|
||||||
filename: `reddit_${id}.mp4`
|
filename: `reddit_${id}.mp4`
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
},
|
},
|
||||||
"reddit": {
|
"reddit": {
|
||||||
"alias": "reddit videos & gifs",
|
"alias": "reddit videos & gifs",
|
||||||
"patterns": ["r/:sub/comments/:id/:title"],
|
"patterns": ["r/:sub/comments/:id/:title", "user/:user/comments/:id/:title"],
|
||||||
"subdomains": "*",
|
"subdomains": "*",
|
||||||
"enabled": true
|
"enabled": true
|
||||||
},
|
},
|
||||||
|
|
|
@ -16,7 +16,8 @@ export const testers = {
|
||||||
patternMatch.id?.length <= 128 || patternMatch.shortLink?.length <= 32,
|
patternMatch.id?.length <= 128 || patternMatch.shortLink?.length <= 32,
|
||||||
|
|
||||||
"reddit": (patternMatch) =>
|
"reddit": (patternMatch) =>
|
||||||
patternMatch.sub?.length <= 22 && patternMatch.id?.length <= 10,
|
(patternMatch.sub?.length <= 22 && patternMatch.id?.length <= 10)
|
||||||
|
|| (patternMatch.user?.length <= 22 && patternMatch.id?.length <= 10),
|
||||||
|
|
||||||
"rutube": (patternMatch) =>
|
"rutube": (patternMatch) =>
|
||||||
patternMatch.id?.length === 32 || patternMatch.yappyId?.length === 32,
|
patternMatch.id?.length === 32 || patternMatch.yappyId?.length === 32,
|
||||||
|
|
Loading…
Reference in a new issue