Skip to content

Commit 3348573

Browse files
committed
feat: Être plus souple pour OpenGraph.
1 parent 65860db commit 3348573

File tree

2 files changed

+13
-65
lines changed

2 files changed

+13
-65
lines changed

src/core/scraper/opengraph.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,14 @@ const actionVideo = async function (_url, content, options) {
5252
}
5353

5454
const type = doc.querySelector('meta[property="og:video:type"]');
55-
if (null === type) {
56-
return undefined;
57-
}
58-
5955
const meta = SELECTORS.VIDEO.map((s) => doc.querySelector(s)).find(
6056
(m) => null !== m && "" !== m.content,
6157
);
6258
if (undefined === meta) {
6359
return undefined;
6460
}
6561

66-
if (type.content.startsWith("video/")) {
62+
if (null === type || type.content.startsWith("video/")) {
6763
return meta.content;
6864
}
6965
if ("text/html" === type.content && !options.depth) {
@@ -98,18 +94,14 @@ const actionAudio = async function (_url, content, options) {
9894
}
9995

10096
const type = doc.querySelector('meta[property="og:audio:type"]');
101-
if (null === type) {
102-
return undefined;
103-
}
104-
10597
const meta = SELECTORS.AUDIO.map((s) => doc.querySelector(s)).find(
10698
(m) => null !== m && "" !== m.content,
10799
);
108100
if (undefined === meta) {
109101
return undefined;
110102
}
111103

112-
if (type.content.startsWith("audio/")) {
104+
if (null === type || type.content.startsWith("audio/")) {
113105
return meta.content;
114106
}
115107
if ("text/html" === type.content && !options.depth) {

test/unit/core/scraper/opengraph.js

+11-55
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as scraper from "../../../../src/core/scraper/opengraph.js";
1010

1111
describe("core/scraper/opengraph.js", function () {
1212
describe("extractVideo()", function () {
13-
it("shouldn't handle when it's a unsupported URL", async function () {
13+
it("should return undefined when it isn't HTML", async function () {
1414
const url = new URL("https://foo.com");
1515
const content = { html: () => Promise.resolve(undefined) };
1616
const options = { depth: false };
@@ -19,26 +19,6 @@ describe("core/scraper/opengraph.js", function () {
1919
assert.equal(file, undefined);
2020
});
2121

22-
it("should return undefined when there isn't Open Graph type", async function () {
23-
const url = new URL("https://foo.com");
24-
const content = {
25-
html: () =>
26-
Promise.resolve(
27-
new DOMParser().parseFromString(
28-
`<html><head>
29-
<meta property="og:video"
30-
content="http://bar.com/" />
31-
</head></html>`,
32-
"text/html",
33-
),
34-
),
35-
};
36-
const options = { depth: false };
37-
38-
const file = await scraper.extractVideo(url, content, options);
39-
assert.equal(file, undefined);
40-
});
41-
4222
it("should return undefined when content is empty", async function () {
4323
const url = new URL("https://foo.com");
4424
const content = {
@@ -60,17 +40,15 @@ describe("core/scraper/opengraph.js", function () {
6040
assert.equal(file, undefined);
6141
});
6242

63-
it("should return undefined when type isn't supported", async function () {
43+
it("should return video URL when there isn't type", async function () {
6444
const url = new URL("https://foo.com");
6545
const content = {
6646
html: () =>
6747
Promise.resolve(
6848
new DOMParser().parseFromString(
6949
`<html><head>
70-
<meta property="og:video:type"
71-
content="application/pdf" />
7250
<meta property="og:video"
73-
content="http://bar.com/baz.pdf" />
51+
content="https://bar.com/baz.hls" />
7452
</head></html>`,
7553
"text/html",
7654
),
@@ -79,7 +57,7 @@ describe("core/scraper/opengraph.js", function () {
7957
const options = { depth: false };
8058

8159
const file = await scraper.extractVideo(url, content, options);
82-
assert.equal(file, undefined);
60+
assert.equal(file, "https://bar.com/baz.hls");
8361
});
8462

8563
it("should return video URL", async function () {
@@ -104,7 +82,7 @@ describe("core/scraper/opengraph.js", function () {
10482
assert.equal(file, "http://bar.com/baz.mkv");
10583
});
10684

107-
it("should return undefined when content is unknown", async function () {
85+
it("should return undefined when type isn't supported", async function () {
10886
const spy = sinon.stub(globalThis, "fetch");
10987

11088
const url = new URL("https://foo.com");
@@ -114,9 +92,9 @@ describe("core/scraper/opengraph.js", function () {
11492
new DOMParser().parseFromString(
11593
`<html><head>
11694
<meta property="og:video:type"
117-
content="bar/baz" />
95+
content="application/pdf" />
11896
<meta property="og:video"
119-
content="http://qux.com/" />
97+
content="http://bar.com/baz.pdf" />
12098
</head></html>`,
12199
"text/html",
122100
),
@@ -210,26 +188,6 @@ describe("core/scraper/opengraph.js", function () {
210188
assert.equal(file, undefined);
211189
});
212190

213-
it("should return undefined when there isn't Open Graph type", async function () {
214-
const url = new URL("https://foo.com");
215-
const content = {
216-
html: () =>
217-
Promise.resolve(
218-
new DOMParser().parseFromString(
219-
`<html><head>
220-
<meta property="og:audio"
221-
content="http://bar.com/" />
222-
</head></html>`,
223-
"text/html",
224-
),
225-
),
226-
};
227-
const options = { depth: false };
228-
229-
const file = await scraper.extractAudio(url, content, options);
230-
assert.equal(file, undefined);
231-
});
232-
233191
it("should return undefined when content is empty", async function () {
234192
const url = new URL("https://foo.com");
235193
const content = {
@@ -251,17 +209,15 @@ describe("core/scraper/opengraph.js", function () {
251209
assert.equal(file, undefined);
252210
});
253211

254-
it("should return undefined when type isn't supported", async function () {
212+
it("should return audio URL when there isn't type", async function () {
255213
const url = new URL("https://foo.com");
256214
const content = {
257215
html: () =>
258216
Promise.resolve(
259217
new DOMParser().parseFromString(
260218
`<html><head>
261-
<meta property="og:audio:type"
262-
content="application/pdf" />
263219
<meta property="og:audio"
264-
content="http://bar.com/baz.pdf" />
220+
content="https://bar.com/baz.mp3" />
265221
</head></html>`,
266222
"text/html",
267223
),
@@ -270,7 +226,7 @@ describe("core/scraper/opengraph.js", function () {
270226
const options = { depth: false };
271227

272228
const file = await scraper.extractAudio(url, content, options);
273-
assert.equal(file, undefined);
229+
assert.equal(file, "https://bar.com/baz.mp3");
274230
});
275231

276232
it("should return audio URL", async function () {
@@ -295,7 +251,7 @@ describe("core/scraper/opengraph.js", function () {
295251
assert.equal(file, "http://bar.com/baz.wav");
296252
});
297253

298-
it("should return undefined when content is unknown", async function () {
254+
it("should return undefined when type isn't supported", async function () {
299255
const spy = sinon.stub(globalThis, "fetch");
300256

301257
const url = new URL("https://foo.com");

0 commit comments

Comments
 (0)