diff --git a/youtube_transcript_api/__init__.py b/youtube_transcript_api/__init__.py index c9bb4eb..34e9ba7 100644 --- a/youtube_transcript_api/__init__.py +++ b/youtube_transcript_api/__init__.py @@ -1,3 +1,11 @@ from ._api import YouTubeTranscriptApi from ._transcripts import TranscriptList, Transcript -from ._errors import TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript, VideoUnavailable +from ._errors import ( + TranscriptsDisabled, + NoTranscriptFound, + CouldNotRetrieveTranscript, + VideoUnavailable, + NotTranslatable, + TranslationLanguageNotAvailable, + NoTranscriptAvailable, +) diff --git a/youtube_transcript_api/_errors.py b/youtube_transcript_api/_errors.py index 6f033c0..2b67e9e 100644 --- a/youtube_transcript_api/_errors.py +++ b/youtube_transcript_api/_errors.py @@ -11,7 +11,7 @@ class CouldNotRetrieveTranscript(Exception): 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.' + '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!' @@ -43,6 +43,10 @@ class TranscriptsDisabled(CouldNotRetrieveTranscript): CAUSE_MESSAGE = 'Subtitles are disabled for this video' +class NoTranscriptAvailable(CouldNotRetrieveTranscript): + CAUSE_MESSAGE = 'No transcripts are available for this video' + + class NotTranslatable(CouldNotRetrieveTranscript): CAUSE_MESSAGE = 'The requested language is not translatable' diff --git a/youtube_transcript_api/_transcripts.py b/youtube_transcript_api/_transcripts.py index 35601db..95f4ead 100644 --- a/youtube_transcript_api/_transcripts.py +++ b/youtube_transcript_api/_transcripts.py @@ -13,7 +13,12 @@ import re from ._html_unescaping import unescape from ._errors import ( - VideoUnavailable, NoTranscriptFound, TranscriptsDisabled, NotTranslatable, TranslationLanguageNotAvailable + VideoUnavailable, + NoTranscriptFound, + TranscriptsDisabled, + NotTranslatable, + TranslationLanguageNotAvailable, + NoTranscriptAvailable, ) from ._settings import WATCH_URL @@ -38,9 +43,14 @@ class TranscriptListFetcher(): raise TranscriptsDisabled(video_id) - return json.loads(splitted_html[1].split(',"videoDetails')[0].replace('\n', ''))[ - 'playerCaptionsTracklistRenderer' - ] + captions_json = json.loads( + splitted_html[1].split(',"videoDetails')[0].replace('\n', '') + )['playerCaptionsTracklistRenderer'] + + if 'captionTracks' not in captions_json: + raise NoTranscriptAvailable(video_id) + + return captions_json def _fetch_html(self, video_id): return self._http_client.get(WATCH_URL.format(video_id=video_id)).text.replace( diff --git a/youtube_transcript_api/test/assets/youtube_no_transcript_available.html.static b/youtube_transcript_api/test/assets/youtube_no_transcript_available.html.static new file mode 100644 index 0000000..1ea7ed2 --- /dev/null +++ b/youtube_transcript_api/test/assets/youtube_no_transcript_available.html.static @@ -0,0 +1,1349 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +MTG Top 10: BAD Cards That Suddenly Became Good - YouTube + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+
+
+
+ DE +
+
+
+ +
+
+
+

+ + + +Wird geladen... + +

+ +
+
+
+ +
+
+
+ +
+
+ + +
+
+
+ +
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

+ + + + + MTG Top 10: BAD Cards That Suddenly Became Good + + +

+
+
+ + +
+ + + + + +
+
305.276 Aufrufe
+
+
+
+
+
+ + + + + +
+
+ + + +
+
+
+
+

+ + + +Wird geladen... + +

+ +
+
+
+ +
+ +
+
+

+ + + +Wird geladen... + +

+ +
+
+ + +
+
+ Die Bewertungsfunktion ist nach Ausleihen des Videos verfügbar. +
+ +
+ +
+
+ Diese Funktion ist gerade nicht verfügbar. Bitte versuche es später noch einmal. +
+
+ + +
+ + +
+ + +
Am 25.07.2019 veröffentlicht

This video is sponsored by CardKingdom! Check out their awesome store here: http://www.cardkingdom.com/?utm_sourc...

Want to see me draft live? You can on Twitch! http://www.twitch.tv/Nizzahon

Want to support the channel? You can on Patreon!: https://www.patreon.com/Nizzahon_Magic

Follow me on Twitter for channel updates and other Magic musings: https://twitter.com/NizzahonMagic

Animations by Mike from Mythic Tales. Find his channel filled with awesome MTG animation here: https://www.youtube.com/user/RadioCom...

I Can Feel it Coming Kevin MacLeod (http://incompetech.com )
Licensed under Creative Commons: By Attribution 3.0 License
http://creativecommons.org/licenses/b...

+
    +
  • +

    + Kategorie +

    + +
  • + +
+
+
+ +
+ + +
+
+

+ + + +Wird geladen... + +

+ +
+ +
+ + +
+
+
+ + + +
+
+ +
+ +
+
+
+Anzeige +
+
+
+
+ + +
+
+
+
+
+ + + +Wenn Autoplay aktiviert ist, wird die Wiedergabe automatisch mit einem der aktuellen Videovorschläge fortgesetzt. + + + +
+

+ Nächstes Video +

+ + +
+
+ + +
+
+
+ +
+
+ +
+
+ +
+
+
+ + +
+ +
+ +
+
+ + +
+
+ + +
+ , um dieses Video zur Playlist "Später ansehen" hinzuzufügen. + +
+
+

+Hinzufügen +

+
+
+

+ + + + Playlists werden geladen... + +

+ +
+
+ + + + + + + \ No newline at end of file diff --git a/youtube_transcript_api/test/test_api.py b/youtube_transcript_api/test/test_api.py index b2897af..91a4de0 100644 --- a/youtube_transcript_api/test/test_api.py +++ b/youtube_transcript_api/test/test_api.py @@ -5,7 +5,13 @@ import os import httpretty -from youtube_transcript_api import YouTubeTranscriptApi, VideoUnavailable, NoTranscriptFound, TranscriptsDisabled +from youtube_transcript_api import ( + YouTubeTranscriptApi, + TranscriptsDisabled, + NoTranscriptFound, + VideoUnavailable, + NoTranscriptAvailable, +) def load_asset(filename): @@ -88,6 +94,16 @@ class TestYouTubeTranscriptApi(TestCase): with self.assertRaises(NoTranscriptFound): YouTubeTranscriptApi.get_transcript('GJLlxj_dtq8', languages=['cz']) + def test_get_transcript__exception_if_no_transcript_available(self): + httpretty.register_uri( + httpretty.GET, + 'https://www.youtube.com/watch', + body=load_asset('youtube_no_transcript_available.html.static') + ) + + with self.assertRaises(NoTranscriptAvailable): + YouTubeTranscriptApi.get_transcript('MwBPvcYFY2E') + def test_get_transcripts(self): video_id_1 = 'video_id_1' video_id_2 = 'video_id_2'