-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelpDownloader.py
65 lines (51 loc) · 1.57 KB
/
HelpDownloader.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
#!/usr/bin/env python
"""
Загрузчик справки с сайта
Microsoft's Old API Help File Reborn
http://laurencejackson.com/win32
"""
from urllib.request import urlopen
#
# read_chunk(url, max_chunk)
#
def read_chunk(url, max_chunk=8192*8):
"""Генератор содержимого web-файла, для использования в инструкции for"""
with urlopen(url) as response:
while True:
chunk = response.read(max_chunk)
if chunk:
yield chunk
else:
return
#
# download(url, name)
#
def download(url, name):
"""Скачивает web-файл на диск"""
# определяем размер скачиваемого файла
file_size = 0
with urlopen(url) as response:
header = response.getheader('Content-Length')
file_size = int(header)
print('File size: %dKB' % (file_size / 1024))
# скачиваем файл на диск
with open(name, 'wb') as file:
downloaded_size = 0
for chunk in read_chunk(url):
downloaded_size += len(chunk)
file.write(chunk)
# выводим прогресс чтения файла
status = 'Downloading... %3.2f%% complete.' % (downloaded_size * 100. / file_size)
print(status, end='\r')
print()
#
# main()
#
def main():
"""Точка входа в программу"""
download('http://laurencejackson.com/win32/Win32.chm', 'Win32.chm')
#
# start script
#
if __name__ == '__main__':
main()