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