Skip to content

Commit

Permalink
feat: Gérer les shorts de YouTube.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Sep 27, 2022
1 parent 3d3a4f9 commit 5920f4a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/core/scraper/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ const actionMinify = async function ({ pathname }, _content, { incognito }) {
return plugin.generateVideoUrl(pathname.slice(1), incognito);
};
export const extractMinify = matchPattern(actionMinify, "*://youtu.be/*");

/**
* Extrait les informations nécessaire pour lire une vidéo sur Kodi.
*
* @param {URL} url L'URL d'un <em>short</em> YouTube.
* @param {Object} _content Le contenu de l'URL.
* @param {Object} options Les options de l'extraction.
* @param {boolean} options.incognito La marque indiquant si l'utilisateur est
* en navigation privée.
* @returns {Promise<string>} Une promesse contenant le lien du
* <em>fichier</em>.
*/
const actionShort = async function ({ pathname }, _content, { incognito }) {
return plugin.generateVideoUrl(pathname.slice(8), incognito);
};
export const extractShort = matchPattern(actionShort,
"*://*.youtube.com/shorts/*",
"*://youtube.com/shorts/*");
31 changes: 31 additions & 0 deletions test/integration/scraper/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,35 @@ describe("Scraper: YouTube", function () {
assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});

it("should return video id from short", async function () {
const stub = sinon.stub(kodi.addons, "getAddons").resolves([]);

const url = new URL("https://www.youtube.com/shorts/Oq98KDthqyk");
const options = { depth: false, incognito: false };

const file = await extract(url, options);
assert.strictEqual(file,
"plugin://plugin.video.youtube/play/" +
"?video_id=Oq98KDthqyk&incognito=false");

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});

it("should return video id from short shared", async function () {
const stub = sinon.stub(kodi.addons, "getAddons").resolves([]);

const url = new URL("https://youtube.com/shorts/rP34nI3E3bc" +
"?feature=share");
const options = { depth: false, incognito: false };

const file = await extract(url, options);
assert.strictEqual(file,
"plugin://plugin.video.youtube/play/" +
"?video_id=rP34nI3E3bc&incognito=false");

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});
});
36 changes: 35 additions & 1 deletion test/unit/core/scraper/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("core/scraper/youtube.js", function () {
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});

it("should return video id form invidio.us", async function () {
it("should return video id from invidio.us", async function () {
const stub = sinon.stub(kodi.addons, "getAddons").resolves([]);

const url = new URL("https://invidio.us/embed/foo");
Expand Down Expand Up @@ -336,4 +336,38 @@ describe("core/scraper/youtube.js", function () {
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});
});

describe("extractShort()", function () {
it("should return video id", async function () {
const stub = sinon.stub(kodi.addons, "getAddons").resolves([]);

const url = new URL("https://www.youtube.com/shorts/foo");
const content = undefined;
const options = { incognito: false };

const file = await scraper.extractShort(url, content, options);
assert.strictEqual(file,
"plugin://plugin.video.youtube/play/" +
"?video_id=foo&incognito=false");

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});

it("should return video id from URL without 'www'", async function () {
const stub = sinon.stub(kodi.addons, "getAddons").resolves([]);

const url = new URL("https://youtube.com/shorts/foo");
const content = undefined;
const options = { incognito: false };

const file = await scraper.extractShort(url, content, options);
assert.strictEqual(file,
"plugin://plugin.video.youtube/play/" +
"?video_id=foo&incognito=false");

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["video"]);
});
});
});

0 comments on commit 5920f4a

Please sign in to comment.