diff options
Diffstat (limited to 'src/util.py')
-rw-r--r-- | src/util.py | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/src/util.py b/src/util.py index 99bb82e..cef5529 100644 --- a/src/util.py +++ b/src/util.py @@ -8,8 +8,11 @@ import hashlib DEFAULT_BAR_COLOR = colors.BLACK + colors.BG_CYAN DEFAULT_BAR_COLOR_RESET = colors.BG_BLACK + colors.CYAN -def add_path(a, b): - return a + (b if a[-1] == "/" else f"/{b}") +def add_path(*argv): + a = argv[0] + for b in argv[1:]: + a = a + (b if a[-1] == "/" else f"/{b}") + return a def loading_bar(completed, total, text, unit="", color=DEFAULT_BAR_COLOR, reset=DEFAULT_BAR_COLOR_RESET): @@ -29,19 +32,57 @@ def loading_bar(completed, total, text, def print_reset(text): print(colors.RESET + text) -def curl(url): +def curl(url, raw=False): try: r = requests.get(url) except: return 500, "" - return r.status_code, r.text + return r.status_code, r.content if raw else r.text + +def get_unit(n): + base = 1000 + if n > base**4: return base**4, "TB" + elif n > base**3: return base**3, "GB" + elif n > base**2: return base**2, "MB" + elif n > base**1: return base**1, "KB" + +def curl_to_file(url, path, text=""): + with requests.get(url, stream=True) as r: + r.raise_for_status() + length = int(r.headers['content-length']) + with open(path, "wb") as f: + + c_size = 4096 + ic = r.iter_content(chunk_size=c_size) + done = 0 + + for chunk in ic: + if text: + divisor, unit = get_unit(length) + loading_bar(int(done/divisor), int(length/divisor), "Downloading " + text, unit=unit) + + f.write(chunk) + done += c_size + if text: + divisor, unit = get_unit(length) + loading_bar(int(done/divisor), int(length/divisor), "Downloaded " + text, unit=unit) + print(colors.RESET) + + return r.status_code, path + def mkdir(path): if not os.path.exists(path): os.makedirs(path) -def md5sum(data): - return hashlib.md5(data) +def md5sum(filename): + md5_hash = hashlib.md5() + + with open(filename,"rb") as f: + for byte_block in iter(lambda: f.read(4096),b""): + md5_hash.update(byte_block) + + return md5_hash.hexdigest() def ask_confirmation(text, default=True, no_confirm=False): yes = "Y" if default else "y" |