diff options
Diffstat (limited to 'wikiget')
| -rw-r--r-- | wikiget/__init__.py | 9 | ||||
| -rw-r--r-- | wikiget/dl.py | 69 | ||||
| -rw-r--r-- | wikiget/validations.py | 6 | ||||
| -rw-r--r-- | wikiget/version.py | 2 | ||||
| -rw-r--r-- | wikiget/wikiget.py | 119 |
5 files changed, 116 insertions, 89 deletions
diff --git a/wikiget/__init__.py b/wikiget/__init__.py index 126f04d..93972fc 100644 --- a/wikiget/__init__.py +++ b/wikiget/__init__.py @@ -22,7 +22,8 @@ from wikiget.version import __version__ as wikiget_version # set some global constants 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)) +DEFAULT_SITE = "commons.wikimedia.org" +DEFAULT_PATH = "/w/" +USER_AGENT = "wikiget/{} (https://github.com/clpo13/wikiget) mwclient/{}".format( + wikiget_version, mwclient_version +) diff --git a/wikiget/dl.py b/wikiget/dl.py index f05061e..06115a3 100644 --- a/wikiget/dl.py +++ b/wikiget/dl.py @@ -35,8 +35,7 @@ def download(dl, args): site_name = url.netloc if args.site is not DEFAULT_SITE and not args.quiet: # this will work even if the user specifies 'commons.wikimedia.org' - print('Warning: target is a URL, ' - 'ignoring site specified with --site') + print("Warning: target is a URL, ignoring site specified with --site") else: filename = dl site_name = args.site @@ -57,11 +56,11 @@ def download(dl, args): dest = args.output or filename if args.verbose >= 2: - print(f'User agent: {USER_AGENT}') + print(f"User agent: {USER_AGENT}") # connect to site and identify ourselves if args.verbose >= 1: - print(f'Site name: {site_name}') + print(f"Site name: {site_name}") try: site = Site(site_name, path=args.path, clients_useragent=USER_AGENT) if args.username and args.password: @@ -71,15 +70,17 @@ def download(dl, args): # connection, though it could be a certificate problem print("Error: couldn't connect to specified site.") if args.verbose >= 2: - print('Full error message:') + print("Full error message:") print(e) sys.exit(1) except HTTPError as e: # most likely a 403 forbidden or 404 not found error for api.php - print("Error: couldn't find the specified wiki's api.php. " - "Check the value of --path.") + print( + "Error: couldn't find the specified wiki's api.php. " + "Check the value of --path." + ) if args.verbose >= 2: - print('Full error message:') + print("Full error message:") print(e) sys.exit(1) except (InvalidResponse, LoginError) as e: @@ -95,10 +96,12 @@ 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 - print('Error: access denied. Try providing credentials with ' - '--username and --password.') + print( + "Error: access denied. Try providing credentials with " + "--username and --password." + ) if args.verbose >= 2: - print('Full error message:') + print("Full error message:") for i in e.args: print(i) sys.exit(1) @@ -106,29 +109,29 @@ def download(dl, args): if file.imageinfo != {}: # file exists either locally or at a common repository, # like Wikimedia Commons - file_url = file.imageinfo['url'] - file_size = file.imageinfo['size'] - file_sha1 = file.imageinfo['sha1'] + file_url = file.imageinfo["url"] + file_size = file.imageinfo["size"] + file_sha1 = file.imageinfo["sha1"] if args.verbose >= 1: - print(f"Info: downloading '{filename}' " - f"({file_size} bytes) from {site.host}", - end='') + print( + f"Info: downloading '{filename}' " + f"({file_size} bytes) from {site.host}", + end="", + ) if args.output: print(f" to '{dest}'") else: - print('\n', end='') - print(f'Info: {file_url}') + print("\n", end="") + print(f"Info: {file_url}") if os.path.isfile(dest) and not args.force: - print(f"File '{dest}' already exists, skipping download " - "(use -f to ignore)") + print(f"File '{dest}' already exists, skipping download (use -f to ignore)") else: try: - fd = open(dest, 'wb') + fd = open(dest, "wb") except IOError as e: - print('File could not be written. ' - 'The following error was encountered:') + print("File could not be written. The following error was encountered:") print(e) sys.exit(1) else: @@ -137,9 +140,13 @@ def download(dl, args): leave_bars = True else: leave_bars = False - with tqdm(leave=leave_bars, total=file_size, - unit='B', unit_scale=True, - unit_divisor=CHUNKSIZE) as progress_bar: + with tqdm( + leave=leave_bars, + total=file_size, + unit="B", + unit_scale=True, + unit_divisor=CHUNKSIZE, + ) as progress_bar: with fd: res = site.connection.get(file_url, stream=True) progress_bar.set_postfix(file=dest, refresh=False) @@ -151,14 +158,14 @@ def download(dl, args): dl_sha1 = verify_hash(dest) if args.verbose >= 1: - print(f'Info: downloaded file SHA1 is {dl_sha1}') - print(f'Info: server file SHA1 is {file_sha1}') + print(f"Info: downloaded file SHA1 is {dl_sha1}") + print(f"Info: server file SHA1 is {file_sha1}") if dl_sha1 == file_sha1: if args.verbose >= 1: - print('Info: hashes match!') + print("Info: hashes match!") # at this point, we've successfully downloaded the file else: - print('Error: hash mismatch! Downloaded file may be corrupt.') + print("Error: hash mismatch! Downloaded file may be corrupt.") sys.exit(1) else: diff --git a/wikiget/validations.py b/wikiget/validations.py index 5e7213f..c0aa90d 100644 --- a/wikiget/validations.py +++ b/wikiget/validations.py @@ -31,7 +31,7 @@ def valid_file(search_string): """ # second group could also restrict to file extensions with three or more # letters with ([^/\r\n\t\f\v]+\.\w{3,}) - file_regex = re.compile(r'(File:|Image:)([^/\r\n\t\f\v]+\.\w+)$', re.I) + file_regex = re.compile(r"(File:|Image:)([^/\r\n\t\f\v]+\.\w+)$", re.I) return file_regex.search(search_string) @@ -44,7 +44,7 @@ def valid_site(search_string): :param search_string: string to validate :returns: a regex Match object if there's a match or None otherwise """ - site_regex = re.compile(r'wiki[mp]edia\.org$', re.I) + site_regex = re.compile(r"wiki[mp]edia\.org$", re.I) return site_regex.search(search_string) @@ -56,7 +56,7 @@ def verify_hash(filename): :return: hash digest """ hasher = hashlib.sha1() - with open(filename, 'rb') as dl: + with open(filename, "rb") as dl: buf = dl.read(BLOCKSIZE) while len(buf) > 0: hasher.update(buf) diff --git a/wikiget/version.py b/wikiget/version.py index 93b60a1..dd9b22c 100644 --- a/wikiget/version.py +++ b/wikiget/version.py @@ -1 +1 @@ -__version__ = '0.5.1' +__version__ = "0.5.1" diff --git a/wikiget/wikiget.py b/wikiget/wikiget.py index da60037..43e4d22 100644 --- a/wikiget/wikiget.py +++ b/wikiget/wikiget.py @@ -29,55 +29,75 @@ def main(): when installed with `pip install` or `python setup.py install`. """ - parser = argparse.ArgumentParser(description=""" - A tool for downloading files from - MediaWiki sites using the file name or - description page URL - """, - epilog=""" - Copyright (C) 2018-2021 Cody Logan - and contributors. - License GPLv3+: GNU GPL version 3 or later - <http://www.gnu.org/licenses/gpl.html>. - This is free software; you are free to - change and redistribute it under certain - conditions. There is NO WARRANTY, to the - extent permitted by law. - """) - parser.add_argument('FILE', help=""" - name of the file to download with the File: - prefix, or the URL of its file description page - """) - parser.add_argument('-V', '--version', action='version', - version=f'%(prog)s {wikiget_version}') + parser = argparse.ArgumentParser( + description=""" + A tool for downloading files from + MediaWiki sites using the file name or + description page URL + """, + epilog=""" + Copyright (C) 2018-2023 Cody Logan + and contributors. + License GPLv3+: GNU GPL version 3 or later + <http://www.gnu.org/licenses/gpl.html>. + This is free software; you are free to + change and redistribute it under certain + conditions. There is NO WARRANTY, to the + extent permitted by law. + """, + ) + parser.add_argument( + "FILE", + help=""" + name of the file to download with the File: + prefix, or the URL of its file description page + """, + ) + parser.add_argument( + "-V", "--version", action="version", version=f"%(prog)s {wikiget_version}" + ) message_options = parser.add_mutually_exclusive_group() - message_options.add_argument('-q', '--quiet', - help='suppress warning messages', - action='store_true') - message_options.add_argument('-v', '--verbose', - help='print detailed information; ' - 'use -vv for even more detail', - action='count', default=0) - parser.add_argument('-f', '--force', - help='force overwriting existing files', - action='store_true') - parser.add_argument('-s', '--site', default=DEFAULT_SITE, - help='MediaWiki site to download from ' - '(default: %(default)s)') - parser.add_argument('-p', '--path', default=DEFAULT_PATH, - help='MediaWiki site path, where api.php is located ' - '(default: %(default)s)') - parser.add_argument('--username', default='', - help='MediaWiki site username, for private wikis') - parser.add_argument('--password', default='', - help='MediaWiki site password, for private wikis') + message_options.add_argument( + "-q", "--quiet", help="suppress warning messages", action="store_true" + ) + message_options.add_argument( + "-v", + "--verbose", + help="print detailed information; use -vv for even more detail", + action="count", + default=0, + ) + parser.add_argument( + "-f", "--force", help="force overwriting existing files", action="store_true" + ) + parser.add_argument( + "-s", + "--site", + default=DEFAULT_SITE, + help="MediaWiki site to download from " "(default: %(default)s)", + ) + parser.add_argument( + "-p", + "--path", + default=DEFAULT_PATH, + help="MediaWiki site path, where api.php is located " "(default: %(default)s)", + ) + parser.add_argument( + "--username", default="", help="MediaWiki site username, for private wikis" + ) + parser.add_argument( + "--password", default="", help="MediaWiki site password, for private wikis" + ) output_options = parser.add_mutually_exclusive_group() - output_options.add_argument('-o', '--output', - help='write download to OUTPUT') - output_options.add_argument('-a', '--batch', - help='treat FILE as a textfile containing ' - 'multiple files to download, one URL or ' - 'filename per line', action='store_true') + output_options.add_argument("-o", "--output", help="write download to OUTPUT") + output_options.add_argument( + "-a", + "--batch", + help="treat FILE as a textfile containing " + "multiple files to download, one URL or " + "filename per line", + action="store_true", + ) args = parser.parse_args() @@ -93,10 +113,9 @@ def main(): if args.verbose >= 1: print(f"Info: using batch file '{input_file}'") try: - fd = open(input_file, 'r') + fd = open(input_file, "r") except IOError as e: - print('File could not be read. ' - 'The following error was encountered:') + print("File could not be read. " "The following error was encountered:") print(e) sys.exit(1) else: |
