Skip to content

Commit

Permalink
Merge pull request #308 from tmddus2/refactor/240228-clova-api
Browse files Browse the repository at this point in the history
Refactor(#307): CLOVA API ํ˜ธ์ถœ ์‹คํŒจ ์‹œ ์žฌ์‹œ๋„
  • Loading branch information
platinouss authored Mar 5, 2024
2 parents ccd291b + 52113ba commit 8b99a15
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 4 additions & 0 deletions mediaServer/src/constants/clova-api-response-type.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const SUCCEEDED = "SUCCEEDED";
const RETRIABLE_ERROR = ["ERROR_SERVER_BUSY", "ERROR_AUDIO_CONVERSION", "ERROR_INTERNAL_ERROR", "ERROR_EXTERNAL_ERROR"];

export { SUCCEEDED, RETRIABLE_ERROR };
15 changes: 15 additions & 0 deletions mediaServer/src/dto/clova-api-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const ClovaApiRequest =(url: any, code: string) =>{
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('X-CLOVASPEECH-API-KEY', process.env.CLOVA_API_KEY as string);
return {
method: 'POST',
headers: headers,
body: JSON.stringify({
language: process.env.CLOVA_API_LANGUAGE,
completion: process.env.CLOVA_API_COMPLETION,
url: url,
callback: `${process.env.SERVER_API_URL}/lecture/${code}/text`
})
}
}
11 changes: 11 additions & 0 deletions mediaServer/src/dto/clova-api-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ClovaApiReponse {
result:string;
message: string;
timestamp: string;

constructor(result:string, message:string, timestamp:string) {
this.result = result;
this.message = message;
this.timestamp = timestamp;
}
}
27 changes: 12 additions & 15 deletions mediaServer/src/utils/media-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import path from 'path';
import { PeerStreamInfo } from '../models/PeerStreamInfo';
import { FfmpegCommand } from '../models/FfmpegCommand';
import { uploadFileToObjectStorage } from './ncp-storage';
import { RETRIABLE_ERROR, SUCCEEDED } from '../constants/clova-api-response-type.constant';
import { ClovaApiReponse } from '../dto/clova-api-response.dto';
import { ClovaApiRequest } from '../dto/clova-api-request.dto';
import { AUDIO_OUTPUT_DIR } from '../constants/media-converter.constant';
ffmpeg.setFfmpegPath(ffmpegPath.path);

Expand Down Expand Up @@ -132,21 +135,15 @@ class MediaConverter {
};

extractSubtitle = async (url: any, code: string) => {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('X-CLOVASPEECH-API-KEY', process.env.CLOVA_API_KEY as string);

const response = await fetch(process.env.CLOVA_API_URL as string, {
method: 'POST',
headers: headers,
body: JSON.stringify({
language: process.env.CLOVA_API_LANGUAGE,
completion: process.env.CLOVA_API_COMPLETION,
url: url,
callback: `${process.env.SERVER_API_URL}/lecture/${code}/text`
})
});
console.log(`[${response.status}] ๊ฐ•์˜ ์ž๋ง‰ ์ €์žฅ`);
const response = await fetch(process.env.CLOVA_API_URL as string, ClovaApiRequest(url, code));
const result = await response.json() as ClovaApiReponse;

if (result.result == SUCCEEDED) {
console.log(`[${result.result}] ๊ฐ•์˜ ์ž๋ง‰ ์ €์žฅ`);
}
if (result.result in RETRIABLE_ERROR) {
const response = await fetch(process.env.CLOVA_API_URL as string, ClovaApiRequest(url, code));
}
};
}

Expand Down

0 comments on commit 8b99a15

Please sign in to comment.