peertube-plugin-auto-import.../lib/peertubeRequester.ts

111 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-07-09 14:26:23 +02:00
// Api request lib
2021-07-28 14:21:21 +02:00
import fetch, { Headers } from "node-fetch";
2021-07-12 17:02:39 +02:00
import { URL, URLSearchParams } from "url";
2021-07-09 14:26:23 +02:00
namespace PeerTubeRequester {
export type Config = {
2021-07-28 14:21:21 +02:00
domainName: string | URL;
2021-07-09 14:26:23 +02:00
username: string;
password: string;
};
}
type UploadInstruction = {
[key: string]: string;
channelId: string;
targetUrl: string;
};
class PeerTubeRequester {
readonly domainName: URL;
2021-07-09 14:26:23 +02:00
readonly username: string;
readonly password: string;
constructor(readonly config: PeerTubeRequester.Config) {
this.domainName = new URL("/", config.domainName);
2021-07-09 14:26:23 +02:00
this.username = config.username;
this.password = config.password;
}
async requestAuthToken(): Promise<any> {
2021-07-09 14:26:23 +02:00
let response = await fetch(
new URL(`/api/v1/oauth-clients/local`, this.domainName)
2021-07-09 14:26:23 +02:00
);
if (!response.ok) {
throw new Error("Cannot get client credentials : " + response.statusText); // CRASH
2021-07-09 14:26:23 +02:00
}
const { client_id: clientId, client_secret: clientSecret } =
await response.json();
2021-07-09 14:26:23 +02:00
const clientInfo: { [key: string]: string } = {
client_id: clientId,
client_secret: clientSecret,
grant_type: "password",
response_type: "code",
2021-07-09 14:26:23 +02:00
username: this.username,
password: this.password,
};
let myParams = new URLSearchParams();
for (const key in clientInfo) myParams.append(key, clientInfo[key]);
2021-07-09 14:26:23 +02:00
response = await fetch(new URL(`/api/v1/users/token`, this.domainName), {
2021-07-09 14:26:23 +02:00
method: "post",
body: myParams,
});
if (!response.ok) {
throw new Error("Cannot get access Token : " + response.statusText); // CRASH
2021-07-09 14:26:23 +02:00
}
const { access_token: accessToken } = await response.json();
return accessToken;
}
2021-07-09 14:26:23 +02:00
async uploadFromUrl(message: UploadInstruction): Promise<void> {
const accessToken = await this.requestAuthToken();
2021-07-09 14:26:23 +02:00
const myUploadForm = new URLSearchParams();
const myHeader = new Headers();
myHeader.append("Authorization", `Bearer ${accessToken}`);
2021-07-09 14:26:23 +02:00
for (const key in message) myUploadForm.append(key, message[key]);
const response = await fetch(
new URL("/api/v1/videos/imports", this.domainName),
2021-07-12 17:02:39 +02:00
{
method: "post",
headers: myHeader,
body: myUploadForm,
}
);
2021-07-09 14:26:23 +02:00
if (!response.ok) {
switch (response.status) {
case 400:
throw new Error(
`Bad or malformed request. Probably because your target URL (from Youtube?) was not accepted by the API.\
The target URL you attempted to pass: ${message.targetUrl}.
Response from the server: ${response.statusText}`
2021-07-09 14:26:23 +02:00
);
break;
case 403:
throw new Error(response.statusText);
break;
case 409:
throw new Error(
`Oops, your instance did not allowed the HTTPS import.\
2021-07-09 14:26:23 +02:00
Contact your administrator.
${response.statusText}`
);
break;
default:
throw new Error(
`Oh, you encountered an undocumented issues.\
Please create an issue to the plugin project.
2021-07-09 14:26:23 +02:00
ERROR: ${response.statusText}`
);
break;
}
}
}
}
export { PeerTubeRequester };