UNPKG

@playzone/youtube-transcript

Version:

A JavaScript API which allows you to get the transcripts/subtitles for a given YouTube video. It also works for automatically generated subtitles and it does not require a headless browser.

212 lines 9.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CookieInvalid = exports.CookiePathInvalid = exports.CookieError = exports.PoTokenRequired = exports.NoTranscriptFound = exports.FailedToCreateConsentCookie = exports.TranslationLanguageNotAvailable = exports.NotTranslatable = exports.AgeRestricted = exports.TranscriptsDisabled = exports.IpBlocked = exports.RequestBlocked = exports.InvalidVideoId = exports.VideoUnavailable = exports.VideoUnplayable = exports.YouTubeRequestFailed = exports.YouTubeDataUnparsable = exports.CouldNotRetrieveTranscript = exports.YouTubeTranscriptApiException = void 0; /** * Base exception class for all YouTube Transcript API errors */ class YouTubeTranscriptApiException extends Error { constructor(message) { super(message); this.name = 'YouTubeTranscriptApiException'; } } exports.YouTubeTranscriptApiException = YouTubeTranscriptApiException; /** * Base class for errors that prevent transcript retrieval */ class CouldNotRetrieveTranscript extends YouTubeTranscriptApiException { constructor(videoId) { super(''); this.videoId = videoId; } buildErrorMessage() { const videoUrl = `https://www.youtube.com/watch?v=${this.videoId}`; let errorMessage = CouldNotRetrieveTranscript.ERROR_MESSAGE.replace('{videoUrl}', videoUrl); const cause = this.getCause(); if (cause) { errorMessage += CouldNotRetrieveTranscript.CAUSE_MESSAGE_INTRO.replace('{cause}', cause) + CouldNotRetrieveTranscript.GITHUB_REFERRAL; } return errorMessage; } getCause() { return ''; } toString() { return this.buildErrorMessage(); } } exports.CouldNotRetrieveTranscript = CouldNotRetrieveTranscript; CouldNotRetrieveTranscript.ERROR_MESSAGE = '\nCould not retrieve a transcript for the video {videoUrl}!'; CouldNotRetrieveTranscript.CAUSE_MESSAGE_INTRO = ' This is most likely caused by:\n\n{cause}'; CouldNotRetrieveTranscript.GITHUB_REFERRAL = '\n\nIf you are sure that the described cause is not responsible for this error ' + 'and that a transcript should be retrievable, please create an issue at ' + 'https://github.com/jdepoix/youtube-transcript-api/issues. ' + 'Please add which version of youtube_transcript_api you are using ' + 'and provide the information needed to replicate the error. ' + 'Also make sure that there are no open issues which already describe your problem!'; class YouTubeDataUnparsable extends CouldNotRetrieveTranscript { getCause() { return 'The data required to fetch the transcript is not parsable. This should ' + 'not happen, please open an issue (make sure to include the video ID)!'; } } exports.YouTubeDataUnparsable = YouTubeDataUnparsable; class YouTubeRequestFailed extends CouldNotRetrieveTranscript { constructor(videoId, reason) { super(videoId); this.reason = reason; } getCause() { return `Request to YouTube failed: ${this.reason}`; } } exports.YouTubeRequestFailed = YouTubeRequestFailed; class VideoUnplayable extends CouldNotRetrieveTranscript { constructor(videoId, reason, subReasons = []) { super(videoId); this.reason = reason; this.subReasons = subReasons; } getCause() { const reasonText = this.reason || 'No reason specified!'; if (this.subReasons.length > 0) { const subReasonsText = this.subReasons.map(r => ` - ${r}`).join('\n'); return `The video is unplayable for the following reason: ${reasonText}\n\nAdditional Details:\n${subReasonsText}`; } return `The video is unplayable for the following reason: ${reasonText}`; } } exports.VideoUnplayable = VideoUnplayable; class VideoUnavailable extends CouldNotRetrieveTranscript { getCause() { return 'The video is no longer available'; } } exports.VideoUnavailable = VideoUnavailable; class InvalidVideoId extends CouldNotRetrieveTranscript { getCause() { return 'You provided an invalid video id. Make sure you are using the video id and NOT the url!\n\n' + 'Do NOT run: `YouTubeTranscriptApi().fetch("https://www.youtube.com/watch?v=1234")`\n' + 'Instead run: `YouTubeTranscriptApi().fetch("1234")`'; } } exports.InvalidVideoId = InvalidVideoId; class RequestBlocked extends CouldNotRetrieveTranscript { constructor(videoId) { super(videoId); this._proxyConfig = null; } withProxyConfig(proxyConfig) { this._proxyConfig = proxyConfig; return this; } getCause() { if (this._proxyConfig) { // Add proxy-specific error messages here return RequestBlocked.CAUSE_MESSAGE; } return RequestBlocked.CAUSE_MESSAGE; } } exports.RequestBlocked = RequestBlocked; RequestBlocked.BASE_CAUSE_MESSAGE = 'YouTube is blocking requests from your IP. This usually is due to one of the ' + 'following reasons:\n' + '- You have done too many requests and your IP has been blocked by YouTube\n' + '- You are doing requests from an IP belonging to a cloud provider (like AWS, ' + 'Google Cloud Platform, Azure, etc.). Unfortunately, most IPs from cloud ' + 'providers are blocked by YouTube.\n\n'; RequestBlocked.CAUSE_MESSAGE = RequestBlocked.BASE_CAUSE_MESSAGE + 'There are two things you can do to work around this:\n' + '1. Use proxies to hide your IP address, as explained in the "Working around ' + 'IP bans" section of the README ' + '(https://github.com/jdepoix/youtube-transcript-api' + '?tab=readme-ov-file' + '#working-around-ip-bans-requestblocked-or-ipblocked-exception).\n' + '2. (NOT RECOMMENDED) If you authenticate your requests using cookies, you ' + 'will be able to continue doing requests for a while. However, YouTube will ' + 'eventually permanently ban the account that you have used to authenticate ' + 'with! So only do this if you don\'t mind your account being banned!'; class IpBlocked extends RequestBlocked { getCause() { return RequestBlocked.BASE_CAUSE_MESSAGE + 'Ways to work around this are explained in the "Working around IP ' + 'bans" section of the README (https://github.com/jdepoix/youtube-transcript-api' + '?tab=readme-ov-file' + '#working-around-ip-bans-requestblocked-or-ipblocked-exception).\n'; } } exports.IpBlocked = IpBlocked; class TranscriptsDisabled extends CouldNotRetrieveTranscript { getCause() { return 'Subtitles are disabled for this video'; } } exports.TranscriptsDisabled = TranscriptsDisabled; class AgeRestricted extends CouldNotRetrieveTranscript { getCause() { return 'This video is age-restricted. Therefore, you are unable to retrieve ' + 'transcripts for it without authenticating yourself.\n\n' + 'Unfortunately, Cookie Authentication is temporarily unsupported in ' + 'youtube-transcript-api, as recent changes in YouTube\'s API broke the previous ' + 'implementation. I will do my best to re-implement it as soon as possible.'; } } exports.AgeRestricted = AgeRestricted; class NotTranslatable extends CouldNotRetrieveTranscript { getCause() { return 'The requested language is not translatable'; } } exports.NotTranslatable = NotTranslatable; class TranslationLanguageNotAvailable extends CouldNotRetrieveTranscript { getCause() { return 'The requested translation language is not available'; } } exports.TranslationLanguageNotAvailable = TranslationLanguageNotAvailable; class FailedToCreateConsentCookie extends CouldNotRetrieveTranscript { getCause() { return 'Failed to automatically give consent to saving cookies'; } } exports.FailedToCreateConsentCookie = FailedToCreateConsentCookie; class NoTranscriptFound extends CouldNotRetrieveTranscript { constructor(videoId, requestedLanguageCodes, transcriptData) { super(videoId); this.requestedLanguageCodes = requestedLanguageCodes; this.transcriptData = transcriptData; } getCause() { return `No transcripts were found for any of the requested language codes: ${this.requestedLanguageCodes.join(', ')}\n\n${this.transcriptData}`; } } exports.NoTranscriptFound = NoTranscriptFound; class PoTokenRequired extends CouldNotRetrieveTranscript { getCause() { return 'The requested video cannot be retrieved without a PO Token. If this happens, ' + 'please open a GitHub issue!'; } } exports.PoTokenRequired = PoTokenRequired; // Cookie-related errors (currently disabled) class CookieError extends YouTubeTranscriptApiException { constructor(message) { super(message); this.name = 'CookieError'; } } exports.CookieError = CookieError; class CookiePathInvalid extends CookieError { constructor(cookiePath) { super(`Can't load the provided cookie file: ${cookiePath}`); } } exports.CookiePathInvalid = CookiePathInvalid; class CookieInvalid extends CookieError { constructor(cookiePath) { super(`The cookies provided are not valid (may have expired): ${cookiePath}`); } } exports.CookieInvalid = CookieInvalid; //# sourceMappingURL=index.js.map