From abe90a3090a90574c667a147d9ea0151fbd38a5c Mon Sep 17 00:00:00 2001 From: Mayank Gupta Date: Thu, 29 Apr 2021 16:59:57 +0200 Subject: [PATCH] update version v1.1.2 --- transfer/__main__.py | 12 ++++++++---- transfer/cli.py | 20 +++++++++++++++----- transfer/exceptions.py | 13 ++++++++++++- transfer/request.py | 6 +++++- transfer/version.py | 4 ++-- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/transfer/__main__.py b/transfer/__main__.py index b9d39fe..b133811 100644 --- a/transfer/__main__.py +++ b/transfer/__main__.py @@ -6,7 +6,7 @@ import requests, re from time import time from transfer.request import MakeRequest -from transfer.exceptions import FileTooLarge +from transfer.exceptions import FileTooLarge, PrepareError from random import choices from string import digits, ascii_uppercase, ascii_lowercase @@ -16,7 +16,7 @@ class Upload(MakeRequest): :param str file: name of the file of full location :param **kwargs: Optional arguments that :class:`MakeRequest ` class takes. ''' - def __init__(self, file:str, verbose:bool=False, cli:bool=False,**kwargs): + def __init__(self, file:str, verbose:bool=False, force:bool=False, cli:bool=False,**kwargs): super().__init__(file=file, **kwargs) start = time() @@ -24,7 +24,11 @@ def __init__(self, file:str, verbose:bool=False, cli:bool=False,**kwargs): if self.verbose: print("Preparing upload file...") - self.prepare() + if not force: + try: + self.prepare() + except: + raise PrepareError() response = self._send(**self.kwargs) @@ -60,7 +64,7 @@ def prepare(self): self._max_file_limit = int(nsize)*size[type] if self._max_file_limit < self._len: - FileTooLarge(self._max_file_limit) + raise FileTooLarge(self._max_file_limit) def _gen_hash(self) -> str: return "".join([ diff --git a/transfer/cli.py b/transfer/cli.py index 5027d4b..6485d23 100644 --- a/transfer/cli.py +++ b/transfer/cli.py @@ -18,6 +18,7 @@ from typing import List from json import loads, dumps from tempfile import gettempdir +from transfer.exceptions import FileTooLarge, PrepareError class ActionCheck(object): @@ -103,10 +104,17 @@ def __init__(self): self._remove(args) def _upload(self, namespace:argparse.Namespace) -> None: - namespace = vars(namespace) - namespace.__setitem__("file", namespace.pop("type").get("value")) - res = Upload(cli=True,**namespace) - self._add_log(res.data) + try: + namespace = vars(namespace) + namespace.__setitem__("file", namespace.pop("type").get("value")) + res = Upload(cli=True,**namespace) + self._add_log(res.data) + except FileTooLarge as e: + print(f"Error: {e.msg}") + + except PrepareError as e: + print(f"Error: {e.msg}") + def _remove(self, namespace:argparse.Namespace): try: @@ -161,7 +169,9 @@ def get_args(self) -> argparse.Namespace: c2.add_argument("-v", "--verbose", required=False, action="store_true", help="Enable Verbose mode for extra logs.") c2.add_argument("-np", "--no-process-bar", required=False, action="store_true", help="Disable process bar.") - c2.add_argument("-s", "--url", required=False, help="Third-party transfer.sh servers.") + c2.add_argument("-u", "--url", required=False, help="Third-party transfer.sh servers.") + c2.add_argument("-f", "--force", required=False,action="store_true", + help="Force to skip some verification tests and upload directly.") c3 = sub_parser.add_parser("list", aliases=["l"], help="List all uploaded files.") c3.add_argument("-s", "--show", action="store_const", required=True, const=dict(type="list"), dest="type") diff --git a/transfer/exceptions.py b/transfer/exceptions.py index 8912947..ed3ac3d 100644 --- a/transfer/exceptions.py +++ b/transfer/exceptions.py @@ -16,7 +16,6 @@ def __str__(self): class FileTooLarge(TransferError): - """Base video unavailable error.""" def __init__(self, limit: int): """ :param int limit: @@ -32,3 +31,15 @@ def error_message(self): @property def limit(self): return str(round(self._limit/(1024*1024*1024), 2))+'GB' if len(str(self._limit//(1024*1024))) >= 4 else str(round(self._limit/(1024*1024), 2))+'MB' + +class PrepareError(TransferError): + def __init__(self): + """ + :param int limit: + Max uploading size. + """ + super().__init__(self.error_message) + + @property + def error_message(self): + return f'Unable to fetch prepare data from site try --force.' \ No newline at end of file diff --git a/transfer/request.py b/transfer/request.py index 82d80b0..3254b14 100644 --- a/transfer/request.py +++ b/transfer/request.py @@ -87,10 +87,14 @@ def _progress_bar(self, size=None, progress=None, in_second=None, - loading_sign:str="█", + loading_sign:str="█", width:int=50, end="\r" ) -> None: + if not size: + size = 1 + progress = 1 + in_second = 0 percentage = (progress*width) // size print(f"Upload: |{loading_sign * percentage:{'-'}<{width}}| {percentage*(100//width):>3}%", f"{str(round(in_second/(1024*1024), 2))+'MB/s' if len(str(in_second//1024)) >= 4 else str(in_second//1024)+'Kb/s':>4}", diff --git a/transfer/version.py b/transfer/version.py index 14a064d..f842f49 100644 --- a/transfer/version.py +++ b/transfer/version.py @@ -1,5 +1,5 @@ -__version__ = "1.1.1" +__version__ = "1.1.2" if __name__ == '__main__': - print(__version__) \ No newline at end of file + print(__version__)