Compare commits
	
		
			1 Commits
		
	
	
		
			develop
			...
			feature/us
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					4f18003fb4 | 
@@ -1,110 +1,122 @@
 | 
				
			|||||||
// Api request lib
 | 
					// Api request lib
 | 
				
			||||||
import fetch, { Headers } from "node-fetch";
 | 
					import fetch, { Headers } from "node-fetch";
 | 
				
			||||||
import { URL, URLSearchParams } from "url";
 | 
					import { URL, URLSearchParams } from "url";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace PeerTubeRequester {
 | 
					namespace PeerTubeRequester {
 | 
				
			||||||
  export type Config = {
 | 
					  export type Config = {
 | 
				
			||||||
    domainName: string | URL;
 | 
					    domainName: string | URL;
 | 
				
			||||||
    username: string;
 | 
					    username: string;
 | 
				
			||||||
    password: string;
 | 
					    password: string;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type UploadInstruction = {
 | 
					type UploadInstruction = {
 | 
				
			||||||
  [key: string]: string;
 | 
					  [key: string]: string;
 | 
				
			||||||
  channelId: string;
 | 
					  channelId: string;
 | 
				
			||||||
  targetUrl: string;
 | 
					  targetUrl: string;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PeerTubeRequester {
 | 
					class PeerTubeRequester {
 | 
				
			||||||
  readonly domainName: URL;
 | 
					  readonly domainName: URL;
 | 
				
			||||||
  readonly username: string;
 | 
					  readonly username: string;
 | 
				
			||||||
  readonly password: string;
 | 
					  readonly password: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(readonly config: PeerTubeRequester.Config) {
 | 
					  constructor(readonly config: PeerTubeRequester.Config) {
 | 
				
			||||||
    this.domainName = new URL("/", config.domainName);
 | 
					    this.domainName = new URL("/", config.domainName);
 | 
				
			||||||
    this.username = config.username;
 | 
					    this.username = config.username;
 | 
				
			||||||
    this.password = config.password;
 | 
					    this.password = config.password;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  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("Cannot get client credentials : " + 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 } = {
 | 
				
			||||||
      client_id: clientId,
 | 
					      client_id: clientId,
 | 
				
			||||||
      client_secret: clientSecret,
 | 
					      client_secret: clientSecret,
 | 
				
			||||||
      grant_type: "password",
 | 
					      grant_type: "password",
 | 
				
			||||||
      response_type: "code",
 | 
					      response_type: "code",
 | 
				
			||||||
      username: this.username,
 | 
					      username: this.username,
 | 
				
			||||||
      password: this.password,
 | 
					      password: this.password,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let myParams = new URLSearchParams();
 | 
					    let myParams = new URLSearchParams();
 | 
				
			||||||
    for (const key in clientInfo) myParams.append(key, clientInfo[key]);
 | 
					    for (const key in clientInfo) myParams.append(key, clientInfo[key]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    response = await fetch(new URL(`/api/v1/users/token`, this.domainName), {
 | 
					    response = await fetch(new URL(`/api/v1/users/token`, this.domainName), {
 | 
				
			||||||
      method: "post",
 | 
					      method: "post",
 | 
				
			||||||
      body: myParams,
 | 
					      body: myParams,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    if (!response.ok) {
 | 
					    if (!response.ok) {
 | 
				
			||||||
      throw new Error("Cannot get access Token : " + 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;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async uploadFromUrl(message: UploadInstruction): Promise<void> {
 | 
					  async uploadFromUrl(message: UploadInstruction): Promise<void> {
 | 
				
			||||||
    const accessToken = await this.requestAuthToken();
 | 
					    const accessToken = await this.requestAuthToken();
 | 
				
			||||||
    const myUploadForm = new URLSearchParams();
 | 
					    const myUploadForm = new URLSearchParams();
 | 
				
			||||||
    const myHeader = new Headers();
 | 
					    const myHeader = new Headers();
 | 
				
			||||||
    myHeader.append("Authorization", `Bearer ${accessToken}`);
 | 
					    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]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const response = await fetch(
 | 
					    const response = await fetch(
 | 
				
			||||||
      new URL("/api/v1/videos/imports", this.domainName),
 | 
					      new URL("/api/v1/videos/imports", this.domainName),
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        method: "post",
 | 
					        method: "post",
 | 
				
			||||||
        headers: myHeader,
 | 
					        headers: myHeader,
 | 
				
			||||||
        body: myUploadForm,
 | 
					        body: myUploadForm,
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!response.ok) {
 | 
					    if (!response.ok) {
 | 
				
			||||||
      switch (response.status) {
 | 
					      switch (response.status) {
 | 
				
			||||||
        case 400:
 | 
					        case 400:
 | 
				
			||||||
          throw new Error(
 | 
					          throw new Error(
 | 
				
			||||||
            `Bad or malformed request. Probably because your target URL (from Youtube?) was not accepted by the API.\
 | 
					            `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}.
 | 
					                        The target URL you attempted to pass: ${message.targetUrl}.
 | 
				
			||||||
                        Response from the server: ${response.statusText}`
 | 
					                        Response from the server: ${response.statusText}`
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case 403:
 | 
					        case 403:
 | 
				
			||||||
          throw new Error(response.statusText);
 | 
					          throw new Error(response.statusText);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case 409:
 | 
					        case 409:
 | 
				
			||||||
          throw new Error(
 | 
					          throw new Error(
 | 
				
			||||||
            `Oops, your instance did 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 encountered an undocumented issues.\
 | 
					            `Oh, you encountered an undocumented issues.\
 | 
				
			||||||
                        Please create an issue to the plugin project.
 | 
					                        Please create an issue to the plugin project.
 | 
				
			||||||
                        ERROR: ${response.statusText}`
 | 
					                        ERROR: ${response.statusText}`
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					
 | 
				
			||||||
 | 
					   async getChannelId(channelName: string): Promise<number> {
 | 
				
			||||||
export { PeerTubeRequester };
 | 
					      const response = await fetch(
 | 
				
			||||||
 | 
					        new URL(`/api/v1/videos-channels/${channelName}`, this.domainName),
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          method: "gett"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const { id } = await response.json();
 | 
				
			||||||
 | 
					      return id;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export { PeerTubeRequester };
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user