diff --git a/typescript/audio2text/package.json b/typescript/audio2text/package.json index 3c2206e..217a026 100644 --- a/typescript/audio2text/package.json +++ b/typescript/audio2text/package.json @@ -14,12 +14,13 @@ "Transform" ], "repository": { - "type":"git", - "url":"https://github.com/scramjetorg/platform-samples/tree/main/typescript/audio2text" + "type": "git", + "url": "https://github.com/scramjetorg/platform-samples/tree/main/typescript/audio2text" }, "author": "piotrek6641", "license": "ISC", "dependencies": { + "@scramjet/obj-logger": "^0.34.3", "@scramjet/utility": "^0.33.5", "axios": "^1.3.6", "date-and-time": "^2.4.3", diff --git a/typescript/audio2text/src/index.ts b/typescript/audio2text/src/index.ts index 3803c2b..eac0f0f 100644 --- a/typescript/audio2text/src/index.ts +++ b/typescript/audio2text/src/index.ts @@ -7,9 +7,12 @@ const app: ReadableApp = async function( ) { const assembly = new Assembly(token); - await assembly.processFile(); + assembly.logger.outputLogStream.pipe(this.logger.inputLogStream); - return Promise.resolve("finished"); + return assembly.processFileFs().then(res => { + this.logger.info("Result", res); + return res; + }); }; export default app; diff --git a/typescript/audio2text/src/utils/assembly.ts b/typescript/audio2text/src/utils/assembly.ts index 91bc443..df2ebd5 100644 --- a/typescript/audio2text/src/utils/assembly.ts +++ b/typescript/audio2text/src/utils/assembly.ts @@ -3,39 +3,42 @@ import axios, { AxiosInstance } from "axios"; import { defer } from "@scramjet/utility"; import { readFile } from "fs"; import path from "path"; +import { ObjLogger } from "@scramjet/obj-logger"; +import { Readable } from "stream"; export class Assembly { //path to file change song.wav to name of your file //file has to be in a same directory file = path.join(__dirname, "song.wav"); - fileUrl:string = ""; - status:string = ""; - transciptId:string = ""; - token:string; - client:AxiosInstance; + logger = new ObjLogger("Assembly"); + fileUrl: string = ""; + transciptId: string = ""; + client: AxiosInstance; constructor(token:string) { - this.token = token; this.client = axios.create({ baseURL: "https://api.assemblyai.com/v2", headers: { - authorization: this.token, //conf.apiKey, + authorization: token, "transfer-encoding": "chunked", + "content-type" : "application/octet-stream" }, }); } - private async upload(): Promise { + private async uploadFromFs(): Promise { return new Promise((resolve, reject) => { readFile(this.file, async (err, data) => { if (err) reject(err); - await this.client .post("/upload", data) .then((res) => { this.fileUrl = res.data.upload_url; - resolve("succesfully uploaded"); + this.logger.info("succesfully uploaded"); + this.logger.info("upload URL", this.fileUrl); + + resolve() }) - .catch((error) => console.error(error)); + .catch((error) => this.logger.error("got axios error:", error)); }); }); } @@ -46,30 +49,45 @@ export class Assembly { }) .then((res) => { this.transciptId = res.data.id; }); } - private async transcript():Promise { - return new Promise(async (resolve, _reject) => { - while (this.status !== "completed") { - this.client - .get(`/transcript/${this.transciptId}`) - // eslint-disable-next-line no-loop-func - .then((res) => { - if (res.data.status === "completed") { - resolve(res.data.text); - } else { - console.log(res.data.status + "..."); - this.status = res.data.status; - } - }) - .catch(console.error); + private async upload(inputStream: Readable): Promise { + return this.client + .post("/upload", inputStream) + .then((res) => { + this.fileUrl = res.data.upload_url; - await defer(3000); - } - }); + this.logger.info("succesfully uploaded"); + this.logger.info("upload URL", this.fileUrl); + }) + .catch((error) => this.logger.error(error)); + } + private async transcript(): Promise { + const result = await this.client + .get<{ status?: string, error?: string, text?: string }>(`/transcript/${this.transciptId}`) + .then((res) => { + if (res.data.status === "error") { + this.logger.info("Got error from API", res.data.error); + } + + return res.data; + }); + + if (result.status !== "completed") { + this.logger.info(result.status); + await defer(3000); + return this.transcript(); + } + + return result.text; + } + async processFileFs(): Promise { + await this.uploadFromFs(); + await this.startTranscript(); + return this.transcript(); } - async processFile():Promise { - await this.upload(); + async processFile(inputStream: Readable): Promise { + await this.upload(inputStream); await this.startTranscript(); - await this.transcript().then(console.log); + return this.transcript(); } }