Skip to content

Commit

Permalink
api/reddit: add support for mobile links & bunch of other links (#1098)
Browse files Browse the repository at this point in the history
* api/reddit: extract params from a mobile share link

* api/reddit: add support for a bunch of links & update the api endpoint

also fixed "undefined" in a filename when downloading a user post

* api/service-patterns: fix reddit id pattern
  • Loading branch information
wukko authored Feb 9, 2025
1 parent 5306760 commit a0f227d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
5 changes: 2 additions & 3 deletions api/src/processing/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ export default async function({ host, patternMatch, params }) {

case "reddit":
r = await reddit({
sub: patternMatch.sub,
id: patternMatch.id,
user: patternMatch.user
...patternMatch,
dispatcher,
});
break;

Expand Down
15 changes: 14 additions & 1 deletion api/src/processing/service-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,21 @@ export const services = {
},
reddit: {
patterns: [
"comments/:id",

"r/:sub/comments/:id",
"r/:sub/comments/:id/:title",
"user/:user/comments/:id/:title"
"r/:sub/comments/:id/comment/:commentId",

"user/:user/comments/:id",
"user/:user/comments/:id/:title",
"user/:user/comments/:id/comment/:commentId",

"r/u_:user/comments/:id",
"r/u_:user/comments/:id/:title",
"r/u_:user/comments/:id/comment/:commentId",

"r/:sub/s/:shareId"
],
subdomains: "*",
},
Expand Down
6 changes: 4 additions & 2 deletions api/src/processing/service-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export const testers = {
pattern.id?.length <= 128 || pattern.shortLink?.length <= 32,

"reddit": pattern =>
(pattern.sub?.length <= 22 && pattern.id?.length <= 10)
|| (pattern.user?.length <= 22 && pattern.id?.length <= 10),
pattern.id?.length <= 16 && !pattern.sub && !pattern.user
|| (pattern.sub?.length <= 22 && pattern.id?.length <= 16)
|| (pattern.user?.length <= 22 && pattern.id?.length <= 16)
|| (pattern.sub?.length <= 22 && pattern.shareId?.length <= 16),

"rutube": pattern =>
(pattern.id?.length === 32 && pattern.key?.length <= 32) ||
Expand Down
33 changes: 24 additions & 9 deletions api/src/processing/services/reddit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolveRedirectingURL } from "../url.js";
import { genericUserAgent, env } from "../../config.js";
import { getCookie, updateCookieValues } from "../cookie/manager.js";

Expand Down Expand Up @@ -48,12 +49,20 @@ async function getAccessToken() {
}

export default async function(obj) {
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`;
let params = obj;

if (!params.id && params.shareId) {
params = await resolveRedirectingURL(
`https://www.reddit.com/r/${params.sub}/s/${params.shareId}`,
obj.dispatcher,
genericUserAgent
);
}

if (!params?.id) return { error: "fetch.short_link" };

const url = new URL(`https://www.reddit.com/comments/${params.id}.json`);

const accessToken = await getAccessToken();
if (accessToken) url.hostname = 'oauth.reddit.com';

Expand All @@ -73,12 +82,17 @@ export default async function(obj) {

data = data[0]?.data?.children[0]?.data;

const id = `${String(obj.sub).toLowerCase()}_${obj.id}`;
let sourceId;
if (params.sub || params.user) {
sourceId = `${String(params.sub || params.user).toLowerCase()}_${params.id}`;
} else {
sourceId = params.id;
}

if (data?.url?.endsWith('.gif')) return {
typeId: "redirect",
urls: data.url,
filename: `reddit_${id}.gif`,
filename: `reddit_${sourceId}.gif`,
}

if (!data.secure_media?.reddit_video)
Expand All @@ -87,8 +101,9 @@ export default async function(obj) {
if (data.secure_media?.reddit_video?.duration > env.durationLimit)
return { error: "content.too_long" };

const video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0];

let audio = false,
video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0],
audioFileLink = `${data.secure_media?.reddit_video?.fallback_url?.split('DASH')[0]}audio`;

if (video.match('.mp4')) {
Expand Down Expand Up @@ -121,7 +136,7 @@ export default async function(obj) {
typeId: "tunnel",
type: "merge",
urls: [video, audioFileLink],
audioFilename: `reddit_${id}_audio`,
filename: `reddit_${id}.mp4`
audioFilename: `reddit_${sourceId}_audio`,
filename: `reddit_${sourceId}.mp4`
}
}

0 comments on commit a0f227d

Please sign in to comment.