san
commited on
Commit
·
533fad4
1
Parent(s):
44732de
added progress bar, and function to read size in human readable format, updated call to print_status function
Browse files- pytube/models.py +3 -1
- pytube/utils.py +39 -6
- scripts/pytubectl +3 -0
pytube/models.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from __future__ import unicode_literals
|
2 |
from os.path import normpath, isfile
|
3 |
from os import remove
|
|
|
4 |
try:
|
5 |
from urllib2 import urlopen
|
6 |
except ImportError:
|
@@ -60,6 +61,7 @@ class Video(object):
|
|
60 |
file_size = int(meta_data.get("Content-Length") or
|
61 |
meta_data.get("content-length"))
|
62 |
self._bytes_received = 0
|
|
|
63 |
try:
|
64 |
with open(fullpath, 'wb') as dst_file:
|
65 |
# Print downloading message
|
@@ -76,7 +78,7 @@ class Video(object):
|
|
76 |
self._bytes_received += len(self._buffer)
|
77 |
dst_file.write(self._buffer)
|
78 |
if on_progress:
|
79 |
-
on_progress(self._bytes_received, file_size)
|
80 |
|
81 |
# Catch possible exceptions occurring during download
|
82 |
except IOError:
|
|
|
1 |
from __future__ import unicode_literals
|
2 |
from os.path import normpath, isfile
|
3 |
from os import remove
|
4 |
+
from time import clock
|
5 |
try:
|
6 |
from urllib2 import urlopen
|
7 |
except ImportError:
|
|
|
61 |
file_size = int(meta_data.get("Content-Length") or
|
62 |
meta_data.get("content-length"))
|
63 |
self._bytes_received = 0
|
64 |
+
start = clock()
|
65 |
try:
|
66 |
with open(fullpath, 'wb') as dst_file:
|
67 |
# Print downloading message
|
|
|
78 |
self._bytes_received += len(self._buffer)
|
79 |
dst_file.write(self._buffer)
|
80 |
if on_progress:
|
81 |
+
on_progress(self._bytes_received, file_size, start)
|
82 |
|
83 |
# Catch possible exceptions occurring during download
|
84 |
except IOError:
|
pytube/utils.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import re
|
2 |
from sys import stdout
|
|
|
3 |
|
4 |
def safe_filename(text, max_length=200):
|
5 |
"""
|
@@ -27,8 +28,38 @@ def safe_filename(text, max_length=200):
|
|
27 |
filename = blacklist.sub('', text)
|
28 |
return truncate(filename)
|
29 |
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
This function - when passed as `on_progress` to `Video.download` - prints
|
34 |
out the current download progress.
|
@@ -36,10 +67,12 @@ def print_status(progress, file_size):
|
|
36 |
Arguments:
|
37 |
progress -- The lenght of the currently downloaded bytes.
|
38 |
file_size -- The total size of the video.
|
|
|
39 |
"""
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
45 |
stdout.flush()
|
|
|
1 |
import re
|
2 |
from sys import stdout
|
3 |
+
from time import clock
|
4 |
|
5 |
def safe_filename(text, max_length=200):
|
6 |
"""
|
|
|
28 |
filename = blacklist.sub('', text)
|
29 |
return truncate(filename)
|
30 |
|
31 |
+
def sizeof(bytes):
|
32 |
+
""" Takes the size of file or folder in bytes and
|
33 |
+
returns size formatted in kb, MB, GB, TB or PB.
|
34 |
|
35 |
+
Args:
|
36 |
+
bytes(int): size of the file in bytes
|
37 |
+
Return:
|
38 |
+
(str): containing size with formatting.
|
39 |
+
"""
|
40 |
+
alternative = [
|
41 |
+
(1024 ** 5, ' PB'),
|
42 |
+
(1024 ** 4, ' TB'),
|
43 |
+
(1024 ** 3, ' GB'),
|
44 |
+
(1024 ** 2, ' MB'),
|
45 |
+
(1024 ** 1, ' KB'),
|
46 |
+
(1024 ** 0, (' byte', ' bytes')),
|
47 |
+
]
|
48 |
+
|
49 |
+
for factor, suffix in alternative:
|
50 |
+
if bytes >= factor:
|
51 |
+
break
|
52 |
+
amount = int(bytes/factor)
|
53 |
+
if isinstance(suffix, tuple):
|
54 |
+
singular, multiple = suffix
|
55 |
+
if amount == 1:
|
56 |
+
suffix = singular
|
57 |
+
else:
|
58 |
+
suffix = multiple
|
59 |
+
return "%s%s" % (str(amount), suffix)
|
60 |
+
|
61 |
+
|
62 |
+
def print_status(progress, file_size, start):
|
63 |
"""
|
64 |
This function - when passed as `on_progress` to `Video.download` - prints
|
65 |
out the current download progress.
|
|
|
67 |
Arguments:
|
68 |
progress -- The lenght of the currently downloaded bytes.
|
69 |
file_size -- The total size of the video.
|
70 |
+
start -- time when started
|
71 |
"""
|
72 |
+
done = int(50 * progress / int(file_size))
|
73 |
+
percentDone = int(progress) * 100. / file_size
|
74 |
+
speed = sizeof(progress//(clock() - start))
|
75 |
+
bar = '=' * done
|
76 |
+
die = ' ' * (50-done)
|
77 |
+
stdout.write("\r[%s%s][%3.2f%%] %s of %s at %s/s " % (bar, die, percentDone, sizeof(progress), sizeof(file_size), speed))
|
78 |
stdout.flush()
|
scripts/pytubectl
CHANGED
@@ -69,3 +69,6 @@ def _main():
|
|
69 |
except KeyboardInterrupt:
|
70 |
print "Download interrupted."
|
71 |
sys.exit(1)
|
|
|
|
|
|
|
|
69 |
except KeyboardInterrupt:
|
70 |
print "Download interrupted."
|
71 |
sys.exit(1)
|
72 |
+
|
73 |
+
if __name__ == '__main__':
|
74 |
+
_main()
|