Edits history of script submission #12993 for ' Create voice (speechify)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Speechify = {
      token: string;
    };
    type Base64 = string;
    /**
     * Create voice
     * Create a personal (cloned) voice for the user
     */
    export async function main(
      auth: Speechify,
      body: {
        name: string;
        gender: "male" | "female" | "notSpecified";
        sample: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
        avatar?: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
        consent: string;
      },
    ) {
      const url = new URL(`https://api.sws.speechify.com/v1/voices`);
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined && v !== "") {
          if (["sample", "avatar"].includes(k)) {
            const { base64, type, name } = v as {
              base64: Base64;
              type: string;
              name: string;
            };
            formData.append(
              k,
              new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
                type,
              }),
              name,
            );
          } else {
            formData.append(k, String(v));
          }
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 428 days ago