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 {
|
2021-07-28 14:20:55 +02:00
|
|
|
readonly domainName: URL;
|
2021-07-09 14:26:23 +02:00
|
|
|
readonly username: string;
|
|
|
|
readonly password: string;
|
|
|
|
|
|
|
|
constructor(readonly config: PeerTubeRequester.Config) {
|
2021-07-28 14:20:55 +02:00
|
|
|
this.domainName = new URL("/", config.domainName);
|
2021-07-09 14:26:23 +02:00
|
|
|
this.username = config.username;
|
|
|
|
this.password = config.password;
|
|
|
|
}
|
|
|
|
|
2021-07-30 12:40:24 +02:00
|
|
|
async requestAuthToken(): Promise<any> {
|
2021-07-09 14:26:23 +02:00
|
|
|
let response = await fetch(
|
2021-07-28 14:20:55 +02:00
|
|
|
new URL(`/api/v1/oauth-clients/local`, this.domainName)
|
2021-07-09 14:26:23 +02:00
|
|
|
);
|
|
|
|
if (!response.ok) {
|
2021-07-30 12:40:24 +02:00
|
|
|
throw new Error("Cannot get client credentials : " + response.statusText); // CRASH
|
2021-07-09 14:26:23 +02:00
|
|
|
}
|
2021-07-28 14:20:55 +02:00
|
|
|
const { client_id: clientId, client_secret: clientSecret } =
|
|
|
|
await response.json();
|
2021-07-09 14:26:23 +02:00
|
|
|
|
2021-07-28 14:20:55 +02:00
|
|
|
const clientInfo: { [key: string]: string } = {
|
2021-07-30 12:40:24 +02:00
|
|
|
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();
|
2021-07-28 14:20:55 +02:00
|
|
|
for (const key in clientInfo) myParams.append(key, clientInfo[key]);
|
2021-07-09 14:26:23 +02:00
|
|
|
|
2021-07-28 14:20:55 +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) {
|
2021-07-30 12:40:24 +02:00
|
|
|
throw new Error("Cannot get access Token : " + response.statusText); // CRASH
|
2021-07-09 14:26:23 +02:00
|
|
|
}
|
2021-07-25 16:56:25 +02:00
|
|
|
const { access_token: accessToken } = await response.json();
|
|
|
|
return accessToken;
|
|
|
|
}
|
2021-07-09 14:26:23 +02:00
|
|
|
|
2021-07-25 16:56:25 +02:00
|
|
|
async uploadFromUrl(message: UploadInstruction): Promise<void> {
|
2021-07-27 18:33:53 +02:00
|
|
|
const accessToken = await this.requestAuthToken();
|
2021-07-09 14:26:23 +02:00
|
|
|
const myUploadForm = new URLSearchParams();
|
|
|
|
const myHeader = new Headers();
|
2021-07-25 16:56:25 +02:00
|
|
|
myHeader.append("Authorization", `Bearer ${accessToken}`);
|
2021-07-09 14:26:23 +02:00
|
|
|
for (const key in message) myUploadForm.append(key, message[key]);
|
|
|
|
|
2021-07-25 16:56:25 +02:00
|
|
|
const response = await fetch(
|
2021-07-28 14:20:55 +02:00
|
|
|
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(
|
2021-07-25 16:56:25 +02:00
|
|
|
`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(
|
2021-07-25 16:56:25 +02:00
|
|
|
`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(
|
2021-07-25 16:56:25 +02:00
|
|
|
`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 };
|