hbmartin commited on
Commit
85bb2e5
·
1 Parent(s): c16d7a9

added test_download_with_playlist_video_error

Browse files
Files changed (4) hide show
  1. pytube/cli.py +3 -3
  2. pytube/query.py +3 -1
  3. tests/test_captions.py +4 -2
  4. tests/test_cli.py +24 -0
pytube/cli.py CHANGED
@@ -426,11 +426,11 @@ def download_caption(
426
  _print_available_captions(youtube.captions)
427
  return
428
 
429
- caption = youtube.captions.get_by_language_code(lang_code=lang_code)
430
- if caption:
431
  downloaded_path = caption.download(title=youtube.title, output_path=target)
432
  print(f"Saved caption file to: {downloaded_path}")
433
- else:
434
  print(f"Unable to find caption with code: {lang_code}")
435
  _print_available_captions(youtube.captions)
436
 
 
426
  _print_available_captions(youtube.captions)
427
  return
428
 
429
+ try:
430
+ caption = youtube.captions[lang_code]
431
  downloaded_path = caption.download(title=youtube.title, output_path=target)
432
  print(f"Saved caption file to: {downloaded_path}")
433
+ except KeyError:
434
  print(f"Unable to find caption with code: {lang_code}")
435
  _print_available_captions(youtube.captions)
436
 
pytube/query.py CHANGED
@@ -369,7 +369,9 @@ class CaptionQuery(Mapping):
369
  self.lang_code_index = {c.code: c for c in captions}
370
 
371
  @deprecated("This object can be treated as a dictionary, i.e. captions['en']")
372
- def get_by_language_code(self, lang_code: str) -> Optional[Caption]:
 
 
373
  """Get the :class:`Caption <Caption>` for a given ``lang_code``.
374
 
375
  :param str lang_code:
 
369
  self.lang_code_index = {c.code: c for c in captions}
370
 
371
  @deprecated("This object can be treated as a dictionary, i.e. captions['en']")
372
+ def get_by_language_code(
373
+ self, lang_code: str
374
+ ) -> Optional[Caption]: # pragma: no cover
375
  """Get the :class:`Caption <Caption>` for a given ``lang_code``.
376
 
377
  :param str lang_code:
tests/test_captions.py CHANGED
@@ -38,7 +38,7 @@ def test_caption_query_get_by_language_code_when_exists():
38
  {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
39
  )
40
  caption_query = CaptionQuery(captions=[caption1, caption2])
41
- assert caption_query.get_by_language_code("en") == caption1
42
 
43
 
44
  def test_caption_query_get_by_language_code_when_not_exists():
@@ -49,7 +49,9 @@ def test_caption_query_get_by_language_code_when_not_exists():
49
  {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
50
  )
51
  caption_query = CaptionQuery(captions=[caption1, caption2])
52
- assert caption_query.get_by_language_code("hello") is None
 
 
53
 
54
 
55
  @mock.patch("pytube.captions.Caption.generate_srt_captions")
 
38
  {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
39
  )
40
  caption_query = CaptionQuery(captions=[caption1, caption2])
41
+ assert caption_query["en"] == caption1
42
 
43
 
44
  def test_caption_query_get_by_language_code_when_not_exists():
 
49
  {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
50
  )
51
  caption_query = CaptionQuery(captions=[caption1, caption2])
52
+ with pytest.raises(KeyError):
53
+ not_found = caption_query["hello"]
54
+ assert not_found is not None # should never reach here
55
 
56
 
57
  @mock.patch("pytube.captions.Caption.generate_srt_captions")
tests/test_cli.py CHANGED
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
6
  import pytest
7
 
8
  from pytube import cli, StreamQuery, Caption, CaptionQuery
 
9
 
10
  parse_args = cli._parse_args
11
 
@@ -236,6 +237,29 @@ def test_download_with_playlist(perform_args_on_youtube, playlist, youtube):
236
  perform_args_on_youtube.assert_called_with(youtube, args)
237
 
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  @mock.patch("pytube.cli.YouTube")
240
  @mock.patch("pytube.StreamQuery")
241
  @mock.patch("pytube.Stream")
 
6
  import pytest
7
 
8
  from pytube import cli, StreamQuery, Caption, CaptionQuery
9
+ from pytube.exceptions import PytubeError
10
 
11
  parse_args = cli._parse_args
12
 
 
237
  perform_args_on_youtube.assert_called_with(youtube, args)
238
 
239
 
240
+ @mock.patch("pytube.cli.YouTube")
241
+ @mock.patch("pytube.cli.Playlist")
242
+ @mock.patch("pytube.cli._perform_args_on_youtube")
243
+ def test_download_with_playlist_video_error(
244
+ perform_args_on_youtube, playlist, youtube, capsys
245
+ ):
246
+ # Given
247
+ cli.safe_filename = MagicMock(return_value="safe_title")
248
+ parser = argparse.ArgumentParser()
249
+ args = parse_args(parser, ["https://www.youtube.com/playlist?list=PLyn"])
250
+ cli._parse_args = MagicMock(return_value=args)
251
+ videos = [youtube]
252
+ playlist_instance = playlist.return_value
253
+ playlist_instance.videos = videos
254
+ perform_args_on_youtube.side_effect = PytubeError()
255
+ # When
256
+ cli.main()
257
+ # Then
258
+ playlist.assert_called()
259
+ captured = capsys.readouterr()
260
+ assert "There was an error with video" in captured.out
261
+
262
+
263
  @mock.patch("pytube.cli.YouTube")
264
  @mock.patch("pytube.StreamQuery")
265
  @mock.patch("pytube.Stream")