Skip to content

Commit

Permalink
fix: Actualiser le scraper de Melty.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Dec 9, 2023
1 parent 2a4627c commit 075d9f4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
34 changes: 25 additions & 9 deletions src/core/scraper/melty.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
import { extract as metaExtract } from "../scrapers.js";
import { matchPattern } from "../tools/matchpattern.js";

/**
* L'expression rationnelle pour extraire les données de la vidéo.
*
* @type {RegExp}
*/
const DATA_REGEXP =
/init_nouveau_player_dailymotion\("[^"]+","(?<id>[^"]+)", params\);/u;

/**
* Extrait les informations nécessaires pour lire une vidéo sur Kodi.
*
* @param {URL} url L'URL d'un article de Melty.
* @param {URL} _url L'URL d'un article de Melty.
* @param {Object} metadata Les métadonnées de l'URL.
* @param {Function} metadata.html La fonction retournant la promesse
* contenant le document HTML.
Expand All @@ -25,19 +33,27 @@ import { matchPattern } from "../tools/matchpattern.js";
* <em>fichier</em> ou
* <code>undefined</code>.
*/
const action = async function (url, metadata, context) {
const action = async function (_url, metadata, context) {
if (context.depth) {
return undefined;
}

const doc = await metadata.html();
for (const meta of doc.querySelectorAll('meta[itemprop="contentUrl"]')) {
const file = await metaExtract(new URL(meta.content, url), {
...context,
depth: true,
});
if (undefined !== file) {
return file;
for (const script of doc.querySelectorAll("script:not([src])")) {
const result = DATA_REGEXP.exec(script.text);
if (null !== result) {
const file = await metaExtract(
new URL(
`https://www.dailymotion.com/video/${result.groups.id}`,
),
{
...context,
depth: true,
},
);
if (undefined !== file) {
return file;
}
}
}
return undefined;
Expand Down
38 changes: 22 additions & 16 deletions test/unit/core/scraper/melty.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ describe("core/scraper/melty.js", function () {
html: () =>
Promise.resolve(
new DOMParser().parseFromString(
`<html><head>
<meta itemprop="contentUrl"
content="http://www.dailymotion.com` +
`/embed/video/bar" />
</head></html>`,
`<html><body>
<script>
jQuery(document).ready(function (){
init_nouveau_player_dailymotion(` +
`"player_bar","baz", params);
</script>
</body></html>`,
"text/html",
),
),
Expand All @@ -45,9 +47,9 @@ describe("core/scraper/melty.js", function () {
html: () =>
Promise.resolve(
new DOMParser().parseFromString(
`<html><head>
<meta name="description" content="bar" />
</head></html>`,
`<html><body>
<script src="bar.js"></script>
</body></html>`,
"text/html",
),
),
Expand All @@ -66,13 +68,17 @@ describe("core/scraper/melty.js", function () {
html: () =>
Promise.resolve(
new DOMParser().parseFromString(
`<html><head>
<meta itemprop="contentUrl"
content="http://foo.com/bar.html" />
<meta itemprop="contentUrl"
content="http://www.dailymotion.com` +
`/embed/video/bar" />
</head></html>`,
`<html><body>
<script>
jQuery(document).ready(function (){
console.log("bar");
</script>
<script>
jQuery(document).ready(function (){
init_nouveau_player_dailymotion(` +
`"player_baz","qux", params);
</script>
</body></html>`,
"text/html",
),
),
Expand All @@ -82,7 +88,7 @@ describe("core/scraper/melty.js", function () {
const file = await scraper.extract(url, metadata, context);
assert.equal(
file,
"plugin://plugin.video.dailymotion_com/?mode=playVideo&url=bar",
"plugin://plugin.video.dailymotion_com/?mode=playVideo&url=qux",
);

assert.equal(stub.callCount, 1);
Expand Down

0 comments on commit 075d9f4

Please sign in to comment.