forked from Outils-PeerTube/peertube-plugin-auto-import-ytb
		
	Split PeertubeRequester main function into 2
This commit is contained in:
		@@ -1,9 +1,6 @@
 | 
				
			|||||||
// import { ImplementableApi } from './implementableApi';
 | 
					 | 
				
			||||||
// Api request lib
 | 
					// Api request lib
 | 
				
			||||||
import fetch, { FetchError, Headers } from "node-fetch";
 | 
					import fetch, { FetchError, Headers } from "node-fetch";
 | 
				
			||||||
import { URL, URLSearchParams } from "url";
 | 
					import { URL, URLSearchParams } from "url";
 | 
				
			||||||
import FormData from "form-data";
 | 
					 | 
				
			||||||
// import dedent from "ts-dedent";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace PeerTubeRequester {
 | 
					namespace PeerTubeRequester {
 | 
				
			||||||
  export type Config = {
 | 
					  export type Config = {
 | 
				
			||||||
@@ -19,22 +16,6 @@ type UploadInstruction = {
 | 
				
			|||||||
  targetUrl: string;
 | 
					  targetUrl: 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 {
 | 
					class PeerTubeRequester {
 | 
				
			||||||
  readonly domain_name: URL;
 | 
					  readonly domain_name: URL;
 | 
				
			||||||
  readonly username: string;
 | 
					  readonly username: string;
 | 
				
			||||||
@@ -46,7 +27,7 @@ class PeerTubeRequester {
 | 
				
			|||||||
    this.password = config.password;
 | 
					    this.password = config.password;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async apiRequest(message: UploadInstruction): Promise<void> {
 | 
					  private async requestAuthToken(): Promise<void> {
 | 
				
			||||||
    let response = await fetch(
 | 
					    let response = await fetch(
 | 
				
			||||||
      new URL(`/api/v1/oauth-clients/local`, this.domain_name)
 | 
					      new URL(`/api/v1/oauth-clients/local`, this.domain_name)
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
@@ -74,15 +55,18 @@ class PeerTubeRequester {
 | 
				
			|||||||
    if (!response.ok) {
 | 
					    if (!response.ok) {
 | 
				
			||||||
      throw new Error(response.statusText); // CRASH
 | 
					      throw new Error(response.statusText); // CRASH
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const { access_token } = await response.json();
 | 
					    const { access_token: accessToken } = await response.json();
 | 
				
			||||||
 | 
					    return accessToken;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Upload
 | 
					  async uploadFromUrl(message: UploadInstruction): Promise<void> {
 | 
				
			||||||
 | 
					    const accessToken = this.requestAuthToken();
 | 
				
			||||||
    const myUploadForm = new URLSearchParams();
 | 
					    const myUploadForm = new URLSearchParams();
 | 
				
			||||||
    const myHeader = new Headers();
 | 
					    const myHeader = new Headers();
 | 
				
			||||||
    myHeader.append("Authorization", `Bearer ${access_token}`);
 | 
					    myHeader.append("Authorization", `Bearer ${accessToken}`);
 | 
				
			||||||
    for (const key in message) myUploadForm.append(key, message[key]);
 | 
					    for (const key in message) myUploadForm.append(key, message[key]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    response = await fetch(
 | 
					    const response = await fetch(
 | 
				
			||||||
      new URL(`/api/v1/videos/imports`, this.domain_name),
 | 
					      new URL(`/api/v1/videos/imports`, this.domain_name),
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        method: "post",
 | 
					        method: "post",
 | 
				
			||||||
@@ -95,9 +79,9 @@ class PeerTubeRequester {
 | 
				
			|||||||
      switch (response.status) {
 | 
					      switch (response.status) {
 | 
				
			||||||
        case 400:
 | 
					        case 400:
 | 
				
			||||||
          throw new Error(
 | 
					          throw new Error(
 | 
				
			||||||
            `Your target URL was not accepted by the API.\
 | 
					            `Bad or malformed request. Probably because your target URL (from Youtube?) was not accepted by the API.\
 | 
				
			||||||
                        Actualy it's : ${message.targetUrl}
 | 
					                        The target URL you attempted to pass: ${message.targetUrl}.
 | 
				
			||||||
                        ${response.statusText}`
 | 
					                        Response from the server: ${response.statusText}`
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case 403:
 | 
					        case 403:
 | 
				
			||||||
@@ -105,15 +89,15 @@ class PeerTubeRequester {
 | 
				
			|||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case 409:
 | 
					        case 409:
 | 
				
			||||||
          throw new Error(
 | 
					          throw new Error(
 | 
				
			||||||
            `Oops, your instance had not allowed the HTTPS import.\
 | 
					            `Oops, your instance did not allowed the HTTPS import.\
 | 
				
			||||||
                        Contact your administrator.
 | 
					                        Contact your administrator.
 | 
				
			||||||
                        ${response.statusText}`
 | 
					                        ${response.statusText}`
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        default:
 | 
					        default:
 | 
				
			||||||
          throw new Error(
 | 
					          throw new Error(
 | 
				
			||||||
            `Oh, you resolved an undocumented issues.\
 | 
					            `Oh, you encountered an undocumented issues.\
 | 
				
			||||||
                        Please report this on the git if you have the time.
 | 
					                        Please create an issue to the plugin project.
 | 
				
			||||||
                        ERROR: ${response.statusText}`
 | 
					                        ERROR: ${response.statusText}`
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user