aboutsummaryrefslogtreecommitdiff
path: root/wikiget/dl.py
diff options
context:
space:
mode:
authorclpo13 <clpo13@gmail.com>2023-09-26 12:09:07 -0700
committerGitHub <noreply@github.com>2023-09-26 12:09:07 -0700
commit602bbbb7e387f5c126d5130c6e7193f5ae906d9c (patch)
tree39d709c7117e05ed25cd4cc1ccd00cf15022448d /wikiget/dl.py
parent6d2acf3bba628f62fe91bb778b7bb92a1057969b (diff)
parentfbac39b1423475345b99c2da6a02be110c660d7a (diff)
downloadwikiget-602bbbb7e387f5c126d5130c6e7193f5ae906d9c.tar.gz
wikiget-602bbbb7e387f5c126d5130c6e7193f5ae906d9c.zip
Merge pull request #6 from clpo13/style-and-lint
Run linters with Hatch.
Diffstat (limited to 'wikiget/dl.py')
-rw-r--r--wikiget/dl.py99
1 files changed, 53 insertions, 46 deletions
diff --git a/wikiget/dl.py b/wikiget/dl.py
index f05061e..949f09e 100644
--- a/wikiget/dl.py
+++ b/wikiget/dl.py
@@ -23,7 +23,7 @@ from mwclient import APIError, InvalidResponse, LoginError, Site
from requests import ConnectionError, HTTPError
from tqdm import tqdm
-from wikiget import CHUNKSIZE, DEFAULT_SITE, USER_AGENT
+import wikiget
from wikiget.validations import valid_file, verify_hash
@@ -33,10 +33,9 @@ def download(dl, args):
if url.netloc:
filename = url.path
site_name = url.netloc
- if args.site is not DEFAULT_SITE and not args.quiet:
+ if args.site is not wikiget.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
@@ -56,30 +55,32 @@ def download(dl, args):
dest = args.output or filename
- if args.verbose >= 2:
- print(f'User agent: {USER_AGENT}')
+ if args.verbose >= wikiget.VERY_VERBOSE:
+ print(f"User agent: {wikiget.USER_AGENT}")
# connect to site and identify ourselves
- if args.verbose >= 1:
- print(f'Site name: {site_name}')
+ if args.verbose >= wikiget.STD_VERBOSE:
+ print(f"Site name: {site_name}")
try:
- site = Site(site_name, path=args.path, clients_useragent=USER_AGENT)
+ site = Site(site_name, path=args.path, clients_useragent=wikiget.USER_AGENT)
if args.username and args.password:
site.login(args.username, args.password)
except ConnectionError as e:
# usually this means there is no such site, or there's no network
# connection, though it could be a certificate problem
print("Error: couldn't connect to specified site.")
- if args.verbose >= 2:
- print('Full error message:')
+ if args.verbose >= wikiget.VERY_VERBOSE:
+ 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.")
- if args.verbose >= 2:
- print('Full error message:')
+ print(
+ "Error: couldn't find the specified wiki's api.php. "
+ "Check the value of --path."
+ )
+ if args.verbose >= wikiget.VERY_VERBOSE:
+ 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.')
- if args.verbose >= 2:
- print('Full error message:')
+ print(
+ "Error: access denied. Try providing credentials with "
+ "--username and --password."
+ )
+ if args.verbose >= wikiget.VERY_VERBOSE:
+ print("Full error message:")
for i in e.args:
print(i)
sys.exit(1)
@@ -106,59 +109,63 @@ 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']
-
- if args.verbose >= 1:
- print(f"Info: downloading '{filename}' "
- f"({file_size} bytes) from {site.host}",
- end='')
+ file_url = file.imageinfo["url"]
+ file_size = file.imageinfo["size"]
+ file_sha1 = file.imageinfo["sha1"]
+
+ if args.verbose >= wikiget.STD_VERBOSE:
+ 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')
- except IOError as e:
- print('File could not be written. '
- 'The following error was encountered:')
+ fd = open(dest, "wb")
+ except OSError as e:
+ print("File could not be written. The following error was encountered:")
print(e)
sys.exit(1)
else:
# download the file(s)
- if args.verbose >= 1:
+ if args.verbose >= wikiget.STD_VERBOSE:
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=wikiget.CHUNKSIZE,
+ ) as progress_bar:
with fd:
res = site.connection.get(file_url, stream=True)
progress_bar.set_postfix(file=dest, refresh=False)
- for chunk in res.iter_content(CHUNKSIZE):
+ for chunk in res.iter_content(wikiget.CHUNKSIZE):
fd.write(chunk)
progress_bar.update(len(chunk))
# verify file integrity and optionally print details
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}')
+ if args.verbose >= wikiget.STD_VERBOSE:
+ 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!')
+ if args.verbose >= wikiget.STD_VERBOSE:
+ 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: