|
- import { EventEmitter } from 'events';
- import { CreateBaseSummarizerParams, SummarizerEventEmitter } from '../../common';
- import {
- retrieveVideoId,
- getVideoPage,
- extractDataFromPage,
- fetchTranscriptItems, TranscriptResponse,
- } from './transcript';
- import { normalizeTranscriptText, summarizeTranscript } from '../../summarizer';
-
- export interface CreateYouTubeSummarizerParams extends CreateBaseSummarizerParams {}
-
- export class YouTubeSummarizerEventEmitter extends EventEmitter implements SummarizerEventEmitter {
- constructor(private readonly params: CreateYouTubeSummarizerParams) {
- super();
- }
-
- process() {
- const {
- url,
- openaiApiKey,
- openaiOrganizationId,
- ...config
- } = this.params;
- const identifier = retrieveVideoId(url);
- let transcripts: TranscriptResponse[] = [];
-
- this.emit('process', {
- type: 'extract-data',
- phase: 'download-page',
- });
-
- getVideoPage(identifier)
- .then((videoPageBody) => {
- const pageData = extractDataFromPage(videoPageBody);
- this.emit('process', {
- type: 'extract-data',
- phase: 'success',
- });
-
- this.emit('process', {
- type: 'fetch-transcript',
- phase: 'start',
- });
- return fetchTranscriptItems(pageData, config);
- })
- .then((transcript) => {
- this.emit('process', {
- type: 'fetch-transcript',
- phase: 'success',
- });
-
- transcripts = transcript;
-
- this.emit('process', {
- type: 'normalize-caption',
- phase: 'start',
- });
-
- return normalizeTranscriptText(
- transcript.map((item) => item.text).join(' '),
- openaiApiKey,
- openaiOrganizationId,
- );
- })
- .then((normalizedCaption) => {
- this.emit('process', {
- type: 'normalize-caption',
- phase: 'success',
- });
-
- this.emit('process', {
- type: 'summarize-caption',
- phase: 'start',
- });
-
- return summarizeTranscript(normalizedCaption, openaiApiKey, openaiOrganizationId);
- })
- .then((summary) => {
- this.emit('process', {
- type: 'summarize-caption',
- phase: 'success',
- });
-
- this.emit('success', {
- contentType: 'application/json',
- content: JSON.stringify({ transcripts, summary }),
- });
- this.emit('end');
- })
- .catch((error) => {
- this.emit('error', error);
- this.emit('end');
- });
- }
- }
|