|
- import { Controller } from '../../packages/fastify-controller';
- import { TranscriptService, TranscriptServiceImpl } from './TranscriptService';
- import { BaseTranscriptItem, VideoType, YouTube } from '@modal-sh/webvideo-transcript-summary-core';
- import { constants } from 'http2';
-
- export interface TranscriptController extends Controller<
- 'getVideoTranscript'
- | 'normalizeVideoTranscriptText'
- > {}
-
- export class TranscriptControllerImpl implements TranscriptController {
- constructor(
- private readonly transcriptService: TranscriptService = new TranscriptServiceImpl(),
- ) {
- // noop
- }
-
- readonly getVideoTranscript: TranscriptController['getVideoTranscript'] = async (request, reply) => {
- try {
- const { videoType, videoId } = request.params as { videoType: VideoType; videoId: string };
- const transcript = await this.transcriptService
- .getVideoTranscript(videoType, { url: videoId });
-
- reply.raw.statusMessage = 'Transcript Fetched Successfully';
- reply.send(transcript);
- } catch (errRaw) {
- const err = errRaw as Error;
- request.server.log.error(err);
-
- reply.code(constants.HTTP_STATUS_BAD_GATEWAY);
- reply.raw.statusMessage = 'Transcript Fetch Failed';
- reply.send();
- }
- };
-
- readonly normalizeVideoTranscriptText: TranscriptController['normalizeVideoTranscriptText'] = async (request, reply) => {
- try {
- const transcriptItems = request.body as BaseTranscriptItem[];
- const normalizedText = await this.transcriptService
- .normalizeVideoTranscriptText(transcriptItems);
-
- reply.raw.statusMessage = 'Transcript Text Normalized Successfully';
- reply.send(normalizedText);
- } catch (errRaw) {
- const err = errRaw as Error;
- request.server.log.error(err);
-
- reply.code(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
- reply.raw.statusMessage = 'Transcript Text Normalization Failed';
- reply.send();
- }
- };
- }
|