From 8f8f0c3cb0b1d8c800fef610a5173734cea25888 Mon Sep 17 00:00:00 2001 From: Florent Date: Sun, 25 Jul 2021 16:38:19 +0200 Subject: [PATCH] Translate labels and log messages --- src/main.ts | 224 ++++++++++++++++++++++++++-------------------------- 1 file changed, 111 insertions(+), 113 deletions(-) diff --git a/src/main.ts b/src/main.ts index 56df72d..3cb66d4 100644 --- a/src/main.ts +++ b/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(); -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(); +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, +};