From 4d6814542e78a3ef6e764df160519e9fd2dee9c0 Mon Sep 17 00:00:00 2001 From: mirusu400 Date: Fri, 22 Oct 2021 10:22:45 +0900 Subject: [PATCH 1/3] Add tqdm module --- transferwee.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/transferwee.py b/transferwee.py index 0a6da15..f86cb0e 100755 --- a/transferwee.py +++ b/transferwee.py @@ -39,6 +39,7 @@ """ from typing import List +from tqdm import tqdm import os.path import re import urllib.parse @@ -134,9 +135,19 @@ def download(url: str, file: str = '') -> None: file = _file_unquote(urllib.parse.urlparse(dl_url).path.split('/')[-1]) r = requests.get(dl_url, stream=True) + r.raise_for_status() + length = int(r.headers.get('content-length')) with open(file, 'wb') as f: - for chunk in r.iter_content(chunk_size=1024): - f.write(chunk) + if r is None: # no content length header + f.write(r.content) + else: + dl = 0 + progress_bar = tqdm(total=length, unit='iB', unit_scale=True) + for data in r.iter_content(chunk_size=1024): + dl += len(data) + f.write(data) + progress_bar.update(len(data)) + progress_bar.close() def _file_name_and_size(file: str) -> dict: From 4eedaaebfca5df3312f8bfe57ed42e77c3173122 Mon Sep 17 00:00:00 2001 From: mirusu400 Date: Sat, 22 Jan 2022 13:04:41 +0900 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Leonardo Taccari --- transferwee.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transferwee.py b/transferwee.py index f86cb0e..fcf1a12 100755 --- a/transferwee.py +++ b/transferwee.py @@ -139,7 +139,7 @@ def download(url: str, file: str = '') -> None: length = int(r.headers.get('content-length')) with open(file, 'wb') as f: if r is None: # no content length header - f.write(r.content) + f.write(r.content) else: dl = 0 progress_bar = tqdm(total=length, unit='iB', unit_scale=True) From f6329af255ffffdd3cc2c05b697e3b7f721fe69e Mon Sep 17 00:00:00 2001 From: mirusu400 Date: Sat, 22 Jan 2022 13:18:22 +0900 Subject: [PATCH 3/3] Fix some reviewed part, change README --- README.md | 1 + transferwee.py | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b1ea6be..f3e08a4 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ positional arguments: optional arguments: -h, --help show this help message and exit -g only print the direct link (without downloading it) + -s show the status of the downloading progress (need tqdm module) -o file output file to be used ``` diff --git a/transferwee.py b/transferwee.py index fcf1a12..00632df 100755 --- a/transferwee.py +++ b/transferwee.py @@ -39,12 +39,10 @@ """ from typing import List -from tqdm import tqdm import os.path import re import urllib.parse import zlib - import requests @@ -122,7 +120,7 @@ def _file_unquote(file: str) -> str: return urllib.parse.unquote(file).replace('../', '').replace('/', '').replace('\\', '') -def download(url: str, file: str = '') -> None: +def download(url: str, file: str = '', use_progress_bar: bool = False) -> None: """Given a `we.tl/' or `wetransfer.com/downloads/' download it. First a direct link is retrieved (via download_url()), the filename can be @@ -133,21 +131,21 @@ def download(url: str, file: str = '') -> None: dl_url = download_url(url) if not file: file = _file_unquote(urllib.parse.urlparse(dl_url).path.split('/')[-1]) - + r = requests.get(dl_url, stream=True) - r.raise_for_status() - length = int(r.headers.get('content-length')) with open(file, 'wb') as f: - if r is None: # no content length header - f.write(r.content) - else: + if use_progress_bar: + from tqdm import tqdm dl = 0 - progress_bar = tqdm(total=length, unit='iB', unit_scale=True) + progress_bar = tqdm(total = int(r.headers.get('content-length')), unit = 'iB', unit_scale = True) for data in r.iter_content(chunk_size=1024): dl += len(data) f.write(data) progress_bar.update(len(data)) progress_bar.close() + else: + for chunk in r.iter_content(chunk_size=1024): + f.write(chunk) def _file_name_and_size(file: str) -> dict: @@ -365,6 +363,8 @@ def upload(files: List[str], message: str = '', sender: str = None, dp = sp.add_parser('download', help='download files') dp.add_argument('-g', action='store_true', help='only print the direct link (without downloading it)') + dp.add_argument('-s', action='store_true', + help='show the status of the downloading progress (need tqdm module)') dp.add_argument('-o', type=str, default='', metavar='file', help='output file to be used') dp.add_argument('url', nargs='+', type=str, metavar='url', @@ -381,14 +381,13 @@ def upload(files: List[str], message: str = '', sender: str = None, help='files to upload') args = ap.parse_args() - if args.action == 'download': if args.g: for u in args.url: print(download_url(u)) else: for u in args.url: - download(u, args.o) + download(u, args.o, args.s) exit(0) if args.action == 'upload':