reddit: add support for user post links & clean up (#484)

This commit is contained in:
wukko 2024-05-03 14:09:46 +06:00 committed by GitHub
parent 080fc043ea
commit a5a01cc0c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 17 deletions

View file

@ -80,7 +80,8 @@ export default async function(host, patternMatch, url, lang, obj) {
case "reddit":
r = await reddit({
sub: patternMatch.sub,
id: patternMatch.id
id: patternMatch.id,
user: patternMatch.user
});
break;
case "tiktok":

View file

@ -48,43 +48,73 @@ async function getAccessToken() {
}
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();
if (accessToken) url.hostname = 'oauth.reddit.com';
let data = await fetch(
url, { headers: accessToken && { authorization: `Bearer ${accessToken}` } }
).then((r) => { return r.json() }).catch(() => { return false });
if (!data) return { error: 'ErrorCouldntFetch' };
url, {
headers: accessToken && { authorization: `Bearer ${accessToken}` }
}
).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["secure_media"]["reddit_video"]["duration"] * 1000 > maxVideoDuration) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
if (data?.url?.endsWith('.gif')) return {
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,
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`;
video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0],
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
if (!audio) {
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];
if (!audio) return { typeId: 1, urls: video };
if (!audio) return {
typeId: 1,
urls: video
}
return {
typeId: 2,
type: "render",
urls: [video, audioFileLink],
audioFilename: `reddit_${id}_audio`,
filename: `reddit_${id}.mp4`
};
}
}

View file

@ -12,7 +12,7 @@
},
"reddit": {
"alias": "reddit videos & gifs",
"patterns": ["r/:sub/comments/:id/:title"],
"patterns": ["r/:sub/comments/:id/:title", "user/:user/comments/:id/:title"],
"subdomains": "*",
"enabled": true
},

View file

@ -16,7 +16,8 @@ export const testers = {
patternMatch.id?.length <= 128 || patternMatch.shortLink?.length <= 32,
"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) =>
patternMatch.id?.length === 32 || patternMatch.yappyId?.length === 32,