Sid Shardanand
commited on
Introducing filesize returned in kb, mb and gb (#1364)
Browse files* Enable inbuilt conversion(with rounding) to KB, MB and GB for the filesize attribute
* return kb, mb, gb as floats rounded up to 3 significant figures
* updating filesize function with appropriate naming conventions
- pytube/streams.py +62 -0
- tests/test_streams.py +8 -0
pytube/streams.py
CHANGED
@@ -8,6 +8,8 @@ separately).
|
|
8 |
"""
|
9 |
import logging
|
10 |
import os
|
|
|
|
|
11 |
from datetime import datetime
|
12 |
from typing import BinaryIO, Dict, Optional, Tuple
|
13 |
from urllib.error import HTTPError
|
@@ -61,6 +63,15 @@ class Stream:
|
|
61 |
|
62 |
# filesize in bytes
|
63 |
self._filesize: Optional[int] = int(stream.get('contentLength', 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Additional information about the stream format, such as resolution,
|
66 |
# frame rate, and whether the stream is live (HLS) or 3D.
|
@@ -149,7 +160,58 @@ class Stream:
|
|
149 |
raise
|
150 |
self._filesize = request.seq_filesize(self.url)
|
151 |
return self._filesize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
@property
|
154 |
def title(self) -> str:
|
155 |
"""Get title of video
|
|
|
8 |
"""
|
9 |
import logging
|
10 |
import os
|
11 |
+
from math import ceil
|
12 |
+
|
13 |
from datetime import datetime
|
14 |
from typing import BinaryIO, Dict, Optional, Tuple
|
15 |
from urllib.error import HTTPError
|
|
|
63 |
|
64 |
# filesize in bytes
|
65 |
self._filesize: Optional[int] = int(stream.get('contentLength', 0))
|
66 |
+
|
67 |
+
# filesize in kilobytes
|
68 |
+
self._filesize_kb: Optional[float] = float(ceil(stream.get('contentLength', 0) / 1024 * 1000) / 1000)
|
69 |
+
|
70 |
+
# filesize in megabytes
|
71 |
+
self._filesize_mb: Optional[float] = float(ceil(stream.get('contentLength', 0) / 1024 / 1024 * 1000) / 1000)
|
72 |
+
|
73 |
+
# filesize in gigabytes(fingers crossed we don't need terabytes going forward though)
|
74 |
+
self._filesize_gb: Optional[float] = float(ceil(stream.get('contentLength', 0) / 1024 / 1024 / 1024 * 1000) / 1000)
|
75 |
|
76 |
# Additional information about the stream format, such as resolution,
|
77 |
# frame rate, and whether the stream is live (HLS) or 3D.
|
|
|
160 |
raise
|
161 |
self._filesize = request.seq_filesize(self.url)
|
162 |
return self._filesize
|
163 |
+
|
164 |
+
@property
|
165 |
+
def filesizekb(self) -> float:
|
166 |
+
"""File size of the media stream in bytes.
|
167 |
+
|
168 |
+
:rtype: float
|
169 |
+
:returns:
|
170 |
+
Rounded filesize (in kilobytes) of the stream.
|
171 |
+
"""
|
172 |
+
if self._filesize_kb == 0:
|
173 |
+
try:
|
174 |
+
self._filesize_kb = float(ceil(request.filesize(self.url)/1024 * 1000) / 1000)
|
175 |
+
except HTTPError as e:
|
176 |
+
if e.code != 404:
|
177 |
+
raise
|
178 |
+
self._filesize_kb = float(ceil(request.seq_filesize(self.url)/1024 * 1000) / 1000)
|
179 |
+
return self._filesize_kb
|
180 |
+
|
181 |
+
@property
|
182 |
+
def filesizemb(self) -> float:
|
183 |
+
"""File size of the media stream in bytes.
|
184 |
+
|
185 |
+
:rtype: float
|
186 |
+
:returns:
|
187 |
+
Rounded filesize (in megabytes) of the stream.
|
188 |
+
"""
|
189 |
+
if self._filesize_mb == 0:
|
190 |
+
try:
|
191 |
+
self._filesize_mb = float(ceil(request.filesize(self.url)/1024/1024 * 1000) / 1000)
|
192 |
+
except HTTPError as e:
|
193 |
+
if e.code != 404:
|
194 |
+
raise
|
195 |
+
self._filesize_mb = float(ceil(request.seq_filesize(self.url)/1024/1024 * 1000) / 1000)
|
196 |
+
return self._filesize_mb
|
197 |
+
|
198 |
+
@property
|
199 |
+
def filesizegb(self) -> float:
|
200 |
+
"""File size of the media stream in bytes.
|
201 |
|
202 |
+
:rtype: float
|
203 |
+
:returns:
|
204 |
+
Rounded filesize (in gigabytes) of the stream.
|
205 |
+
"""
|
206 |
+
if self._filesize_gb == 0:
|
207 |
+
try:
|
208 |
+
self._filesize_gb = float(ceil(request.filesize(self.url)/1024/1024/1024 * 1000) / 1000)
|
209 |
+
except HTTPError as e:
|
210 |
+
if e.code != 404:
|
211 |
+
raise
|
212 |
+
self._filesize_gb = float(ceil(request.seq_filesize(self.url)/1024/1024/1024 * 1000) / 1000)
|
213 |
+
return self._filesize_gb
|
214 |
+
|
215 |
@property
|
216 |
def title(self) -> str:
|
217 |
"""Get title of video
|
tests/test_streams.py
CHANGED
@@ -29,7 +29,15 @@ def test_stream_to_buffer(mock_request, cipher_signature):
|
|
29 |
|
30 |
def test_filesize(cipher_signature):
|
31 |
assert cipher_signature.streams[0].filesize == 3399554
|
|
|
|
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def test_filesize_approx(cipher_signature):
|
35 |
stream = cipher_signature.streams[0]
|
|
|
29 |
|
30 |
def test_filesize(cipher_signature):
|
31 |
assert cipher_signature.streams[0].filesize == 3399554
|
32 |
+
|
33 |
+
def test_filesize_kb(cipher_signature):
|
34 |
+
assert cipher_signature.streams[0].filesize_kb == float(3319.877)
|
35 |
|
36 |
+
def test_filesize_mb(cipher_signature):
|
37 |
+
assert cipher_signature.streams[0].filesize_mb == float(3.243)
|
38 |
+
|
39 |
+
def test_filesize_gb(cipher_signature):
|
40 |
+
assert cipher_signature.streams[0].filesize_gb == float(0.004)
|
41 |
|
42 |
def test_filesize_approx(cipher_signature):
|
43 |
stream = cipher_signature.streams[0]
|