Add progress bar #9

Merged
AleksaMCode merged 1 commits from feat/progress-bar into master 2023-10-12 16:05:02 +02:00
Showing only changes of commit d121b5d749 - Show all commits
+22 -6
View File
@@ -7,21 +7,30 @@ import requests
import click
import pyperclip
from tqdm import tqdm
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
# Variables
URL_TRANSFERSH = 'https://transfer.sh'
@click.command()
@click.argument('filename')
@click.option('-a', '--max-days', type=int, help='Maximum number of days to keep file')
@click.option('-d', '--max-downloads', type=int, help='Maximum number of times that file can be downloaded')
def transfersh_cli(filename, max_days, max_downloads):
""" Program that uploads a file to Transfer.sh """
download_url = ""
try:
# Open file
with tqdm(desc=filename,
total=os.path.getsize(filename),
unit='B',
unit_scale=True,
unit_divisor=1_024, ) as bar:
with open(filename, 'rb') as data:
click.echo('Uploading file')
# Upload file
conf_file = {filename: data}
fields = {'file': (filename, data)}
headers = {}
# Option to indicate the maximum number of days
if max_days is not None:
@@ -29,15 +38,22 @@ def transfersh_cli(filename, max_days, max_downloads):
# Option to indicate the maximum number of downloads
if max_downloads is not None:
headers['Max-Downloads'] = str(max_downloads)
r = requests.post(URL_TRANSFERSH, files=conf_file, headers=headers)
# Shows route to download
m = MultipartEncoderMonitor(
MultipartEncoder(fields=fields), lambda monitor: bar.update(monitor.bytes_read - bar.n)
)
headers['Content-Type'] = m.content_type
r = requests.post(URL_TRANSFERSH, data=m, headers=headers)
download_url = r.text
except Exception:
click.echo('Something has failed. The file could not be uploaded.')
# Shows route to download
click.echo(f'Download from here: {download_url}')
click.echo(f'It has also been copied to the clipboard!')
# Copy route to clipboard
pyperclip.copy(download_url)
except Exception:
click.echo('Something has failed. The file could not be uploaded.')
if __name__ == '__main__':
transfersh_cli()