i missed that

This commit is contained in:
Amaury 2021-05-23 16:07:33 +02:00
parent b5087b8d2c
commit 866578f422
3 changed files with 213 additions and 0 deletions

45
src/discordParser.ts Normal file
View File

@ -0,0 +1,45 @@
import { Channel, Client } from 'discord.js';
import { isBooleanObject } from 'util/types';
import { ImplementableApi } from './implementableApi';
namespace DiscordParser {
export type Config = ImplementableApi.Config & {
token: string;
};
}
class DiscordParser extends ImplementableApi {
readonly token: string;
readonly channelid: string;
client: Client;
constructor(readonly config: DiscordParser.Config) {
super(config);
this.token = config.token;
// this.settingEvents();
this.client = new Client();
this.instantiateClient();
}
private instantiateClient() {
this.client.login(this.token);
}
public receivedMessage(message: ImplementableApi.Message) {
switch (message.type) {
case 'newEntriesNotify':
default:
break;
}
}
private sendMsg(message: string) {
// this.client.channels.resolveID();
}
private settingEvents(): void {
throw 'empty';
}
}
export { DiscordParser };

51
src/logWriter.ts Normal file
View File

@ -0,0 +1,51 @@
import { ImplementableApi } from './implementableApi';
import {
appendFileSync,
closeSync,
openSync,
writeFile,
writeFileSync,
} from 'fs';
namespace LogWriter {
export type Config = ImplementableApi.Config & {
path: string;
};
}
class LogWriter extends ImplementableApi {
readonly path: string;
constructor(readonly config: LogWriter.Config) {
super(config);
this.path = config.path;
this.firstWrite();
}
private firstWrite() {
this.writeMsg('LogWriter is running');
}
private writeMsg(message: string): void;
private writeMsg(message: ImplementableApi.Message): void;
private writeMsg(message: ImplementableApi.Message | string) {
if (typeof message !== 'string')
message = `[${message.type} :: ${new Date().toLocaleString(
'fr-FR'
)}] ${message.rawContent} ${
message.idListener ?? `( listener_id : ${message.idListener} )`
}`;
const fd = openSync(this.path, 'a');
const str = `[${new Date().toLocaleString('fr-FR')}] ${message}`;
appendFileSync(fd, str);
closeSync(fd);
}
public receivedMessage(message: ImplementableApi.Message) {
this.writeMsg(message);
}
}
export { LogWriter };

117
src/peertubeRequester.ts Normal file
View File

@ -0,0 +1,117 @@
import { ImplementableApi } from './implementableApi';
namespace PeerTubeRequester {
export type Config = ImplementableApi.Config & {
domain_name: string;
username: string;
password: string;
};
}
type UploadInstruction = {
[key: string]: string;
channelID: string;
targetUrl: string;
name: string;
description: string;
originallyPublishedAt: string;
thumbnailsfile: string;
};
type ClientToken = {
client_id: string;
client_secret: string;
grant_type: 'password';
response_type: 'code';
username: string;
password: string;
};
type UserToken = {
access_token: string;
token_type: string;
expires_in: string;
refresh_token: string;
};
class PeerTubeRequester extends ImplementableApi {
readonly domain_name: string;
readonly username: string;
readonly password: string;
constructor(readonly config: PeerTubeRequester.Config) {
super(config);
this.domain_name = config.domain_name;
this.username = config.username;
this.password = config.password;
}
public receivedMessage(message: ImplementableApi.Message) {
switch (message.type) {
case 'newEntriesNotify':
this.newEntriesNotify(message);
break;
default:
break;
}
}
private newEntriesNotify(message: ImplementableApi.Message) {
// parse content
const items = message.rawContent['items'];
if (Array.isArray(items))
items.forEach((item) => {
const media_group = item['media:group'];
const args: UploadInstruction = {
channelID: 'undefined', // to do binding avec les idDeChaines Skeptikom
description: media_group['media:description'][0],
name: media_group['media:title'][0],
originallyPublishedAt: item.pubDate,
targetUrl: media_group['media:content'][0]['$']['url'],
thumbnailsfile:
media_group['media:thumbnail'][0]['$']['url'],
};
this.apiRequest(args);
});
}
private async apiRequest(message: UploadInstruction) {
// Auth
const client_info: ClientToken = {
...(await (
await fetch(
`https://${this.domain_name}/api/v1/oauth-clients/local`
)
).json()),
...{
grant_type: 'password',
response_type: 'code',
username: this.username,
password: this.password,
},
};
const myAuthForm = new FormData();
for (const key in client_info) myAuthForm.append(key, message[key]);
const tokens_info: UserToken = await (
await fetch(`https://${this.domain_name}/api/v1/users/token`, {
method: 'get',
body: myAuthForm,
})
).json();
// Upload
const myUploadForm = new FormData();
const myHeader = new Headers();
myHeader.append('Authorization', `Bearer ${tokens_info.access_token}`);
for (const key in message) myUploadForm.append(key, message[key]);
await fetch(`https://${this.domain_name}/api/v1/videos/imports`, {
method: 'post',
headers: myHeader,
body: myUploadForm,
});
}
}
export { PeerTubeRequester };