Skip to content

Commit

Permalink
fix: Actualiser le scraper de 1tv.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Feb 24, 2020
1 parent 7f95ddf commit 1a1b46b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
19 changes: 15 additions & 4 deletions src/core/scraper/onetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@ const API_URL = "https://www.1tv.ru/playlist?single=true&video_id=";
/**
* Extrait les informations nécessaire pour lire une vidéo sur Kodi.
*
* @param {URL} url L'URL d'une page embarquée de Первый канал (1tv.ru).
* @param {URL} url L'URL d'une page de Первый канал (1tv.ru).
* @param {HTMLDocument} doc Le contenu HTML de la page.
* @returns {Promise.<?string>} Une promesse contenant le lien du
* <em>fichier</em> ou <code>null</code>.
*/
const action = async function ({ pathname }) {
const id = pathname.slice(7, pathname.indexOf(":"));
const action = async function ({ pathname }, doc) {
let id;
if (pathname.startsWith("/embed/")) {
id = pathname.slice(7, pathname.indexOf(":"));
} else {
const meta = doc.querySelector(`meta[property="ya:ovs:content_id"]`);
if (null === meta) {
return null;
}
id = meta.content.split(":")[0];
}

const response = await fetch(API_URL + id);
const json = await response.json();
return "https:" + json[0].mbr[0].src;
};
export const extract = matchPattern(action, "*://www.1tv.ru/embed/*");
export const extract = matchPattern(action, "*://www.1tv.ru/*");
18 changes: 18 additions & 0 deletions test/integration/scraper/onetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ describe("Scraper: Первый канал (1tv.ru)", function () {
assert.strictEqual(file, expected);
});

it("should return show URL from embed", async function () {
const url = "https://www.1tv.ru/embed/160522:12";
const options = { "depth": 0, "incognito": false };
const expected = {
"start": "https://balancer-vod.1tv.ru/video/multibitrate/video/",
"middle": "_Golos-",
"end": ".mp4"
};

const file = await extract(new URL(url), options);
assert.ok(file.startsWith(expected.start),
`"${file}".startsWith(expected.start) from ${url}`);
assert.ok(file.includes(expected.middle),
`"${file}".includes(expected.middle) from ${url}`);
assert.ok(file.endsWith(expected.end),
`"${file}".endsWith(expected.end) from ${url}`);
});

it("should return URL when it's not a movie", async function () {
const url = "https://www.1tv.ru/movies/vse-filmy";
const options = { "depth": 0, "incognito": false };
Expand Down
38 changes: 34 additions & 4 deletions test/unit/core/scraper/onetv.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,54 @@ describe("core/scraper/onetv.js", function () {
describe("extract()", function () {
it("should return null when there isn't Open Graph", async function () {
const url = "https://www.1tv.ru/foo.html";
const doc = new DOMParser().parseFromString(`
<html>
<head></head>
</html>`, "text/html");
const expected = null;

const file = await extract(new URL(url));
const file = await extract(new URL(url), doc);
assert.strictEqual(file, expected);
});

it("should return video URL", async function () {
sinon.stub(globalThis, "fetch")
.callsFake(() => Promise.resolve({
"json": () => [{
"mbr": [{ "src": "//baz.com/quz.avi" }]
"mbr": [{ "src": "//qux.com/quux.avi" }]
}]
}));

const url = "https://www.1tv.ru/foo.html";
const doc = new DOMParser().parseFromString(`
<html>
<head>
<meta property="ya:ovs:content_id" content="bar:baz" />
</head>
</html>`, "text/html");
const expected = "https://qux.com/quux.avi";

const file = await extract(new URL(url), doc);
assert.strictEqual(file, expected);
const call = globalThis.fetch.firstCall;
assert.strictEqual(call.args[0],
"https://www.1tv.ru/playlist?single=true" +
"&video_id=bar");
});

it("should return video URL from embed", async function () {
sinon.stub(globalThis, "fetch")
.callsFake(() => Promise.resolve({
"json": () => [{
"mbr": [{ "src": "//baz.com/qux.avi" }]
}]
}));

const url = "https://www.1tv.ru/embed/foo:bar";
const expected = "https://baz.com/quz.avi";
const doc = null;
const expected = "https://baz.com/qux.avi";

const file = await extract(new URL(url));
const file = await extract(new URL(url), doc);
assert.strictEqual(file, expected);
const call = globalThis.fetch.firstCall;
assert.strictEqual(call.args[0],
Expand Down

0 comments on commit 1a1b46b

Please sign in to comment.