CaptionQuery implements Mapping from lang codes
Browse files- pytube/query.py +12 -9
- tests/test_captions.py +7 -3
pytube/query.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
|
3 |
"""This module provides a query interface for media streams and captions."""
|
4 |
from typing import Callable, List, Optional, Union
|
5 |
-
from collections.abc import Sequence
|
6 |
|
7 |
from pytube import Stream, Caption
|
8 |
from pytube.helpers import deprecated
|
@@ -356,7 +356,7 @@ class StreamQuery(Sequence):
|
|
356 |
return f"{self.fmt_streams}"
|
357 |
|
358 |
|
359 |
-
class CaptionQuery(
|
360 |
"""Interface for querying the available captions."""
|
361 |
|
362 |
def __init__(self, captions: List[Caption]):
|
@@ -366,9 +366,9 @@ class CaptionQuery(Sequence):
|
|
366 |
list of :class:`Caption <Caption>` instances.
|
367 |
|
368 |
"""
|
369 |
-
self.captions = captions
|
370 |
self.lang_code_index = {c.code: c for c in captions}
|
371 |
|
|
|
372 |
def get_by_language_code(self, lang_code: str) -> Optional[Caption]:
|
373 |
"""Get the :class:`Caption <Caption>` for a given ``lang_code``.
|
374 |
|
@@ -381,20 +381,23 @@ class CaptionQuery(Sequence):
|
|
381 |
"""
|
382 |
return self.lang_code_index.get(lang_code)
|
383 |
|
384 |
-
@deprecated("This object can be treated as a
|
385 |
def all(self) -> List[Caption]: # pragma: no cover
|
386 |
"""Get all the results represented by this query as a list.
|
387 |
|
388 |
:rtype: list
|
389 |
|
390 |
"""
|
391 |
-
return self.
|
392 |
|
393 |
-
def __getitem__(self, i:
|
394 |
-
return self.
|
395 |
|
396 |
def __len__(self) -> int:
|
397 |
-
return len(self.
|
|
|
|
|
|
|
398 |
|
399 |
def __repr__(self) -> str:
|
400 |
-
return f"{self.
|
|
|
2 |
|
3 |
"""This module provides a query interface for media streams and captions."""
|
4 |
from typing import Callable, List, Optional, Union
|
5 |
+
from collections.abc import Mapping, Sequence
|
6 |
|
7 |
from pytube import Stream, Caption
|
8 |
from pytube.helpers import deprecated
|
|
|
356 |
return f"{self.fmt_streams}"
|
357 |
|
358 |
|
359 |
+
class CaptionQuery(Mapping):
|
360 |
"""Interface for querying the available captions."""
|
361 |
|
362 |
def __init__(self, captions: List[Caption]):
|
|
|
366 |
list of :class:`Caption <Caption>` instances.
|
367 |
|
368 |
"""
|
|
|
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 |
|
|
|
381 |
"""
|
382 |
return self.lang_code_index.get(lang_code)
|
383 |
|
384 |
+
@deprecated("This object can be treated as a dictionary")
|
385 |
def all(self) -> List[Caption]: # pragma: no cover
|
386 |
"""Get all the results represented by this query as a list.
|
387 |
|
388 |
:rtype: list
|
389 |
|
390 |
"""
|
391 |
+
return list(self.lang_code_index.values())
|
392 |
|
393 |
+
def __getitem__(self, i: str):
|
394 |
+
return self.lang_code_index[i]
|
395 |
|
396 |
def __len__(self) -> int:
|
397 |
+
return len(self.lang_code_index)
|
398 |
+
|
399 |
+
def __iter__(self):
|
400 |
+
return iter(self.lang_code_index)
|
401 |
|
402 |
def __repr__(self) -> str:
|
403 |
+
return f"{self.lang_code_index}"
|
tests/test_captions.py
CHANGED
@@ -2,6 +2,8 @@
|
|
2 |
from unittest import mock
|
3 |
from unittest.mock import patch, mock_open, MagicMock
|
4 |
|
|
|
|
|
5 |
from pytube import Caption, CaptionQuery, captions
|
6 |
|
7 |
|
@@ -20,9 +22,11 @@ def test_caption_query_sequence():
|
|
20 |
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
|
21 |
)
|
22 |
caption_query = CaptionQuery(captions=[caption1, caption2])
|
23 |
-
assert caption_query.captions == [caption1, caption2]
|
24 |
assert len(caption_query) == 2
|
25 |
-
assert caption_query[
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
def test_caption_query_get_by_language_code_when_exists():
|
@@ -104,7 +108,7 @@ def test_repr():
|
|
104 |
assert str(caption) == '<Caption lang="name1" code="en">'
|
105 |
|
106 |
caption_query = CaptionQuery(captions=[caption])
|
107 |
-
assert repr(caption_query) == '
|
108 |
|
109 |
|
110 |
@mock.patch("pytube.request.get")
|
|
|
2 |
from unittest import mock
|
3 |
from unittest.mock import patch, mock_open, MagicMock
|
4 |
|
5 |
+
import pytest
|
6 |
+
|
7 |
from pytube import Caption, CaptionQuery, captions
|
8 |
|
9 |
|
|
|
22 |
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
|
23 |
)
|
24 |
caption_query = CaptionQuery(captions=[caption1, caption2])
|
|
|
25 |
assert len(caption_query) == 2
|
26 |
+
assert caption_query["en"] == caption1
|
27 |
+
assert caption_query["fr"] == caption2
|
28 |
+
with pytest.raises(KeyError):
|
29 |
+
caption_query["nada"]
|
30 |
|
31 |
|
32 |
def test_caption_query_get_by_language_code_when_exists():
|
|
|
108 |
assert str(caption) == '<Caption lang="name1" code="en">'
|
109 |
|
110 |
caption_query = CaptionQuery(captions=[caption])
|
111 |
+
assert repr(caption_query) == '{\'en\': <Caption lang="name1" code="en">}'
|
112 |
|
113 |
|
114 |
@mock.patch("pytube.request.get")
|