Kakulukian / youtube-transcript

Fetch transcript from a youtube video
307 stars 61 forks source link

Modularize youtube-transcript's fetchTranscript API #35

Open rshmhrj opened 3 months ago

rshmhrj commented 3 months ago

Hi @Kakulukian @mefengl @balmacefa, here is an initial draft at refactoring to modularize the fetchTranscript method, which allows users to override specific parts -- related to #34.

Using YTTranscript from node or from a server works fine, but in the Obsidian Plugin web-view context, we are getting CORS errors. These changes should allow me to do something like this:

import { YoutubeTranscript } from 'youtube-transcript';
import { request } from "obsidian";

class ObsidianYoutubeTranscript extends YoutubeTranscript {
  videoId: string;
  videoPageBody: string;
  transcriptBody: string;
  public async getPageBody(): Promise<string> {
    const videoPageResponse = await request(
    `https://www.youtube.com/watch?v=${this.videoId}`
    );
    this.videoPageBody = videoPageResponse;
    return this.videoPageBody;
  }

  public async getTranscriptResponse(transcriptURL: string): Promise<string> {
    const transcriptResponse = await request(transcriptURL);
    this.transcriptBody = transcriptResponse;
    return this.transcriptBody;
  }
}

ObsidianYoutubeTranscript.fetchTranscript(url).then(console.log);

So I can use the obsidian.request module to perform the fetch for the pageBody and for the transcriptBody and continue to use the rest of the functionality as defined.

Also took the opportunity to add a bunch of tests, as I didn't want to break anything (and to also get a better idea of how everything worked). I used the command npm run test to execute all the tests.

image

What do you all think?

rshmhrj commented 3 months ago

Hi @Kakulukian @mefengl @balmacefa -- any chance you can make some time to review this please?