best way to save what you love
Svelte collaborationdownloaderinstagramjavascriptmusicredditsocial-mediasoundcloudsveltetiktoktwittertypescriptvideovimeovkwebappyoutubeyoutube-downloader
reddit: add support for user post links & clean up (#484)
wukkoa5a01cc0c663
diff --git a/src/modules/processing/match.js b/src/modules/processing/match.jsindex 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.jsindex 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(() => {});++ if (!data || !Array.isArray(data)) return { error: 'ErrorCouldntFetch' };- data = data[0]["data"]["children"][0]["data"];+ data = data[0]?.data?.children[0]?.data;++ if (data?.url?.endsWith('.gif')) return {+ typeId: 1,+ urls: data.url+ }- if (data.url.endsWith('.gif')) return { typeId: 1, urls: data.url };+ if (!data.secure_media?.reddit_video)+ return { error: 'ErrorEmptyDownload' };- 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.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 qualityif (!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.jsonindex 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.jsindex 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,