Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --start-idx=<n>, --end-idx=<m> option to enable ranged downloads #605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Patches and Suggestions
-----------------------

- VM Brasseur
- Russ Magee <[email protected]>
114 changes: 68 additions & 46 deletions internetarchive/cli/ia_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
-R, --retries=<retries> Set number of retries to <retries> [default: 5].
-I, --itemlist=<file> Download items from a specified file. Itemlists should
be a plain text file with one identifier per line.
-n, --start-idx=<n> Start immediately at item <n>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the long argument name to --start-index. The short from -n should probably be removed.

-m, --end-idx=<m> End download after item <m>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the long argument name to --end-index. The short from -m should probably be removed.

-S, --search=<query> Download items returned from a specified search query.
-P, --search-parameters=<key:value>... Download items returned from a specified search query.
-g, --glob=<pattern> Only download files whose filename matches the
Expand Down Expand Up @@ -110,6 +112,8 @@ def main(argv, session: ArchiveSession) -> None:
'--download-history': Use(bool),
'--parameters': Use(lambda x: get_args_dict(x, query_string=True)),
'--source': list,
'--start-idx': Use(lambda item: item[0] if item else None),
'--end-idx': Use(lambda item: item[0] if item else None),
'--exclude-source': list,
'--timeout': Or([], And(Use(lambda t: ast.literal_eval(t[0])), Or(int, float),
error=timeout_msg))
Expand All @@ -128,6 +132,18 @@ def main(argv, session: ArchiveSession) -> None:
print(f'{exc}\n{printable_usage(__doc__)}', file=sys.stderr)
sys.exit(1)

if args['--start-idx']:
start_idx = int(args['--start-idx'])-1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces around all operators int(args['--start-index']) - 1. Also use four spaces for indentation as used everywhere else in the code ...
And if it should no be clear from the argument remark, also use start_index for the variable name instead of using idx.

print(f'Starting download at collection item {start_idx+1}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, spaces.

else:
start_idx = 0

if args['--end-idx']:
end_idx = int(args['--end-idx'])
print(f'Ending download at specified item {end_idx}.')
else:
end_idx = None

retries = int(args['--retries'])
ids: list[File | str] | Search | TextIO

Expand Down Expand Up @@ -167,53 +183,59 @@ def main(argv, session: ArchiveSession) -> None:

errors = []
for i, identifier in enumerate(ids):
try:
identifier = identifier.strip()
except AttributeError:
identifier = identifier.get('identifier')
if total_ids > 1:
item_index = f'{i + 1}/{total_ids}'
if end_idx != None and end_idx == i:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None is a singleton. Use is not None for the comparison. In this case end_index == i would also suffice as condition.

break
if start_idx != None and i < start_idx:
pass
else:
item_index = None

try:
item = session.get_item(identifier)
except Exception as exc:
print(f'{identifier}: failed to retrieve item metadata - errors', file=sys.stderr)
raise
if 'You are attempting to make an HTTPS' in str(exc):
print(f'\n{exc}', file=sys.stderr)
sys.exit(1)
else:
continue

# Otherwise, download the entire item.
ignore_history_dir = True if not args['--download-history'] else False
_errors = item.download(
files=files,
formats=args['--format'],
glob_pattern=args['--glob'],
exclude_pattern=args['--exclude'],
dry_run=args['--dry-run'],
verbose=not args['--quiet'],
ignore_existing=args['--ignore-existing'],
checksum=args['--checksum'],
destdir=args['--destdir'],
no_directory=args['--no-directories'],
retries=retries,
item_index=item_index,
ignore_errors=True,
on_the_fly=args['--on-the-fly'],
no_change_timestamp=args['--no-change-timestamp'],
params=args['--parameters'],
ignore_history_dir=ignore_history_dir,
source=args['--source'],
exclude_source=args['--exclude-source'],
stdout=args['--stdout'],
timeout=args['--timeout'],
)
if _errors:
errors.append(_errors)
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation in this whole section is wrong.

identifier = identifier.strip()
except AttributeError:
identifier = identifier.get('identifier')
if total_ids > 1:
item_index = f'{i + 1}/{total_ids}'
else:
item_index = None

try:
item = session.get_item(identifier)
except Exception as exc:
print(f'{identifier}: failed to retrieve item metadata - errors', file=sys.stderr)
raise
if 'You are attempting to make an HTTPS' in str(exc):
print(f'\n{exc}', file=sys.stderr)
sys.exit(1)
else:
continue

# Otherwise, download the entire item.
ignore_history_dir = True if not args['--download-history'] else False
_errors = item.download(
files=files,
formats=args['--format'],
glob_pattern=args['--glob'],
exclude_pattern=args['--exclude'],
dry_run=args['--dry-run'],
verbose=not args['--quiet'],
ignore_existing=args['--ignore-existing'],
checksum=args['--checksum'],
destdir=args['--destdir'],
no_directory=args['--no-directories'],
retries=retries,
item_index=item_index,
ignore_errors=True,
on_the_fly=args['--on-the-fly'],
no_change_timestamp=args['--no-change-timestamp'],
params=args['--parameters'],
ignore_history_dir=ignore_history_dir,
source=args['--source'],
exclude_source=args['--exclude-source'],
stdout=args['--stdout'],
timeout=args['--timeout'],
)
if _errors:
errors.append(_errors)
##endif (start_idx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this superfluous line.

if errors:
# TODO: add option for a summary/report.
sys.exit(1)
Expand Down