hbmartin commited on
Commit
c227094
·
1 Parent(s): 06ecde8

OTF and filesize_approx tests

Browse files
Files changed (3) hide show
  1. pytube/query.py +7 -1
  2. tests/test_query.py +8 -0
  3. tests/test_streams.py +9 -0
pytube/query.py CHANGED
@@ -288,7 +288,13 @@ class StreamQuery:
288
  """
289
  return self.filter(only_audio=True, subtype=subtype).order_by("abr").last()
290
 
291
- def otf(self, is_otf: bool = False):
 
 
 
 
 
 
292
  return self._filter([lambda s: s.is_otf == is_otf])
293
 
294
  def first(self) -> Optional[Stream]:
 
288
  """
289
  return self.filter(only_audio=True, subtype=subtype).order_by("abr").last()
290
 
291
+ def otf(self, is_otf: bool = False) -> "StreamQuery":
292
+ """Filter stream by OTF, useful if some streams have 404 URLs
293
+
294
+ :param bool is_otf: Set to False to retrieve only non-OTF streams
295
+ :rtype: :class:`StreamQuery <StreamQuery>`
296
+ :returns: A StreamQuery object with otf filtered streams
297
+ """
298
  return self._filter([lambda s: s.is_otf == is_otf])
299
 
300
  def first(self) -> Optional[Stream]:
tests/test_query.py CHANGED
@@ -162,3 +162,11 @@ def test_get_audio_only(cipher_signature):
162
 
163
  def test_get_audio_only_with_subtype(cipher_signature):
164
  assert cipher_signature.streams.get_audio_only(subtype="webm").itag == 251
 
 
 
 
 
 
 
 
 
162
 
163
  def test_get_audio_only_with_subtype(cipher_signature):
164
  assert cipher_signature.streams.get_audio_only(subtype="webm").itag == 251
165
+
166
+
167
+ def test_otf(cipher_signature):
168
+ non_otf = cipher_signature.streams.otf().all()
169
+ assert len(non_otf) == 22
170
+
171
+ otf = cipher_signature.streams.otf(True).all()
172
+ assert len(otf) == 0
tests/test_streams.py CHANGED
@@ -14,6 +14,15 @@ def test_filesize(cipher_signature, mocker):
14
  assert cipher_signature.streams.first().filesize == 6796391
15
 
16
 
 
 
 
 
 
 
 
 
 
17
  def test_default_filename(cipher_signature):
18
  expected = "PSY - GANGNAM STYLE(강남스타일) MV.mp4"
19
  stream = cipher_signature.streams.first()
 
14
  assert cipher_signature.streams.first().filesize == 6796391
15
 
16
 
17
+ def test_filesize_approx(cipher_signature, mocker):
18
+ mocker.patch.object(request, "head")
19
+ request.head.return_value = {"content-length": "123"}
20
+ stream = cipher_signature.streams.first()
21
+ assert stream.filesize_approx == 22350604
22
+ stream.bitrate = None
23
+ assert stream.filesize_approx == 123
24
+
25
+
26
  def test_default_filename(cipher_signature):
27
  expected = "PSY - GANGNAM STYLE(강남스타일) MV.mp4"
28
  stream = cipher_signature.streams.first()