Add visual progress bar.

This commit is contained in:
kenkeiras 2017-07-05 01:21:38 +02:00
parent 5c12e76cf9
commit e28ab0863c
2 changed files with 48 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3
import progress_meter
import re
import hashlib
import os
@ -65,7 +66,6 @@ def get_extension(path):
def request(url):
print("> {}".format(url))
req = urllib.request.Request(
url,
data=None,
@ -80,7 +80,7 @@ def show_error(e):
print("\x1b[41;37m{}\x1b[0m")
def archive(content, base_url, selector, directory, attribute):
def archive(content, base_url, selector, directory, attribute, progbar):
os.makedirs(directory, exist_ok=True)
for part in content.find_all(**selector):
if attribute not in part.attrs:
@ -88,6 +88,7 @@ def archive(content, base_url, selector, directory, attribute):
href = urllib.parse.urljoin(base_url, part[attribute],
allow_fragments=False)
progbar.next_iter(href)
name = (hashlib.sha1(href.encode()).hexdigest()
+ '.'
@ -123,6 +124,18 @@ def relink_links(content, base_url):
link['href'] = full_href
def get_num_elements(content):
count = 0
for (selector, _, attribute) in OBJECT_TYPE_DESCRIPTORS:
count += len([True
for element
in content.find_all(**selector)
if attribute in element.attrs])
return count
def archive_to_dir(directory, url):
current_path = os.getcwd()
os.chdir(directory)
@ -134,9 +147,11 @@ def archive_to_dir(directory, url):
relink_links(archived_content, url)
# Archive objects
progbar = progress_meter.ProgressBar(get_num_elements(archived_content))
for (selector, directory, attribute) in OBJECT_TYPE_DESCRIPTORS:
archive(archived_content, url,
selector, directory, attribute)
selector, directory, attribute, progbar)
with open('index.html', 'wt') as f:
f.write(str(archived_content))

30
progress_meter.py Normal file
View File

@ -0,0 +1,30 @@
import sys
class ProgressBar:
def __init__(self, num_elements):
self.num_elements = num_elements
self.current_element = 0
def show(self, msg=''):
total_blocks = 10
blocks_done = ((self.current_element * total_blocks)
// self.num_elements)
blocks_to_go = total_blocks - blocks_done
print('\r' # Go to the start of the line
'\x1b[K' # Clear line
'\x1b[0m' # Restart the "style"
'\x1b[7l' # Disable line wrapping
'|' # Put the first "|"
+ blocks_done * '' # Completed blocks
+ blocks_to_go * ' ' # Uncompleted blocks
+ '\x1b[7m|\x1b[0m' # End the bar
+ ' '
+ msg # Add message
+ '\x1b[7h' # Enable line wrapping
+ '\r', # Go back to the start
end='')
def next_iter(self, msg=''):
self.current_element += 1
self.show(msg)