Kakulukian / youtube-transcript

Fetch transcript from a youtube video
240 stars 51 forks source link

fetchTranscript can only be called once #1

Closed MrManiak closed 3 years ago

MrManiak commented 3 years ago

Hi,

You can only use YoutubeTranscript.fetchTranscript once per process, because RE_YOUTUBE.exec returns null next time you call fetchTranscript. You can replace it by videoId.match(RE_YOUTUBE) instead.

MrManiak commented 3 years ago
  const RE_YOUTUBE = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/im;
  /**
   * Retrieve video id from url or string
   * @param videoId video url or video id
   */
  private static retrieveVideoId(videoId: string) {
    if (videoId.length === 11) return videoId;
    const matchId = videoId.match(RE_YOUTUBE);
    if (matchId && matchId.length > 1) {
      return matchId[1];
    }
    throw new YoutubeTranscriptError(
      'Impossible to retrieve Youtube video ID.'
    );
  }

Tell me if you want a PR, I can do that

Kakulukian commented 3 years ago

Hello ! Thanks for the bug, I released a new version with the fix !