This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_wget.py
executable file
·91 lines (70 loc) · 2.62 KB
/
example_wget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""Example implementation of ProgressBarWget. Does not save to file.
This example application downloads a specified file to null. Its only purpose
is to show a progress bar and ETA similar to that of the wget application.
Usage:
example_wget.py [--ignore-length] <url>
Options:
--ignore-length Ignore `Content-Length' header field.
"""
from __future__ import print_function
import locale
import os
import signal
import sys
import threading
import time
from docopt import docopt
from etaprogress.progress import ProgressBarWget
import requests
OPTIONS = docopt(__doc__) if __name__ == '__main__' else dict()
def error(message, code=1):
"""Prints an error message to stderr and exits with a status of 1 by default."""
if message:
print('ERROR: {0}'.format(message), file=sys.stderr)
else:
print(file=sys.stderr)
sys.exit(code)
class DownloadThread(threading.Thread):
"""Downloads the file, but doesn't save it (just the file size)."""
def __init__(self, response):
super(DownloadThread, self).__init__()
self.response = response
self._bytes_downloaded = 0
self.daemon = True
def run(self):
for chunk in self.response.iter_content(1024):
self._bytes_downloaded += len(chunk)
@property
def bytes_downloaded(self):
"""Read-only interface to _bytes_downloaded."""
return self._bytes_downloaded
def main():
"""From: http://stackoverflow.com/questions/20801034/how-to-measure-download-speed-and-progress-using-requests"""
# Prepare.
if os.name == 'nt':
locale.setlocale(locale.LC_ALL, 'english-us')
else:
locale.resetlocale()
response = requests.get(OPTIONS['<url>'], stream=True)
content_length = None if OPTIONS['--ignore-length'] else int(response.headers.get('Content-Length'))
progress_bar = ProgressBarWget(content_length, eta_every=4)
thread = DownloadThread(response)
print_every_seconds = 0.25
# Download.
thread.start()
while True:
progress_bar.numerator = thread.bytes_downloaded
print(progress_bar, end='\r')
sys.stdout.flush()
# For undefined downloads (no content-length), check if thread has stopped. Loop only checks defined downloads.
if not thread.isAlive():
progress_bar.force_done = True
break
if progress_bar.done:
break
time.sleep(print_every_seconds)
print(progress_bar) # Always print one last time.
if __name__ == '__main__':
signal.signal(signal.SIGINT, lambda *_: error('', 0)) # Properly handle Control+C
main()