Get transcript summaries of Web videos.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

94 рядки
2.5 KiB

  1. import { EventEmitter } from 'events';
  2. import { CreateBaseSummarizerParams, SummarizerEventEmitter } from '../../common';
  3. import {
  4. retrieveVideoId,
  5. getVideoPage,
  6. extractDataFromPage,
  7. fetchTranscriptItems,
  8. } from './transcript';
  9. import { normalizeTranscriptText, summarizeTranscript } from '../../summarizer';
  10. export type CreateYouTubeSummarizerParams = CreateBaseSummarizerParams
  11. export class YouTubeSummarizerEventEmitter extends EventEmitter implements SummarizerEventEmitter {
  12. constructor(private readonly params: CreateYouTubeSummarizerParams) {
  13. super();
  14. }
  15. process() {
  16. const {
  17. url,
  18. openAiParams,
  19. ...config
  20. } = this.params;
  21. const identifier = retrieveVideoId(url);
  22. this.emit('process', {
  23. processType: 'extract-data',
  24. phase: 'download-page',
  25. });
  26. getVideoPage(identifier)
  27. .then((videoPageBody) => {
  28. const pageData = extractDataFromPage(videoPageBody);
  29. this.emit('process', {
  30. processType: 'extract-data',
  31. phase: 'success',
  32. });
  33. this.emit('process', {
  34. processType: 'fetch-transcript',
  35. phase: 'start',
  36. });
  37. return fetchTranscriptItems(pageData, config);
  38. })
  39. .then((transcript) => {
  40. this.emit('process', {
  41. processType: 'fetch-transcript',
  42. phase: 'success',
  43. content: JSON.stringify(transcript),
  44. contentType: 'application/json',
  45. });
  46. this.emit('process', {
  47. processType: 'normalize-transcript',
  48. phase: 'start',
  49. });
  50. return normalizeTranscriptText({
  51. rawTranscriptText: transcript.map((item) => item.text).join(' '),
  52. openAiParams,
  53. });
  54. })
  55. .then((normalizedTranscript) => {
  56. this.emit('process', {
  57. processType: 'normalize-transcript',
  58. phase: 'success',
  59. content: normalizedTranscript,
  60. contentType: 'text/plain',
  61. });
  62. this.emit('process', {
  63. processType: 'summarize-transcript',
  64. phase: 'start',
  65. });
  66. return summarizeTranscript({ normalizedTranscript, openAiParams });
  67. })
  68. .then((summary) => {
  69. this.emit('process', {
  70. processType: 'summarize-transcript',
  71. phase: 'success',
  72. content: summary,
  73. contentType: 'text/plain',
  74. });
  75. this.emit('end');
  76. })
  77. .catch((error) => {
  78. this.emit('error', error);
  79. this.emit('end');
  80. });
  81. }
  82. }