Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tqdm module #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
24 changes: 17 additions & 7 deletions transferwee.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import re
import urllib.parse
import zlib

import requests


Expand Down Expand Up @@ -121,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
Expand All @@ -132,11 +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)
with open(file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
if use_progress_bar:
from tqdm import tqdm
dl = 0
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:
Expand Down Expand Up @@ -354,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',
Expand All @@ -370,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':
Expand Down