From a5a01cc0c663fa9eaa7ed293c73b1e51ffa21b44 Mon Sep 17 00:00:00 2001 From: wukko Date: Fri, 3 May 2024 14:09:46 +0600 Subject: [PATCH] reddit: add support for user post links & clean up (#484) --- src/modules/processing/match.js | 3 +- src/modules/processing/services/reddit.js | 58 ++++++++++++++----- src/modules/processing/servicesConfig.json | 2 +- .../processing/servicesPatternTesters.js | 3 +- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index 63ffcc6c..cc9543c0 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -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": diff --git a/src/modules/processing/services/reddit.js b/src/modules/processing/services/reddit.js index 8964b24f..e022f62c 100644 --- a/src/modules/processing/services/reddit.js +++ b/src/modules/processing/services/reddit.js @@ -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` - }; + } } diff --git a/src/modules/processing/servicesConfig.json b/src/modules/processing/servicesConfig.json index 413854a2..0b32016c 100644 --- a/src/modules/processing/servicesConfig.json +++ b/src/modules/processing/servicesConfig.json @@ -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 }, diff --git a/src/modules/processing/servicesPatternTesters.js b/src/modules/processing/servicesPatternTesters.js index bdd691d5..25b8943a 100644 --- a/src/modules/processing/servicesPatternTesters.js +++ b/src/modules/processing/servicesPatternTesters.js @@ -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,