Get transcript summaries of Web videos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. import { Controller } from '../../packages/fastify-controller';
  2. import { TranscriptService, TranscriptServiceImpl } from './TranscriptService';
  3. import { BaseTranscriptItem, VideoType, YouTube } from '@modal-sh/webvideo-transcript-summary-core';
  4. import { constants } from 'http2';
  5. export interface TranscriptController extends Controller<
  6. 'getVideoTranscript'
  7. | 'normalizeVideoTranscriptText'
  8. > {}
  9. export class TranscriptControllerImpl implements TranscriptController {
  10. constructor(
  11. private readonly transcriptService: TranscriptService = new TranscriptServiceImpl(),
  12. ) {
  13. // noop
  14. }
  15. readonly getVideoTranscript: TranscriptController['getVideoTranscript'] = async (request, reply) => {
  16. try {
  17. const { videoType, videoId } = request.params as { videoType: VideoType; videoId: string };
  18. const transcript = await this.transcriptService
  19. .getVideoTranscript(videoType, { url: videoId });
  20. reply.raw.statusMessage = 'Transcript Fetched Successfully';
  21. reply.send(transcript);
  22. } catch (errRaw) {
  23. const err = errRaw as Error;
  24. request.server.log.error(err);
  25. reply.code(constants.HTTP_STATUS_BAD_GATEWAY);
  26. reply.raw.statusMessage = 'Transcript Fetch Failed';
  27. reply.send();
  28. }
  29. };
  30. readonly normalizeVideoTranscriptText: TranscriptController['normalizeVideoTranscriptText'] = async (request, reply) => {
  31. try {
  32. const transcriptItems = request.body as BaseTranscriptItem[];
  33. const normalizedText = await this.transcriptService
  34. .normalizeVideoTranscriptText(transcriptItems);
  35. reply.raw.statusMessage = 'Transcript Text Normalized Successfully';
  36. reply.send(normalizedText);
  37. } catch (errRaw) {
  38. const err = errRaw as Error;
  39. request.server.log.error(err);
  40. reply.code(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
  41. reply.raw.statusMessage = 'Transcript Text Normalization Failed';
  42. reply.send();
  43. }
  44. };
  45. }