Add visual progress bar.
This commit is contained in:
parent
5c12e76cf9
commit
e28ab0863c
21
macli.py
21
macli.py
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import progress_meter
|
||||||
import re
|
import re
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
@ -65,7 +66,6 @@ def get_extension(path):
|
|||||||
|
|
||||||
|
|
||||||
def request(url):
|
def request(url):
|
||||||
print("> {}".format(url))
|
|
||||||
req = urllib.request.Request(
|
req = urllib.request.Request(
|
||||||
url,
|
url,
|
||||||
data=None,
|
data=None,
|
||||||
@ -80,7 +80,7 @@ def show_error(e):
|
|||||||
print("\x1b[41;37m{}\x1b[0m")
|
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)
|
os.makedirs(directory, exist_ok=True)
|
||||||
for part in content.find_all(**selector):
|
for part in content.find_all(**selector):
|
||||||
if attribute not in part.attrs:
|
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],
|
href = urllib.parse.urljoin(base_url, part[attribute],
|
||||||
allow_fragments=False)
|
allow_fragments=False)
|
||||||
|
progbar.next_iter(href)
|
||||||
|
|
||||||
name = (hashlib.sha1(href.encode()).hexdigest()
|
name = (hashlib.sha1(href.encode()).hexdigest()
|
||||||
+ '.'
|
+ '.'
|
||||||
@ -123,6 +124,18 @@ def relink_links(content, base_url):
|
|||||||
link['href'] = full_href
|
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):
|
def archive_to_dir(directory, url):
|
||||||
current_path = os.getcwd()
|
current_path = os.getcwd()
|
||||||
os.chdir(directory)
|
os.chdir(directory)
|
||||||
@ -134,9 +147,11 @@ def archive_to_dir(directory, url):
|
|||||||
relink_links(archived_content, url)
|
relink_links(archived_content, url)
|
||||||
|
|
||||||
# Archive objects
|
# Archive objects
|
||||||
|
progbar = progress_meter.ProgressBar(get_num_elements(archived_content))
|
||||||
|
|
||||||
for (selector, directory, attribute) in OBJECT_TYPE_DESCRIPTORS:
|
for (selector, directory, attribute) in OBJECT_TYPE_DESCRIPTORS:
|
||||||
archive(archived_content, url,
|
archive(archived_content, url,
|
||||||
selector, directory, attribute)
|
selector, directory, attribute, progbar)
|
||||||
|
|
||||||
with open('index.html', 'wt') as f:
|
with open('index.html', 'wt') as f:
|
||||||
f.write(str(archived_content))
|
f.write(str(archived_content))
|
||||||
|
30
progress_meter.py
Normal file
30
progress_meter.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user