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
+34 -18
View File
@@ -7,37 +7,53 @@ import requests
import click import click
import pyperclip import pyperclip
from tqdm import tqdm
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
# Variables # Variables
URL_TRANSFERSH = 'https://transfer.sh' URL_TRANSFERSH = 'https://transfer.sh'
@click.command() @click.command()
@click.argument('filename') @click.argument('filename')
@click.option('-a', '--max-days', type=int, help='Maximum number of days to keep file') @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') @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): def transfersh_cli(filename, max_days, max_downloads):
""" Program that uploads a file to Transfer.sh """ """ Program that uploads a file to Transfer.sh """
download_url = ""
try: try:
# Open file # Open file
with open(filename, 'rb') as data: with tqdm(desc=filename,
click.echo('Uploading file') total=os.path.getsize(filename),
# Upload file unit='B',
conf_file = {filename: data} unit_scale=True,
headers = {} unit_divisor=1_024, ) as bar:
# Option to indicate the maximum number of days with open(filename, 'rb') as data:
if max_days is not None: # Upload file
headers['Max-Days'] = str(max_days) fields = {'file': (filename, data)}
# Option to indicate the maximum number of downloads headers = {}
if max_downloads is not None: # Option to indicate the maximum number of days
headers['Max-Downloads'] = str(max_downloads) if max_days is not None:
r = requests.post(URL_TRANSFERSH, files=conf_file, headers=headers) headers['Max-Days'] = str(max_days)
# Shows route to download # Option to indicate the maximum number of downloads
download_url = r.text if max_downloads is not None:
click.echo(f'Download from here: {download_url}') headers['Max-Downloads'] = str(max_downloads)
click.echo(f'It has also been copied to the clipboard!')
# Copy route to clipboard m = MultipartEncoderMonitor(
pyperclip.copy(download_url) 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: except Exception:
click.echo('Something has failed. The file could not be uploaded.') 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)
if __name__ == '__main__': if __name__ == '__main__':
transfersh_cli() transfersh_cli()