Skip to content

Commit

Permalink
fix: Corriger le scraper de FranceTV.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Jul 5, 2024
1 parent 3025a9c commit fb66633
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/core/scraper/francetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ const action = async function (_url, metadata) {
continue;
}

// Demander la vidéo pour Safari sur mobile, car l'API retourne une
// vidéo au format HLS qui fonctionne dans Kodi. Avec Chrome sur un
// ordinateur, la vidéo est au format DASH qui ne fonctionne pas dans
// Kodi.
// https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/extractor/francetv.py
let url =
`https://k7.ftven.fr/videos/${result.groups.videoId}` +
"?domain=www.france.tv&browser=chrome";
"?domain=www.france.tv&device_type=mobile&browser=safari";
let response = await fetch(url);
let json = await response.json();

Expand All @@ -44,7 +49,7 @@ const action = async function (_url, metadata) {
encodeURIComponent(json.video.url);
response = await fetch(url);
json = await response.json();
return json.url;
return `${json.url}|User-Agent=${encodeURIComponent(navigator.userAgent)}`;
}
return undefined;
};
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scraper/francetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Scraper: France tv", function () {
const file = await extract(url, context);
assert.ok(
undefined !== file &&
new URL(file).pathname.endsWith("/manifest.mpd"),
new URL(file).pathname.endsWith("/master.m3u8"),
`new URL("${file}").pathname.endsWith(...)`,
);
});
Expand Down
13 changes: 13 additions & 0 deletions test/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ import { browser } from "./polyfill/browser.js";
import { CloseEvent } from "./polyfill/closeevent.js";
import { DOMParser, XPathResult } from "./polyfill/dom.js";
import { stealthFetch } from "./polyfill/fetch.js";
import { USER_AGENT } from "./polyfill/useragent.js";

globalThis.browser = browser;
globalThis.CloseEvent = CloseEvent;
globalThis.DOMParser = DOMParser;
globalThis.fetch = stealthFetch;
// Ne pas modifier directement la variable navigator, car sa modification n'est
// pas possible : "Cannot set property userAgent of #<Navigator> which has only
// a getter".
Object.defineProperty(
Object.getPrototypeOf(globalThis.navigator),
"userAgent",
{
get: () => USER_AGENT,
enumerable: true,
configurable: true,
},
);
// Désactiver cette règle, car il y a un faux positif quand on affecte une
// valeur à la variable globale WebSocket.
// https://github.com/eslint-community/eslint-plugin-n/issues/274
Expand Down
13 changes: 3 additions & 10 deletions test/polyfill/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
* @author Sébastien Règne
*/

/**
* L'agent utilisateur de Chromium sous Ubuntu.
*
* @type {string}
*/
const USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" +
" Chrome/126.0.0.0 Safari/537.36";

/**
* La méthode <code>fetch()</code> native dans Node.js.
*
Expand All @@ -34,7 +25,9 @@ export const stealthFetch = function (input, init) {
// des navigateurs.
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Site": "same-origin",
"User-Agent": USER_AGENT,
// Remettre l'agent utilisateur, car la valeur dans le navigator a été
// écrasée.
"User-Agent": navigator.userAgent,
...init?.headers,
};
return nativeFetch(input, { ...init, headers });
Expand Down
14 changes: 14 additions & 0 deletions test/polyfill/useragent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @module
* @license MIT
* @author Sébastien Règne
*/

/**
* L'agent utilisateur de Chromium sous Ubuntu.
*
* @type {string}
*/
export const USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" +
" Chrome/126.0.0.0 Safari/537.36";
8 changes: 6 additions & 2 deletions test/unit/core/scraper/francetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ describe("core/scraper/francetv.js", function () {
};

const file = await scraper.extract(url, metadata);
assert.equal(file, "https://bar.fr/baz.mpd");
assert.equal(
file,
"https://bar.fr/baz.mpd|User-Agent=" +
encodeURIComponent(navigator.userAgent),
);

assert.equal(stub.callCount, 2);
assert.deepEqual(stub.firstCall.args, [
"https://k7.ftven.fr/videos/123-abc" +
"?domain=www.france.tv&browser=chrome",
"?domain=www.france.tv&device_type=mobile&browser=safari",
]);
assert.deepEqual(stub.secondCall.args, [
"https://hdfauth.ftven.fr/esi/TA" +
Expand Down

0 comments on commit fb66633

Please sign in to comment.