Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding better exception for when blocked by bot protection #337

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion youtube_transcript_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class YouTubeTranscriptApi(object):
@classmethod
def list_transcripts(cls, video_id, proxies=None, cookies=None):
def list_transcripts(cls, video_id, proxies=None, cookies=None, verify=True):
"""
Retrieves the list of transcripts which are available for a given video. It returns a `TranscriptList` object
which is iterable and provides methods to filter the list of transcripts for specific languages. While iterating
Expand Down Expand Up @@ -61,13 +61,16 @@ def list_transcripts(cls, video_id, proxies=None, cookies=None):
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
:param cookies: a string of the path to a text file containing youtube authorization cookies
:type cookies: str
:param verify: SSL Verification default. Will make your application vulnerable to man-in-the-middle (MitM) attacks.
:type verify: bool
:return: the list of available transcripts
:rtype TranscriptList:
"""
with requests.Session() as http_client:
if cookies:
http_client.cookies = cls._load_cookies(cookies, video_id)
http_client.proxies = proxies if proxies else {}
http_client.verify = verify
return TranscriptListFetcher(http_client).fetch(video_id)

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions youtube_transcript_api/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class TranscriptsDisabled(CouldNotRetrieveTranscript):
CAUSE_MESSAGE = 'Subtitles are disabled for this video'


class BotRestricted(CouldNotRetrieveTranscript):
CAUSE_MESSAGE = 'Login required because video is restricted by bot detection system'


class NoTranscriptAvailable(CouldNotRetrieveTranscript):
CAUSE_MESSAGE = 'No transcripts are available for this video'

Expand Down
3 changes: 3 additions & 0 deletions youtube_transcript_api/_transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ._html_unescaping import unescape
from ._errors import (
VideoUnavailable,
BotRestricted,
TooManyRequests,
YouTubeRequestFailed,
NoTranscriptFound,
Expand Down Expand Up @@ -58,6 +59,8 @@ def _extract_captions_json(self, html, video_id):
raise TooManyRequests(video_id)
if '"playabilityStatus":' not in html:
raise VideoUnavailable(video_id)
if '"playabilityStatus":' in html and '"Sign in to confirm you’re not a bot"' in html:
raise BotRestricted(video_id)

raise TranscriptsDisabled(video_id)

Expand Down