forked from Outils-PeerTube/peertube-plugin-auto-import-ytb
Translate labels and log messages
This commit is contained in:
parent
9597cddc80
commit
8f8f0c3cb0
224
src/main.ts
224
src/main.ts
|
@ -1,113 +1,111 @@
|
|||
import { ListenerRssAggregator } from "listener-rss-agregator";
|
||||
import { ListenerRss } from "listener-rss";
|
||||
import { PeerTubeRequester } from "../lib/peertubeRequester";
|
||||
|
||||
type ListenerData = ListenerRss.Config & {
|
||||
address: string;
|
||||
channelId: string;
|
||||
};
|
||||
|
||||
let myManager: ListenerRssAggregator;
|
||||
let listenersDataBinding = new Map<string, ListenerData>();
|
||||
let logger: any;
|
||||
let peertube: PeerTubeRequester;
|
||||
|
||||
import * as path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
async function register({
|
||||
registerSetting,
|
||||
settingsManager,
|
||||
peertubeHelpers,
|
||||
}: any) {
|
||||
const basePath = peertubeHelpers.plugin.getDataDirectoryPath();
|
||||
logger = peertubeHelpers.logger;
|
||||
|
||||
registerSetting({
|
||||
name: "ytb-urls",
|
||||
label: "liste des urls youtube a auto-importer",
|
||||
type: "input-textarea",
|
||||
});
|
||||
|
||||
logger.warn("setting register");
|
||||
fs.appendFileSync(path.join(basePath, "/storage.bd"), "");
|
||||
|
||||
const configAggregator = await ListenerRssAggregator.instantiateAggregator(
|
||||
path.join(basePath, "/storage.bd")
|
||||
);
|
||||
myManager = new ListenerRssAggregator(configAggregator);
|
||||
|
||||
peertube = new PeerTubeRequester({
|
||||
domain_name: "http://localhost:9000",
|
||||
username: "root",
|
||||
password: "test",
|
||||
});
|
||||
|
||||
logger.warn("Aggregator created");
|
||||
|
||||
const inputs = await settingsManager.getSetting("ytb-urls");
|
||||
if (inputs) await addListeners(inputs);
|
||||
|
||||
logger.warn("Config loaded");
|
||||
|
||||
settingsManager.onSettingsChange(async (settings: any) => {
|
||||
await addListeners(settings["ytb-urls"]);
|
||||
});
|
||||
|
||||
myManager.on("newEntries", (entries: any) => {
|
||||
const datas = listenersDataBinding.get(entries.addressListener);
|
||||
if (!datas) return;
|
||||
|
||||
logger.warn(
|
||||
"Nouvelles entrées détéctées: " +
|
||||
JSON.stringify(entries) +
|
||||
" de " +
|
||||
datas.channelId
|
||||
);
|
||||
for (const item of entries.items)
|
||||
peertube.apiRequest({
|
||||
channelId: datas.channelId,
|
||||
targetUrl: item.link,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function addListeners(listenerInput: string) {
|
||||
let listeners: ListenerData[];
|
||||
try {
|
||||
listeners = JSON.parse(listenerInput);
|
||||
} catch {
|
||||
logger.warn("Erreur: malformé");
|
||||
return;
|
||||
}
|
||||
let newListeners = listeners.filter(
|
||||
(item) => !listenersDataBinding.has(item.address)
|
||||
);
|
||||
let removedUrls = Array.from(listenersDataBinding.keys()).filter(
|
||||
(url) => !listeners.some((listener) => listener.address === url)
|
||||
);
|
||||
|
||||
for (const newItem of newListeners) {
|
||||
listenersDataBinding.set(newItem.address, newItem);
|
||||
}
|
||||
for (const removedUrl of removedUrls) {
|
||||
listenersDataBinding.delete(removedUrl);
|
||||
}
|
||||
|
||||
myManager.stopAll();
|
||||
await myManager.saveOverride(listeners);
|
||||
|
||||
if (logger) logger.warn("Configuration modifiée: " + listenerInput);
|
||||
|
||||
myManager.startAll();
|
||||
}
|
||||
|
||||
async function unregister() {
|
||||
myManager.stopAll();
|
||||
return;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
unregister,
|
||||
};
|
||||
import { ListenerRssAggregator } from "listener-rss-agregator";
|
||||
import { ListenerRss } from "listener-rss";
|
||||
import { PeerTubeRequester } from "../lib/peertubeRequester";
|
||||
|
||||
type ListenerData = ListenerRss.Config & {
|
||||
address: string;
|
||||
channelId: string;
|
||||
};
|
||||
|
||||
let myManager: ListenerRssAggregator;
|
||||
let listenersDataBinding = new Map<string, ListenerData>();
|
||||
let logger: any;
|
||||
let peertube: PeerTubeRequester;
|
||||
|
||||
import * as path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
async function register({
|
||||
registerSetting,
|
||||
settingsManager,
|
||||
peertubeHelpers,
|
||||
}: any) {
|
||||
const basePath = peertubeHelpers.plugin.getDataDirectoryPath();
|
||||
logger = peertubeHelpers.logger;
|
||||
|
||||
registerSetting({
|
||||
name: "ytb-urls",
|
||||
label: 'URL list of Youtube channel to synchronize',
|
||||
type: "input-textarea",
|
||||
});
|
||||
|
||||
logger.debug("setting register");
|
||||
fs.appendFileSync(path.join(basePath, "/storage.bd"), "");
|
||||
|
||||
const configAggregator = await ListenerRssAggregator.instantiateAggregator(
|
||||
path.join(basePath, "/storage.bd")
|
||||
);
|
||||
myManager = new ListenerRssAggregator(configAggregator);
|
||||
|
||||
peertube = new PeerTubeRequester({
|
||||
domain_name: "http://localhost:9000",
|
||||
username: "root",
|
||||
password: "test",
|
||||
});
|
||||
|
||||
logger.debug("Aggregator created");
|
||||
|
||||
const inputs = await settingsManager.getSetting("ytb-urls");
|
||||
if (inputs) await addListeners(inputs);
|
||||
|
||||
logger.debug("Config loaded");
|
||||
|
||||
settingsManager.onSettingsChange(async (settings: any) => {
|
||||
await addListeners(settings["ytb-urls"]);
|
||||
});
|
||||
|
||||
myManager.on("newEntries", (entries: any) => {
|
||||
const datas = listenersDataBinding.get(entries.addressListener);
|
||||
if (!datas) return;
|
||||
|
||||
logger.debug(
|
||||
"New entries detected from channel #%i: %s",
|
||||
datas.channelId,
|
||||
JSON.stringify(entries)
|
||||
);
|
||||
for (const item of entries.items)
|
||||
peertube.apiRequest({
|
||||
channelId: datas.channelId,
|
||||
targetUrl: item.link,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function addListeners(listenerInput: string) {
|
||||
let listeners: ListenerData[];
|
||||
try {
|
||||
listeners = JSON.parse(listenerInput);
|
||||
} catch {
|
||||
logger.error("Malformed URL");
|
||||
return;
|
||||
}
|
||||
let newListeners = listeners.filter(
|
||||
(item) => !listenersDataBinding.has(item.address)
|
||||
);
|
||||
let removedUrls = Array.from(listenersDataBinding.keys()).filter(
|
||||
(url) => !listeners.some((listener) => listener.address === url)
|
||||
);
|
||||
|
||||
for (const newItem of newListeners) {
|
||||
listenersDataBinding.set(newItem.address, newItem);
|
||||
}
|
||||
for (const removedUrl of removedUrls) {
|
||||
listenersDataBinding.delete(removedUrl);
|
||||
}
|
||||
|
||||
myManager.stopAll();
|
||||
await myManager.saveOverride(listeners);
|
||||
|
||||
if (logger) logger.warn("Configuration changed: " + listenerInput);
|
||||
|
||||
myManager.startAll();
|
||||
}
|
||||
|
||||
async function unregister() {
|
||||
myManager.stopAll();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
unregister,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user