Skip to content

best way to save what you love

api/reddit: add support for short links

Felix Vuong 🍂b1bde25dee28
diff --git a/api/src/processing/service-config.js b/api/src/processing/service-config.js
index 00fa4ebf..87a71c38 100644
--- a/api/src/processing/service-config.js
+++ b/api/src/processing/service-config.js
@@ -90,7 +90,9 @@ export const services = {
"r/u_:user/comments/:id/:title",
"r/u_:user/comments/:id/comment/:commentId",
- "r/:sub/s/:shareId"
+ "r/:sub/s/:shareId",
+
+ "video/:shortId",
],
subdomains: "*",
},
diff --git a/api/src/processing/service-patterns.js b/api/src/processing/service-patterns.js
index 8735f123..2412fd46 100644
--- a/api/src/processing/service-patterns.js
+++ b/api/src/processing/service-patterns.js
@@ -23,7 +23,8 @@ export const testers = {
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),
+ || (pattern.sub?.length <= 22 && pattern.shareId?.length <= 16)
+ || (pattern.shortId?.length <= 16),
"rutube": pattern =>
(pattern.id?.length === 32 && pattern.key?.length <= 32) ||
diff --git a/api/src/processing/services/reddit.js b/api/src/processing/services/reddit.js
index 50c78d35..3bd8e88f 100644
--- a/api/src/processing/services/reddit.js
+++ b/api/src/processing/services/reddit.js
@@ -50,6 +50,24 @@ async function getAccessToken() {
export default async function(obj) {
let params = obj;
+ const accessToken = await getAccessToken();
+
+ if (params.shortId) {
+ let url = await fetch(`https://www.reddit.com/video/${params.shortId}`, {
+ headers: {
+ 'User-Agent': genericUserAgent,
+ 'Authorization': `Bearer ${accessToken}`
+ }
+ }).then(r => r.url).catch(() => {});
+
+ if (!url) return { error: "fetch.fail" };
+
+ try {
+ params = extract(normalizeURL(url)).patternMatch;
+ } catch (error) {
+ return { error: "fetch.fail" };
+ }
+ }
if (!params.id && params.shareId) {
params = await resolveRedirectingURL(
@@ -63,7 +81,6 @@ export default async function(obj) {
const url = new URL(`https://www.reddit.com/comments/${params.id}.json`);
- const accessToken = await getAccessToken();
if (accessToken) url.hostname = 'oauth.reddit.com';
let data = await fetch(
diff --git a/api/src/processing/url.js b/api/src/processing/url.js
index 82299999..a0f70fed 100644
--- a/api/src/processing/url.js
+++ b/api/src/processing/url.js
@@ -106,6 +106,14 @@ function aliasURL(url) {
url.pathname = `/share/${idPart.slice(-32)}`;
}
break;
+
+ case "redd":
+ /* reddit short video links can be treated by changing https://v.redd.it/<id>
+ to https://reddit.com/video/<id>.*/
+ if (url.hostname === "v.redd.it" && parts.length === 2) {
+ url = new URL(`https://www.reddit.com/video/${parts[1]}`);
+ }
+ break;
}
return url;
diff --git a/api/src/util/tests/reddit.json b/api/src/util/tests/reddit.json
index 3afc6126..1dd10ee5 100644
--- a/api/src/util/tests/reddit.json
+++ b/api/src/util/tests/reddit.json
@@ -56,5 +56,23 @@
"code": 200,
"status": "tunnel"
}
+ },
+ {
+ "name": "shortened video link",
+ "url": "https://v.redd.it/ifg2emt5ck0e1",
+ "params": {},
+ "expected": {
+ "code": 200,
+ "status": "tunnel"
+ }
+ },
+ {
+ "name": "shortened video link (alternative)",
+ "url": "https://reddit.com/video/ifg2emt5ck0e1",
+ "params": {},
+ "expected": {
+ "code": 200,
+ "status": "tunnel"
+ }
}
]
\ No newline at end of file