Skip to content

Commit c6fa8d3

Browse files
committed
fix: Corriger le scraper pour Flickr.
1 parent 5b9b710 commit c6fa8d3

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/core/scraper/flickr.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ const API_URL =
3838
*/
3939
const action = async function (_url, metadata) {
4040
const doc = await metadata.html();
41-
const video = doc.querySelector("video");
42-
if (null === video) {
43-
return undefined;
44-
}
4541

46-
const parts = new URL(video.poster).pathname.split(/[./_]/u);
42+
const meta = doc.querySelector('meta[property="og:image"]');
43+
const parts = new URL(meta.content).pathname.split(/[./_]/u);
4744
const photoId = parts[2];
4845
const secret = parts[3];
49-
const key = KEY_REGEXP.exec(doc.documentElement.innerHTML).groups.key;
46+
47+
const apiKey = KEY_REGEXP.exec(doc.documentElement.innerHTML).groups.key;
48+
5049
const response = await fetch(
51-
`${API_URL}&photo_id=${photoId}&secret=${secret}&api_key=${key}`,
50+
`${API_URL}&photo_id=${photoId}&secret=${secret}&api_key=${apiKey}`,
5251
);
5352
const json = await response.json();
54-
return json.streams.stream[0]["_content"];
53+
return json.streams?.stream[0]["_content"];
5554
};
5655
export const extract = matchPattern(action, "*://www.flickr.com/photos/*");

test/unit/core/scraper/flickr.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,46 @@ describe("core/scraper/flickr.js", function () {
1818
});
1919

2020
it("should return undefined when it isn't a video", async function () {
21+
const stub = sinon
22+
.stub(globalThis, "fetch")
23+
.resolves(Response.json({}));
24+
2125
const url = new URL("https://www.flickr.com/photos/foo");
2226
const metadata = {
2327
html: () =>
2428
Promise.resolve(
2529
new DOMParser().parseFromString(
26-
"<html><body></body></html>",
30+
`<html><head>
31+
<meta property="og:image"
32+
content="https://live.staticflickr.com` +
33+
`/bar/baz_qux_b.jpg">
34+
</head><body>
35+
<script>
36+
root.YUI_config.flickr.api.site_key = "quux";
37+
</script>
38+
</body></html>`,
2739
"text/html",
2840
),
2941
),
3042
};
3143

3244
const file = await scraper.extract(url, metadata);
3345
assert.equal(file, undefined);
46+
47+
assert.equal(stub.callCount, 1);
48+
assert.deepEqual(stub.firstCall.args, [
49+
"https://api.flickr.com/services/rest" +
50+
"?method=flickr.video.getStreamInfo&format=json" +
51+
"&nojsoncallback=1&photo_id=baz&secret=qux" +
52+
"&api_key=quux",
53+
]);
3454
});
3555

3656
it("should return video URL", async function () {
3757
const stub = sinon.stub(globalThis, "fetch").resolves(
3858
Response.json({
3959
streams: {
40-
stream: [{ _content: "https://foo.net/bar.mp4" }],
60+
stream: [{ _content: "https://foo.com/bar.mp4" }],
4161
},
4262
}),
4363
);
@@ -47,27 +67,29 @@ describe("core/scraper/flickr.js", function () {
4767
html: () =>
4868
Promise.resolve(
4969
new DOMParser().parseFromString(
50-
`<html><body>
70+
`<html><head>
71+
<meta property="og:image"
72+
content="https://live.staticflickr.com` +
73+
`/qux/quux_corge_b.jpg">
74+
</head><body>
5175
<script>
52-
root.YUI_config.flickr.api.site_key = "qux";
76+
root.YUI_config.flickr.api.site_key = "grault";
5377
</script>
54-
<video poster="//quux.com/corge` +
55-
`/grault_garply.jpg" />
5678
</body></html>`,
5779
"text/html",
5880
),
5981
),
6082
};
6183

6284
const file = await scraper.extract(url, metadata);
63-
assert.equal(file, "https://foo.net/bar.mp4");
85+
assert.equal(file, "https://foo.com/bar.mp4");
6486

6587
assert.equal(stub.callCount, 1);
6688
assert.deepEqual(stub.firstCall.args, [
6789
"https://api.flickr.com/services/rest" +
6890
"?method=flickr.video.getStreamInfo&format=json" +
69-
"&nojsoncallback=1&photo_id=grault&secret=garply" +
70-
"&api_key=qux",
91+
"&nojsoncallback=1&photo_id=quux&secret=corge" +
92+
"&api_key=grault",
7193
]);
7294
});
7395
});

0 commit comments

Comments
 (0)