Skip to content

Commit 9d556d8

Browse files
authored
feat: Support old.reddit.com (#165)
1 parent aa54bcf commit 9d556d8

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/core/scraper/reddit.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,24 @@ export const extractEmbed = matchURLPattern(
6868
actionEmbed,
6969
"https://www.reddit.com/r/*",
7070
);
71+
72+
/**
73+
* Extrait les informations nécessaires pour lire une vidéo sur Kodi.
74+
*
75+
* @param {URLMatch} _url L'URL d'une vidéo Old Reddit.
76+
* @param {Object} metadata Les métadonnées de l'URL.
77+
* @param {Function} metadata.html La fonction retournant la promesse contenant
78+
* le document HTML.
79+
* @returns {Promise<string|undefined>} Une promesse contenant le lien du
80+
* _fichier_ ou `undefined`.
81+
*/
82+
83+
const actionOld = async (_url, metadata) => {
84+
const doc = await metadata.html();
85+
const player = doc.querySelector("div[data-hls-url]");
86+
return player?.dataset.hlsUrl;
87+
};
88+
export const extractOld = matchURLPattern(
89+
actionOld,
90+
"https://old.reddit.com/r/*",
91+
);

test/integration/scraper/reddit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,20 @@ describe("Scraper: Reddit [fr]", function () {
5858
"?video_id=s91Hs241YS4&incognito=false",
5959
);
6060
});
61+
62+
it("should return old video URL", async function () {
63+
const url = new URL(
64+
"https://old.reddit.com/r/Unexpected/comments/1l83vph" +
65+
"/fliping_each_other/",
66+
);
67+
const context = { depth: false, incognito: false };
68+
69+
const file = await extract(url, context);
70+
assert.ok(
71+
file?.startsWith(
72+
"https://v.redd.it/cs5l1nf8r46f1/HLSPlaylist.m3u8",
73+
),
74+
`"${file}"?.startsWith(...)`,
75+
);
76+
});
6177
});

test/unit/core/scraper/reddit.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,48 @@ describe("core/scraper/reddit.js", function () {
149149
assert.deepEqual(getAddons.mock.calls[0].arguments, ["video"]);
150150
});
151151
});
152+
153+
describe("extractOld()", function () {
154+
it("shouldn't handle when it's a unsupported URL", async function () {
155+
const url = new URL("https://www.redditinc.com/policies/");
156+
const file = await scraper.extractOld(url);
157+
assert.equal(file, undefined);
158+
});
159+
160+
it("should return undefined when it isn't a video on old.reddit", async function () {
161+
const url = new URL("https://old.reddit.com/r/foo");
162+
const metadata = {
163+
html: () =>
164+
Promise.resolve(
165+
new DOMParser().parseFromString(
166+
'<html lang="en"><body></body></html>',
167+
"text/html",
168+
),
169+
),
170+
};
171+
172+
const file = await scraper.extractOld(url, metadata);
173+
assert.equal(file, undefined);
174+
});
175+
176+
it("should return video URL from old.reddit.com", async function () {
177+
const url = new URL("https://old.reddit.com/r/foo");
178+
const metadata = {
179+
html: () =>
180+
Promise.resolve(
181+
new DOMParser().parseFromString(
182+
`<html lang="en"><body>
183+
<div
184+
data-hls-url="https://old.reddit.com/video.m3u8"
185+
></div>
186+
</body></html>`,
187+
"text/html",
188+
),
189+
),
190+
};
191+
192+
const file = await scraper.extractOld(url, metadata);
193+
assert.equal(file, "https://old.reddit.com/video.m3u8");
194+
});
195+
});
152196
});

0 commit comments

Comments
 (0)