From 035f5a7839488d89fea33339c6af5bdb475cfd8e Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 14 Oct 2023 22:48:36 +0000 Subject: [PATCH 1/3] cookie: allow updating cookie values directly --- src/modules/processing/cookie/manager.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/modules/processing/cookie/manager.js b/src/modules/processing/cookie/manager.js index 6ea05561..7189eb18 100644 --- a/src/modules/processing/cookie/manager.js +++ b/src/modules/processing/cookie/manager.js @@ -56,7 +56,10 @@ export function updateCookie(cookie, headers) { cookie.unset(parsed.filter(c => c.expires < new Date()).map(c => c.name)); parsed.filter(c => !c.expires || c.expires > new Date()).forEach(c => values[c.name] = c.value); + updateCookieValues(cookie, values); +} +export function updateCookieValues(cookie, values) { cookie.set(values); if (Object.keys(values).length) dirty = true } From a45d500c3b9f98f58e1bd848a876e92e802f6ad2 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 14 Oct 2023 22:43:59 +0000 Subject: [PATCH 2/3] reddit: add support for authenticated requests --- src/modules/processing/services/reddit.js | 58 ++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/modules/processing/services/reddit.js b/src/modules/processing/services/reddit.js index 8fae3f02..bf5ef911 100644 --- a/src/modules/processing/services/reddit.js +++ b/src/modules/processing/services/reddit.js @@ -1,7 +1,61 @@ -import { maxVideoDuration } from "../../config.js"; +import { genericUserAgent, maxVideoDuration } from "../../config.js"; +import { getCookie, updateCookieValues } from "../cookie/manager.js"; + +async function getAccessToken() { + /* "cookie" in cookiefile needs to contain: + * client_id, client_secret, refresh_token + * e.g. client_id=bla; client_secret=bla; refresh_token=bla + * + * you can get these by making a reddit app and + * authenticating an account against reddit's oauth2 api + * see: https://github.com/reddit-archive/reddit/wiki/OAuth2 + * + * any additional cookie fields are managed by this code and you + * should not touch them unless you know what you're doing. **/ + const cookie = await getCookie('reddit'); + if (!cookie) return; + + const values = cookie.values(), + needRefresh = !values.access_token + || !values.expiry + || Number(values.expiry) > new Date().getTime(); + if (!needRefresh) return values.access_token; + + const data = await fetch('https://www.reddit.com/api/v1/access_token', { + method: 'POST', + headers: { + 'authorization': `Basic ${Buffer.from( + [values.client_id, values.client_secret].join(':') + ).toString('base64')}`, + 'content-type': 'application/x-www-form-urlencoded', + 'user-agent': genericUserAgent, + 'accept': 'application/json' + }, + body: `grant_type=refresh_token&refresh_token=${encodeURIComponent(values.refresh_token)}` + }).then(r => r.json()).catch(_ => {}); + if (!data) return; + + const { access_token, refresh_token, expires_in } = data; + if (!access_token) return; + + updateCookieValues(cookie, { + ...cookie.values(), + access_token, refresh_token, + expiry: new Date().getTime() + (expires_in * 1000), + }); + + return access_token; +} export default async function(obj) { - let data = await fetch(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}/${obj.name}.json`).then((r) => { return r.json() }).catch(() => { return false }); + const url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}/${obj.title}.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' }; data = data[0]["data"]["children"][0]["data"]; From 33daaaf9365c85bed9cf77c9d21a44dc31d8b6b7 Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 15 Oct 2023 11:03:22 +0600 Subject: [PATCH 3/3] Update reddit.js --- src/modules/processing/services/reddit.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/processing/services/reddit.js b/src/modules/processing/services/reddit.js index bf5ef911..c985831a 100644 --- a/src/modules/processing/services/reddit.js +++ b/src/modules/processing/services/reddit.js @@ -50,7 +50,7 @@ async function getAccessToken() { export default async function(obj) { const url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}/${obj.title}.json`); - const accessToken = await getAccessToken() + const accessToken = await getAccessToken(); if (accessToken) url.hostname = 'oauth.reddit.com'; let data = await fetch( @@ -71,7 +71,7 @@ export default async function(obj) { await fetch(audioFileLink, { method: "HEAD" }).then((r) => { if (Number(r.status) === 200) audio = true }).catch(() => { audio = false }); - // fallback for videos with differentiating audio quality + // 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 });