forked from Outils-PeerTube/peertube-plugin-auto-import-ytb
Fix some refactiring error. And Add a way to insert admin credentials
This commit is contained in:
parent
6407c1b072
commit
2c6df73d02
|
@ -27,21 +27,21 @@ class PeerTubeRequester {
|
||||||
this.password = config.password;
|
this.password = config.password;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async requestAuthToken(): Promise<any> {
|
async requestAuthToken(): Promise<any> {
|
||||||
let response = await fetch(
|
let response = await fetch(
|
||||||
new URL(`/api/v1/oauth-clients/local`, this.domainName)
|
new URL(`/api/v1/oauth-clients/local`, this.domainName)
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(response.statusText); // CRASH
|
throw new Error("Cannot get client credentials : " + response.statusText); // CRASH
|
||||||
}
|
}
|
||||||
const { client_id: clientId, client_secret: clientSecret } =
|
const { client_id: clientId, client_secret: clientSecret } =
|
||||||
await response.json();
|
await response.json();
|
||||||
|
|
||||||
const clientInfo: { [key: string]: string } = {
|
const clientInfo: { [key: string]: string } = {
|
||||||
clientId,
|
client_id: clientId,
|
||||||
clientSecret,
|
client_secret: clientSecret,
|
||||||
grantType: "password",
|
grant_type: "password",
|
||||||
responseType: "code",
|
response_type: "code",
|
||||||
username: this.username,
|
username: this.username,
|
||||||
password: this.password,
|
password: this.password,
|
||||||
};
|
};
|
||||||
|
@ -54,7 +54,7 @@ class PeerTubeRequester {
|
||||||
body: myParams,
|
body: myParams,
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(response.statusText); // CRASH
|
throw new Error("Cannot get access Token : " + response.statusText); // CRASH
|
||||||
}
|
}
|
||||||
const { access_token: accessToken } = await response.json();
|
const { access_token: accessToken } = await response.json();
|
||||||
return accessToken;
|
return accessToken;
|
||||||
|
|
71
src/main.ts
71
src/main.ts
|
@ -10,7 +10,8 @@ type ListenerData = ListenerRss.Config & {
|
||||||
let myManager: ListenerRssAggregator;
|
let myManager: ListenerRssAggregator;
|
||||||
let listenersDataBinding = new Map<string, ListenerData>();
|
let listenersDataBinding = new Map<string, ListenerData>();
|
||||||
let logger: any;
|
let logger: any;
|
||||||
let peertube: PeerTubeRequester;
|
let peertube: PeerTubeRequester | undefined = undefined;
|
||||||
|
let goodPeertubeCredential: boolean = false;
|
||||||
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
@ -29,6 +30,18 @@ async function register({
|
||||||
type: "input-textarea",
|
type: "input-textarea",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
registerSetting({
|
||||||
|
name: "admin-name",
|
||||||
|
label: "Admin Username",
|
||||||
|
type: "input",
|
||||||
|
});
|
||||||
|
|
||||||
|
registerSetting({
|
||||||
|
name: "admin-password",
|
||||||
|
label: "Admin Password",
|
||||||
|
type: "input-password",
|
||||||
|
});
|
||||||
|
|
||||||
logger.debug("setting register");
|
logger.debug("setting register");
|
||||||
fs.appendFileSync(path.join(basePath, "/storage.bd"), "");
|
fs.appendFileSync(path.join(basePath, "/storage.bd"), "");
|
||||||
|
|
||||||
|
@ -37,20 +50,36 @@ async function register({
|
||||||
);
|
);
|
||||||
myManager = new ListenerRssAggregator(configAggregator);
|
myManager = new ListenerRssAggregator(configAggregator);
|
||||||
|
|
||||||
peertube = new PeerTubeRequester({
|
|
||||||
domainName: "http://localhost:9000",
|
|
||||||
username: "root",
|
|
||||||
password: "test",
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.debug("Aggregator created");
|
logger.debug("Aggregator created");
|
||||||
|
|
||||||
const inputs = await settingsManager.getSetting("ytb-urls");
|
const settingYtbUrls = await settingsManager.getSetting("ytb-urls");
|
||||||
if (inputs) await addListeners(inputs);
|
if (settingYtbUrls) await addListeners(settingYtbUrls);
|
||||||
|
|
||||||
logger.debug("Config loaded");
|
const settingCredentials: any = await settingsManager.getSettings([
|
||||||
|
"admin-name",
|
||||||
|
"admin-password",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (settingCredentials["admin-name"] && settingCredentials["admin-password"])
|
||||||
|
apiRequestInitializer({
|
||||||
|
domainName: peertubeHelpers.config.getWebserverUrl(),
|
||||||
|
username: settingCredentials["admin-name"],
|
||||||
|
password: settingCredentials["admin-password"],
|
||||||
|
});
|
||||||
|
logger.debug("Actual config loaded");
|
||||||
|
|
||||||
settingsManager.onSettingsChange(async (settings: any) => {
|
settingsManager.onSettingsChange(async (settings: any) => {
|
||||||
|
if (
|
||||||
|
!peertube ||
|
||||||
|
peertube.username != settings["admin-name"] ||
|
||||||
|
peertube.password != settings["admin-password"]
|
||||||
|
)
|
||||||
|
apiRequestInitializer({
|
||||||
|
domainName: peertubeHelpers.config.getWebserverUrl(),
|
||||||
|
username: settings["admin-name"],
|
||||||
|
password: settings["admin-password"],
|
||||||
|
});
|
||||||
|
|
||||||
await addListeners(settings["ytb-urls"]);
|
await addListeners(settings["ytb-urls"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -64,13 +93,31 @@ async function register({
|
||||||
JSON.stringify(entries)
|
JSON.stringify(entries)
|
||||||
);
|
);
|
||||||
for (const item of entries.items)
|
for (const item of entries.items)
|
||||||
|
if (peertube)
|
||||||
await peertube.uploadFromUrl({
|
await peertube.uploadFromUrl({
|
||||||
channelId: datas.channelId,
|
channelId: datas.channelId,
|
||||||
targetUrl: item.link,
|
targetUrl: item.link,
|
||||||
});
|
});
|
||||||
|
else {
|
||||||
|
logger.warn("Bad credential provides. New entries Skipped.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function apiRequestInitializer(data: PeerTubeRequester.Config) {
|
||||||
|
peertube = new PeerTubeRequester(data);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await peertube.requestAuthToken();
|
||||||
|
goodPeertubeCredential = true;
|
||||||
|
logger.debug("credential ok");
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn("Error during the credential validation : " + error);
|
||||||
|
peertube = undefined;
|
||||||
|
goodPeertubeCredential = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function addListeners(listenerInput: string) {
|
async function addListeners(listenerInput: string) {
|
||||||
let listeners: ListenerData[];
|
let listeners: ListenerData[];
|
||||||
try {
|
try {
|
||||||
|
@ -96,9 +143,9 @@ async function addListeners(listenerInput: string) {
|
||||||
myManager.stopAll();
|
myManager.stopAll();
|
||||||
await myManager.saveOverride(listeners);
|
await myManager.saveOverride(listeners);
|
||||||
|
|
||||||
if (logger) logger.warn("Configuration changed: " + listenerInput);
|
if (logger) logger.debug("Configuration changed: " + listenerInput);
|
||||||
|
|
||||||
myManager.startAll();
|
if (goodPeertubeCredential) myManager.startAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unregister() {
|
async function unregister() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user