diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/wikiget/__init__.py | 5 | ||||
| -rw-r--r-- | src/wikiget/dl.py | 30 | ||||
| -rw-r--r-- | src/wikiget/wikiget.py | 29 |
3 files changed, 30 insertions, 34 deletions
diff --git a/src/wikiget/__init__.py b/src/wikiget/__init__.py index 20ea620..5b917cf 100644 --- a/src/wikiget/__init__.py +++ b/src/wikiget/__init__.py @@ -24,8 +24,9 @@ BLOCKSIZE = 65536 CHUNKSIZE = 1024 DEFAULT_SITE = "commons.wikimedia.org" DEFAULT_PATH = "/w/" -USER_AGENT = "wikiget/{} (https://github.com/clpo13/wikiget) mwclient/{}".format( - wikiget_version, mwclient_version +USER_AGENT = ( + f"wikiget/{wikiget_version} (https://github.com/clpo13/wikiget) " + f"mwclient/{mwclient_version}" ) STD_VERBOSE = 1 VERY_VERBOSE = 2 diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 9850ce8..791db61 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -36,8 +36,7 @@ def download(dl, args): site_name = url.netloc if args.site is not wikiget.DEFAULT_SITE: # this will work even if the user specifies 'commons.wikimedia.org' - logging.warning("target is a URL, " - "ignoring site specified with --site") + logging.warning("target is a URL, ignoring site specified with --site") else: filename = dl site_name = args.site @@ -74,8 +73,9 @@ def download(dl, args): sys.exit(1) except HTTPError as e: # most likely a 403 forbidden or 404 not found error for api.php - logging.error("Couldn't find the specified wiki's api.php. " - "Check the value of --path.") + logging.error( + "Couldn't find the specified wiki's api.php. Check the value of --path." + ) logging.debug("Full error message:") logging.debug(e) sys.exit(1) @@ -92,8 +92,10 @@ def download(dl, args): except APIError as e: # an API error at this point likely means access is denied, # which could happen with a private wiki - logging.error("Access denied. Try providing credentials with " - "--username and --password.") + logging.error( + "Access denied. Try providing credentials with " + "--username and --password." + ) logging.debug("Full error message:") for i in e.args: logging.debug(i) @@ -106,22 +108,23 @@ def download(dl, args): file_size = file.imageinfo["size"] file_sha1 = file.imageinfo["sha1"] - filename_log = (f"Downloading '{filename}' ({file_size} bytes) " - f"from {site.host}") + filename_log = f"Downloading '{filename}' ({file_size} bytes) from {site.host}" if args.output: filename_log += f" to '{dest}'" logging.info(filename_log) logging.info(f"{file_url}") if os.path.isfile(dest) and not args.force: - logging.warning(f"File '{dest}' already exists, skipping download " - "(use -f to ignore)") + logging.warning( + f"File '{dest}' already exists, skipping download (use -f to ignore)" + ) else: try: fd = open(dest, "wb") except OSError as e: - logging.error("File could not be written. " - "The following error was encountered:") + logging.error( + "File could not be written. The following error was encountered:" + ) logging.error(e) sys.exit(1) else: @@ -158,6 +161,5 @@ def download(dl, args): else: # no file information returned - logging.error(f"Target '{filename}' does not appear to be " - "a valid file.") + logging.error(f"Target '{filename}' does not appear to be a valid file.") sys.exit(1) diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py index b9a227f..bc6de38 100644 --- a/src/wikiget/wikiget.py +++ b/src/wikiget/wikiget.py @@ -102,10 +102,7 @@ def main(): action="store_true", ) parser.add_argument( - "-l", - "--logfile", - default="", - help="save log output to LOGFILE" + "-l", "--logfile", default="", help="save log output to LOGFILE" ) args = parser.parse_args() @@ -127,23 +124,18 @@ def main(): logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)-7s] %(message)s", - filename=args.logfile + filename=args.logfile, ) console = logging.StreamHandler() # TODO: even when loglevel is set to logging.DEBUG, # debug messages aren't printing to console console.setLevel(loglevel) - console.setFormatter( - logging.Formatter("[%(levelname)s] %(message)s") - ) + console.setFormatter(logging.Formatter("[%(levelname)s] %(message)s")) logging.getLogger("").addHandler(console) else: # log only to console - logging.basicConfig( - level=loglevel, - format="[%(levelname)s] %(message)s" - ) + logging.basicConfig(level=loglevel, format="[%(levelname)s] %(message)s") # log events are appended to the file if it already exists, # so note the start of a new download session @@ -158,10 +150,11 @@ def main(): logging.info(f"Using batch file '{input_file}'.") try: - fd = open(input_file, "r") + fd = open(input_file) except OSError as e: - logging.error("File could not be read. " - "The following error was encountered:") + logging.error( + "File could not be read. The following error was encountered:" + ) logging.error(e) sys.exit(1) else: @@ -173,11 +166,11 @@ def main(): # TODO: validate file contents before download process starts for line_num, url in enumerate(dl_list, start=1): - url = url.strip() + s_url = url.strip() # keep track of batch file line numbers for # debugging/logging purposes - logging.info(f"Downloading '{url}' at line {line_num}:") - download(url, args) + logging.info(f"Downloading '{s_url}' at line {line_num}:") + download(s_url, args) else: # single download mode dl = args.FILE |
