Copy from youtube-listener project + some refactor to fit with peertube. (Some testing issues)

This commit is contained in:
Amaury Joly 2021-03-28 13:24:29 +02:00
parent 32b5b3505c
commit 0b24b39d0a
14 changed files with 18121 additions and 0 deletions

15
.eslintrc.json Normal file
View File

@ -0,0 +1,15 @@
{
"env": {
"node": true,
"commonjs": true,
"es2021": true
},
"extends": ["plugin:prettier/recommended", "plugin:mocha/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12
},
"plugins": ["@typescript-eslint", "mocha"],
"rules": {
}
}

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
build

1
.prettierrc.json Normal file
View File

@ -0,0 +1 @@
{}

1
index.ts Normal file
View File

@ -0,0 +1 @@
export { PeertubeListenerRss } from "./src/PeertubeListenerRss";

13284
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

35
package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "peertube-listener-rss",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "cross-env TS_NODE_PROJECT='./tests/tsconfig.json' mocha --require ts-node/register ./tests/**/*-spec.ts",
"build": "tsc -p ./src"
},
"devDependencies": {
"@types/chai": "4.2.14",
"@types/mocha": "8.2.0",
"@types/node": "14.14.25",
"@typescript-eslint/eslint-plugin": "4.14.2",
"@typescript-eslint/parser": "4.14.2",
"chai": "4.3.0",
"cross-env": "7.0.3",
"eslint": "7.19.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-mocha": "8.0.0",
"eslint-plugin-prettier": "3.3.1",
"mocha": "8.2.1",
"nock": "13.0.11",
"prettier": "2.2.1",
"sinon-chai": "3.5.0",
"ts-node": "9.1.1",
"ts-sinon": "2.0.1",
"tsc-watch": "4.2.9",
"typescript": "4.1.3"
},
"dependencies": {
"listener-rss": "file:../listener-rss"
}
}

View File

@ -0,0 +1,19 @@
import { ListenerRss } from "listener-rss";
export class PeertubeListenerRss extends ListenerRss {
constructor(
adrInstance: string,
idChaine: string,
timeloop?: number,
lastEntriesLinks?: string[]
) {
super({
address: `https://${adrInstance}/feeds/videos.xml?videoChannelId=${idChaine}`,
timeloop: timeloop,
lastEntriesLinks: lastEntriesLinks,
customfields: {
"media:group": ["media:group"],
},
});
}
}

10
src/tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"moduleResolution": "node"
},
"exclude": [
"build/",
"node_modules"
]
}

View File

@ -0,0 +1,197 @@
[
{
"creator": "Dimitri",
"title": "⚡ VA, VAR, Watts... C'est quoi la puissance réactive ?? - Monsieur Bidouille",
"link": "https://video.monsieurbidouille.fr/videos/watch/5ffe5a3a-8377-472d-9c04-d4ed130d6775",
"pubDate": "Fri, 26 Feb 2021 17:23:49 GMT",
"content:encoded": "Allez sur https://NordVPN.com/bidouille et utilisez le code BIDOUILLE pour profiter de la promotion pour un engagement de 2ans avec 1mois offert, et possibilité dêtre remboursé dans les 30 premiers jours. \n\nVA, VAR, W cest quoi ces unités ? Aujourdhui nous allons voir quelque chose dessentiel pour les réseaux électriques, et qui permet de comprendre beaucoup de choses : la puissance réactive. \n\nMerci à Barbatronic pour les retours. \n\nSoutenir la chaîne sur Tipeee : https://fr.tipeee.com/monsieur-bidouille\nSoutenir la chaîne sur Utip : https://utip.io/monsieurbidouille\n\nMa chaîne : https://www.youtube.com/user/monsieurbidouille\nMon instance Peertube : https://video.monsieurbidouille.fr\nFacebook : https://www.facebook.com/monsieurbidouille\nTwitter : https://twitter.com/MrBidouille\nMastodon : https://framapiaf.org/web/accounts/23449\nSite web : http://monsieurbidouille.fr \nDiscord : https://discord.gg/93BVEz6\n\nSources : \nVidéos pour aller plus loin : https://www.youtube.com/watch?v=izjWx4h7-DY\nSur le facteur de puissance : https://www.etaplighting.com/fr/news/facteur-de-puissance-et-les-harmoniques-une-explication-simple\n\n0:00 sponso\n0:33 intro\n0:52 de quoi on parle\n1:17 cest qui un courant électrique ?\n2:15 courant sinusoïdal\n3:18 la puissance en courant alternatif sinus\n5:15 BIÈRE\n7:10 pourquoi le déphasage ? \n9:16 mythes et légendes\n10:12 peut on exploiter cette énergie ?\n11:38 facteur de puissance\n\n#énergie #cosphi #réseaux",
"content:encodedSnippet": "Allez sur https://NordVPN.com/bidouille et utilisez le code BIDOUILLE pour profiter de la promotion pour un engagement de 2ans avec 1mois offert, et possibilité dêtre remboursé dans les 30 premiers jours. \n\nVA, VAR, W cest quoi ces unités ? Aujourdhui nous allons voir quelque chose dessentiel pour les réseaux électriques, et qui permet de comprendre beaucoup de choses : la puissance réactive. \n\nMerci à Barbatronic pour les retours. \n\nSoutenir la chaîne sur Tipeee : https://fr.tipeee.com/monsieur-bidouille\nSoutenir la chaîne sur Utip : https://utip.io/monsieurbidouille\n\nMa chaîne : https://www.youtube.com/user/monsieurbidouille\nMon instance Peertube : https://video.monsieurbidouille.fr\nFacebook : https://www.facebook.com/monsieurbidouille\nTwitter : https://twitter.com/MrBidouille\nMastodon : https://framapiaf.org/web/accounts/23449\nSite web : http://monsieurbidouille.fr \nDiscord : https://discord.gg/93BVEz6\n\nSources : \nVidéos pour aller plus loin : https://www.youtube.com/watch?v=izjWx4h7-DY\nSur le facteur de puissance : https://www.etaplighting.com/fr/news/facteur-de-puissance-et-les-harmoniques-une-explication-simple\n\n0:00 sponso\n0:33 intro\n0:52 de quoi on parle\n1:17 cest qui un courant électrique ?\n2:15 courant sinusoïdal\n3:18 la puissance en courant alternatif sinus\n5:15 BIÈRE\n7:10 pourquoi le déphasage ? \n9:16 mythes et légendes\n10:12 peut on exploiter cette énergie ?\n11:38 facteur de puissance\n\n#énergie #cosphi #réseaux",
"enclosure": {
"type": "application/x-bittorrent",
"url": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-1080.torrent",
"length": "163469785"
},
"dc:creator": "Dimitri",
"media:group": {
"media:peerLink": [
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-1080.torrent",
"isDefault": "true"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-1080.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-720.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-720.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-480.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-480.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-240.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-240.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-0.torrent"
}
},
{
"$": {
"type": "application/x-bittorrent",
"href": "https://video.monsieurbidouille.fr/static/torrents/5ffe5a3a-8377-472d-9c04-d4ed130d6775-0.torrent"
}
}
],
"media:content": [
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-1080.mp4",
"fileSize": "163469785",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "1080"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-1080.mp4",
"fileSize": "173724412",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "1080"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-720.mp4",
"fileSize": "137588850",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "720"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-720.mp4",
"fileSize": "137384708",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "720"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-480.mp4",
"fileSize": "80804444",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "480"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-480.mp4",
"fileSize": "80599622",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "480"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-240.mp4",
"fileSize": "35336631",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "240"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-240.mp4",
"fileSize": "35134765",
"type": "video/mp4",
"medium": "video",
"framerate": "25",
"duration": "823",
"height": "240"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-0.mp4",
"fileSize": "13307586",
"type": "video/mp4",
"medium": "video",
"framerate": "0",
"duration": "823",
"height": "0"
}
},
{
"$": {
"url": "https://video.monsieurbidouille.fr/static/webseed/5ffe5a3a-8377-472d-9c04-d4ed130d6775-0.mp4",
"fileSize": "13340348",
"type": "video/mp4",
"medium": "video",
"framerate": "0",
"duration": "823",
"height": "0"
}
}
]
},
"content": "Allez sur https://NordVPN.com/bidouille et utilisez le code BIDOUILLE pour profiter de la promotion pour un engagement de 2ans avec 1mois offert, et possibilité dêtre remboursé dans les 30 premiers jours. \n\nVA, VAR, W cest quoi ces unités ? Aujo...",
"contentSnippet": "Allez sur https://NordVPN.com/bidouille et utilisez le code BIDOUILLE pour profiter de la promotion pour un engagement de 2ans avec 1mois offert, et possibilité dêtre remboursé dans les 30 premiers jours. \n\nVA, VAR, W cest quoi ces unités ? Aujo...",
"guid": "https://video.monsieurbidouille.fr/videos/watch/5ffe5a3a-8377-472d-9c04-d4ed130d6775",
"isoDate": "2021-02-26T17:23:49.000Z"
}
]

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

354
tests/index-spec.ts Normal file
View File

@ -0,0 +1,354 @@
// tested class
import { PeertubeListenerRss } from "../";
// Unit test
import path from "path";
import events from "events";
import nock from "nock";
import * as chai from "chai";
import sinon from "ts-sinon";
import sinonChai from "sinon-chai";
chai.use(sinonChai);
const expect = chai.expect;
// default value (more easy when it's aliases)
const defaultChannelID = "3";
const defaultInstanceAdress = "video.monsieurbidouille.fr";
const defaultTimeloop = 15;
const defaultHistory = [
"https://video.monsieurbidouille.fr/videos/watch/5ffe5a3a-8377-472d-9c04-d4ed130d6775",
];
// expected value during my test
const expectedChannelAddress = `https://${defaultInstanceAdress}/feeds/videos.xml?videoChannelId=${defaultChannelID}`;
const expectedCustomFields = {
"media:group": ["media:group"],
};
const expectedElmts = require("./RessourcesTest/expectedElmts.json");
const expectedFirstElmt = expectedElmts[0];
const expectedLastElmt = {}; //expectedElmts[1];
// let's test
describe("test ytbListener_RSS class", function () {
describe("test constructor", function () {
it("construct with 3 params", function () {
// given
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID,
defaultTimeloop, // 15 sec
defaultHistory
);
// assertions
expect(listener.address).to.be.eql(expectedChannelAddress);
expect(listener.timeloop).to.be.eql(defaultTimeloop);
expect(listener.customfields).to.be.eql(expectedCustomFields);
expect(listener.lastEntriesLinks).to.be.eql(defaultHistory);
});
it("construct with 2 params (without history)", function () {
// given
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID,
defaultTimeloop // 15 sec
);
// assertions
expect(listener.address).to.be.eql(expectedChannelAddress);
expect(listener.timeloop).to.be.eql(defaultTimeloop);
expect(listener.customfields).to.be.eql(expectedCustomFields);
});
it("construct with 1 params (without history and timeloop)", function () {
// given
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID
);
// assertions
expect(listener.address).to.be.eql(expectedChannelAddress);
expect(listener.timeloop).to.be.eql(5 * 60);
expect(listener.customfields).to.be.eql(expectedCustomFields);
});
});
describe.only("integration test", function () {
beforeEach(function () {
nock.disableNetConnect();
});
afterEach(function () {
nock.cleanAll();
});
it.only("fetches", async function () {
// given
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID,
defaultTimeloop
);
//when
const res = await listener.fetchRSS();
// assertion
// console.log(JSON.stringify(res.items[0]));
expect(res.items[0]).to.be.eql(expectedFirstElmt);
//expect(res.items[res.items.length - 1]).to.be.eql(expectedLastElmt);
});
/**
* This test will test the usage of the librairie with, in the order
* - 1 fetch of the original document (here trigger update and new_entries events)
* - 1 fetch with the original document and a new entry (here trigger update and new_entries events)
* - 1 fetch of the previous document (here trigger update events)
*/
it("fetches with start loop in 3 times", async function () {
// given
const clock = sinon.useFakeTimers();
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID,
defaultTimeloop
);
// spy
const updateListenerSpy = sinon.spy();
const newEntriesListenerSpy = sinon.spy();
// start timer
listener.on("update", updateListenerSpy);
listener.on("newEntries", newEntriesListenerSpy);
listener.start();
// when
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledOnce;
expect(updateListenerSpy.firstCall.args[0])
.to.have.property("items")
.that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
expect(newEntriesListenerSpy).to.have.been.called;
expect(
newEntriesListenerSpy.firstCall.args[0]
).that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
expect(updateListenerSpy.firstCall.args[0].items.length).to.be.eql(
newEntriesListenerSpy.firstCall.args[0].length
);
// todo update the rss feed
// Fake RSS entry to simulate an update
const newEntry = expectedElmts[2];
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
// when
await clock.tickAsync(15000);
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledTwice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([
expectedFirstElmt,
expectedLastElmt,
newEntry,
]);
expect(newEntriesListenerSpy).to.have.been.calledTwice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([newEntry]);
// when
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
await clock.tickAsync(15000);
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledThrice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([
expectedFirstElmt,
expectedLastElmt,
newEntry,
]);
expect(newEntriesListenerSpy).to.not.have.been.calledThrice;
// then
listener.stop();
});
/**
* This test will test the usage of the librairie with, in the order
* - 1 fetch of the original document (here trigger update and new_entries events)
* - 1 fetch with the original document and a new entry (here trigger update and new_entries events)
* - 1 fetch of the previous document (here trigger update events)
*/
it("fetches with start loop in 3 times and history is initialize", async function () {
// given
const clock = sinon.useFakeTimers();
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
const listener = new PeertubeListenerRss(
defaultInstanceAdress,
defaultChannelID,
defaultTimeloop,
defaultHistory
);
// spy
const updateListenerSpy = sinon.spy();
const newEntriesListenerSpy = sinon.spy();
// start timer
listener.on("update", updateListenerSpy);
listener.on("newEntries", newEntriesListenerSpy);
listener.start();
// when
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledOnce;
expect(updateListenerSpy.firstCall.args[0])
.to.have.property("items")
.that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
expect(newEntriesListenerSpy).to.have.been.called;
expect(newEntriesListenerSpy.firstCall.args[0])
.that.deep.include.members([expectedLastElmt])
.and.that.not.deep.include.members([expectedFirstElmt]);
expect(updateListenerSpy.firstCall.args[0].items.length).to.be.eql(
newEntriesListenerSpy.firstCall.args[0].length + 1
);
// todo update the rss feed
// Fake RSS entry to simulate an update
const newEntry = expectedElmts[2];
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
// when
await clock.tickAsync(15000);
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledTwice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([
expectedFirstElmt,
expectedLastElmt,
newEntry,
]);
expect(newEntriesListenerSpy).to.have.been.calledTwice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([newEntry]);
// when
nock(`https://${defaultInstanceAdress}`)
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
.replyWithFile(
200,
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
{
"content-type": "application/rss+xml",
charset: "UTF-8",
}
);
await clock.tickAsync(15000);
await events.once(listener, "update");
// assertion
expect(updateListenerSpy).to.have.been.calledThrice;
expect(updateListenerSpy.secondCall.args[0])
.to.have.property("items")
.that.deep.include.members([
expectedFirstElmt,
expectedLastElmt,
newEntry,
]);
expect(newEntriesListenerSpy).to.not.have.been.calledThrice;
// then
listener.stop();
});
});
});

7
tests/tsconfig.json Normal file
View File

@ -0,0 +1,7 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true
}
}

21
tsconfig.base.json Normal file
View File

@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2018",
"module": "es2015",
"lib": ["es6"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"build/",
"node_modules"
]
}