1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
|
#
# The internetarchive module is a Python/CLI interface to Archive.org.
#
# Copyright (C) 2012-2024 Internet Archive
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
internetarchive.api
~~~~~~~~~~~~~~~~~~~
This module implements the Internetarchive API.
:copyright: (C) 2012-2024 by Internet Archive.
:license: AGPL 3, see LICENSE for more details.
"""
from __future__ import annotations
from getpass import getpass
from typing import Iterable, Mapping, MutableMapping
import requests
from urllib3 import Retry
from internetarchive import auth, catalog, files, item, search, session
from internetarchive import config as config_module
from internetarchive.exceptions import AuthenticationError
def get_session(
config: Mapping | None = None,
config_file: str | None = None,
debug: bool = False,
http_adapter_kwargs: MutableMapping | None = None,
) -> session.ArchiveSession:
"""Return a new :class:`ArchiveSession` object. The :class:`ArchiveSession`
object is the main interface to the ``internetarchive`` lib. It allows you to
persist certain parameters across tasks.
:param config: A dictionary used to configure your session.
:param config_file: A path to a config file used to configure your session.
:param debug: To be passed on to this session's method calls.
:param http_adapter_kwargs: Keyword arguments that
:py:class:`requests.adapters.HTTPAdapter` takes.
:returns: To persist certain parameters across tasks.
Usage:
>>> from internetarchive import get_session
>>> config = {'s3': {'access': 'foo', 'secret': 'bar'}}
>>> s = get_session(config)
>>> s.access_key
'foo'
From the session object, you can access all of the functionality of the
``internetarchive`` lib:
>>> item = s.get_item('nasa')
>>> item.download()
nasa: ddddddd - success
>>> s.get_tasks(task_ids=31643513)[0].server
'ia311234'
"""
return session.ArchiveSession(config, config_file or "", debug, http_adapter_kwargs)
def get_item(
identifier: str,
config: Mapping | None = None,
config_file: str | None = None,
archive_session: session.ArchiveSession | None = None,
debug: bool = False,
http_adapter_kwargs: MutableMapping | None = None,
request_kwargs: MutableMapping | None = None,
) -> item.Item:
"""Get an :class:`Item` object.
:param identifier: The globally unique Archive.org item identifier.
:param config: A dictionary used to configure your session.
:param config_file: A path to a config file used to configure your session.
:param archive_session: An :class:`ArchiveSession` object can be provided
via the ``archive_session`` parameter.
:param debug: To be passed on to get_session().
:param http_adapter_kwargs: Keyword arguments that
:py:class:`requests.adapters.HTTPAdapter` takes.
:param request_kwargs: Keyword arguments that
:py:class:`requests.Request` takes.
:returns: The Item that fits the criteria.
Usage:
>>> from internetarchive import get_item
>>> item = get_item('nasa')
>>> item.item_size
121084
"""
if not archive_session:
archive_session = get_session(config, config_file, debug, http_adapter_kwargs)
return archive_session.get_item(identifier, request_kwargs=request_kwargs)
def get_files(
identifier: str,
files: files.File | list[files.File] | None = None,
formats: str | list[str] | None = None,
glob_pattern: str | None = None,
exclude_pattern: str | None = None,
on_the_fly: bool = False,
**get_item_kwargs,
) -> list[files.File]:
r"""Get :class:`File` objects from an item.
:param identifier: The globally unique Archive.org identifier for a given item.
:param files: Only return files matching the given filenames.
:param formats: Only return files matching the given formats.
:param glob_pattern: Only return files matching the given glob pattern.
:param exclude_pattern: Exclude files matching the given glob pattern.
:param on_the_fly: Include on-the-fly files (i.e. derivative EPUB,
MOBI, DAISY files).
:param \*\*get_item_kwargs: Arguments that ``get_item()`` takes.
:returns: Files from an item.
Usage:
>>> from internetarchive import get_files
>>> fnames = [f.name for f in get_files('nasa', glob_pattern='*xml')]
>>> print(fnames)
['nasa_reviews.xml', 'nasa_meta.xml', 'nasa_files.xml']
"""
item = get_item(identifier, **get_item_kwargs)
return item.get_files(files, formats, glob_pattern, exclude_pattern, on_the_fly)
def modify_metadata(
identifier: str,
metadata: Mapping,
target: str | None = None,
append: bool = False,
append_list: bool = False,
priority: int = 0,
access_key: str | None = None,
secret_key: str | None = None,
debug: bool = False,
request_kwargs: Mapping | None = None,
**get_item_kwargs,
) -> requests.Request | requests.Response:
r"""Modify the metadata of an existing item on Archive.org.
:param identifier: The globally unique Archive.org identifier for a given item.
:param metadata: Metadata used to update the item.
:param target: The metadata target to update. Defaults to `metadata`.
:param append: set to True to append metadata values to current values
rather than replacing. Defaults to ``False``.
:param append_list: Append values to an existing multi-value
metadata field. No duplicate values will be added.
:param priority: Set task priority.
:param access_key: IA-S3 access_key to use when making the given request.
:param secret_key: IA-S3 secret_key to use when making the given request.
:param debug: set to True to return a :class:`requests.Request <Request>`
object instead of sending request. Defaults to ``False``.
:param \*\*get_item_kwargs: Arguments that ``get_item`` takes.
:returns: A Request if debug else a Response.
"""
item = get_item(identifier, **get_item_kwargs)
return item.modify_metadata(
metadata,
target=target,
append=append,
append_list=append_list,
priority=priority,
access_key=access_key,
secret_key=secret_key,
debug=debug,
request_kwargs=request_kwargs,
refresh=False
)
def upload(
identifier: str,
files,
metadata: Mapping | None = None,
headers: dict | None = None,
access_key: str | None = None,
secret_key: str | None = None,
queue_derive=None,
verbose: bool = False,
verify: bool = False,
checksum: bool = False,
delete: bool = False,
retries: int | None = None,
retries_sleep: int | None = None,
debug: bool = False,
validate_identifier: bool = False,
request_kwargs: dict | None = None,
**get_item_kwargs,
) -> list[requests.Request | requests.Response]:
r"""Upload files to an item. The item will be created if it does not exist.
:param identifier: The globally unique Archive.org identifier for a given item.
:param files: The filepaths or file-like objects to upload. This value can be an
iterable or a single file-like object or string.
:param metadata: Metadata used to create a new item. If the item already
exists, the metadata will not be updated -- use ``modify_metadata``.
:param headers: Add additional HTTP headers to the request.
:param access_key: IA-S3 access_key to use when making the given request.
:param secret_key: IA-S3 secret_key to use when making the given request.
:param queue_derive: Set to False to prevent an item from being derived
after upload.
:param verbose: Display upload progress.
:param verify: Verify local MD5 checksum matches the MD5 checksum of the
file received by IAS3.
:param checksum: Skip uploading files based on checksum.
:param delete: Delete local file after the upload has been successfully
verified.
:param retries: Number of times to retry the given request if S3 returns a
503 SlowDown error.
:param retries_sleep: Amount of time to sleep between ``retries``.
:param debug: Set to True to print headers to stdout, and exit without
sending the upload request.
:param validate_identifier: Set to True to validate the identifier before
uploading the file.
:param \*\*kwargs: Optional arguments that ``get_item`` takes.
:returns: A list Requests if debug else a list of Responses.
"""
item = get_item(identifier, **get_item_kwargs)
return item.upload(
files,
metadata=metadata,
headers=headers,
access_key=access_key,
secret_key=secret_key,
queue_derive=queue_derive,
verbose=verbose,
verify=verify,
checksum=checksum,
delete=delete,
retries=retries,
retries_sleep=retries_sleep,
debug=debug,
validate_identifier=validate_identifier,
request_kwargs=request_kwargs,
)
def download(
identifier: str,
files: files.File | list[files.File] | None = None,
formats: str | list[str] | None = None,
glob_pattern: str | None = None,
dry_run: bool = False,
verbose: bool = False,
ignore_existing: bool = False,
checksum: bool = False,
checksum_archive: bool = False,
destdir: str | None = None,
no_directory: bool = False,
retries: int | None = None,
item_index: int | None = None,
ignore_errors: bool = False,
on_the_fly: bool = False,
return_responses: bool = False,
no_change_timestamp: bool = False,
timeout: float | tuple[int, float] | None = None,
**get_item_kwargs,
) -> list[requests.Request | requests.Response]:
r"""Download files from an item.
:param identifier: The globally unique Archive.org identifier for a given item.
:param files: Only return files matching the given file names.
:param formats: Only return files matching the given formats.
:param glob_pattern: Only return files matching the given glob pattern.
:param dry_run: Print URLs to files to stdout rather than downloading
them.
:param verbose: Turn on verbose output.
:param ignore_existing: Skip files that already exist locally.
:param checksum: Skip downloading file based on checksum.
:param checksum_archive: Skip downloading file based on checksum, and skip
checksum validation if it already succeeded
(will create and use _checksum_archive.txt).
:param destdir: The directory to download files to.
:param no_directory: Download files to current working
directory rather than creating an item directory.
:param retries: The number of times to retry on failed
requests.
:param item_index: The index of the item for displaying
progress in bulk downloads.
:param ignore_errors: Don't fail if a single file fails to
download, continue to download other files.
:param on_the_fly: Download on-the-fly files (i.e. derivative EPUB,
MOBI, DAISY files).
:param return_responses: Rather than downloading files to disk, return
a list of response objects.
:param \*\*kwargs: Optional arguments that ``get_item`` takes.
:returns: A list Requests if debug else a list of Responses.
"""
item = get_item(identifier, **get_item_kwargs)
r = item.download(
files=files,
formats=formats,
glob_pattern=glob_pattern,
dry_run=dry_run,
verbose=verbose,
ignore_existing=ignore_existing,
checksum=checksum,
checksum_archive=checksum_archive,
destdir=destdir,
no_directory=no_directory,
retries=retries,
item_index=item_index,
ignore_errors=ignore_errors,
on_the_fly=on_the_fly,
return_responses=return_responses,
no_change_timestamp=no_change_timestamp,
timeout=timeout,
)
return r
def delete(
identifier: str,
files: files.File | list[files.File] | None = None,
formats: str | list[str] | None = None,
glob_pattern: str | None = None,
cascade_delete: bool = False,
access_key: str | None = None,
secret_key: str | None = None,
verbose: bool = False,
debug: bool = False,
**kwargs,
) -> list[requests.Request | requests.Response]:
"""Delete files from an item. Note: Some system files, such as <itemname>_meta.xml,
cannot be deleted.
:param identifier: The globally unique Archive.org identifier for a given item.
:param files: Only return files matching the given filenames.
:param formats: Only return files matching the given formats.
:param glob_pattern: Only return files matching the given glob pattern.
:param cascade_delete: Delete all files associated with the specified file,
including upstream derivatives and the original.
:param access_key: IA-S3 access_key to use when making the given request.
:param secret_key: IA-S3 secret_key to use when making the given request.
:param verbose: Print actions to stdout.
:param debug: Set to True to print headers to stdout and exit exit without
sending the delete request.
:returns: A list Requests if debug else a list of Responses
"""
_files = get_files(identifier, files, formats, glob_pattern, **kwargs)
responses = []
for f in _files:
r = f.delete(
cascade_delete=cascade_delete,
access_key=access_key,
secret_key=secret_key,
verbose=verbose,
debug=debug,
)
responses.append(r)
return responses
def get_tasks(
identifier: str = "",
params: dict | None = None,
config: Mapping | None = None,
config_file: str | None = None,
archive_session: session.ArchiveSession | None = None,
http_adapter_kwargs: MutableMapping | None = None,
request_kwargs: MutableMapping | None = None,
) -> set[catalog.CatalogTask]:
"""Get tasks from the Archive.org catalog.
:param identifier: The Archive.org identifier for which to retrieve tasks for.
:param params: The URL parameters to send with each request sent to the
Archive.org catalog API.
:returns: A set of :class:`CatalogTask` objects.
"""
if not archive_session:
archive_session = get_session(config, config_file, False, http_adapter_kwargs)
return archive_session.get_tasks(
identifier=identifier, params=params, request_kwargs=request_kwargs
)
def search_items(
query: str,
fields: Iterable | None = None,
sorts=None,
params: Mapping | None = None,
full_text_search: bool = False,
dsl_fts: bool = False,
archive_session: session.ArchiveSession | None = None,
config: Mapping | None = None,
config_file: str | None = None,
http_adapter_kwargs: MutableMapping | None = None,
request_kwargs: Mapping | None = None,
max_retries: int | Retry | None = None,
) -> search.Search:
"""Search for items on Archive.org.
:param query: The Archive.org search query to yield results for. Refer to
https://archive.org/advancedsearch.php#raw for help formatting your
query.
:param fields: The metadata fields to return in the search results.
:param params: The URL parameters to send with each request sent to the
Archive.org Advancedsearch Api.
:param full_text_search: Beta support for querying the archive.org
Full Text Search API [default: False].
:param dsl_fts: Beta support for querying the archive.org Full Text
Search API in dsl (i.e. do not prepend ``!L `` to the
``full_text_search`` query [default: False].
:param secure: Configuration options for session.
:param config_file: A path to a config file used to configure your session.
:param http_adapter_kwargs: Keyword arguments that
:py:class:`requests.adapters.HTTPAdapter` takes.
:param request_kwargs: Keyword arguments that
:py:class:`requests.Request` takes.
:param max_retries: The number of times to retry a failed request.
This can also be an `urllib3.Retry` object.
If you need more control (e.g. `status_forcelist`), use a
`ArchiveSession` object, and mount your own adapter after the
session object has been initialized. For example::
>>> s = get_session()
>>> s.mount_http_adapter()
>>> search_results = s.search_items('nasa')
See :meth:`ArchiveSession.mount_http_adapter`
for more details.
:returns: A :class:`Search` object, yielding search results.
"""
if not archive_session:
archive_session = get_session(config, config_file, False, http_adapter_kwargs)
return archive_session.search_items(
query,
fields=fields,
sorts=sorts,
params=params,
full_text_search=full_text_search,
dsl_fts=dsl_fts,
request_kwargs=request_kwargs,
max_retries=max_retries,
)
def configure( # nosec: hardcoded_password_default
username: str = "",
password: str = "",
config_file: str = "",
host: str = "archive.org",
) -> str:
"""Configure internetarchive with your Archive.org credentials.
:param username: The email address associated with your Archive.org account.
:param password: Your Archive.org password.
:returns: The config file path.
Usage:
>>> from internetarchive import configure
>>> configure('user@example.com', 'password')
"""
auth_config = config_module.get_auth_config(
username or input("Email address: "),
password or getpass("Password: "),
host,
)
config_file_path = config_module.write_config_file(auth_config, config_file)
return config_file_path
def get_username(access_key: str, secret_key: str) -> str:
"""Returns an Archive.org username given an IA-S3 key pair.
:param access_key: IA-S3 access_key to use when making the given request.
:param secret_key: IA-S3 secret_key to use when making the given request.
:returns: The username.
"""
j = get_user_info(access_key, secret_key)
return j.get("username", "")
def get_user_info(access_key: str, secret_key: str) -> dict[str, str]:
"""Returns details about an Archive.org user given an IA-S3 key pair.
:param access_key: IA-S3 access_key to use when making the given request.
:param secret_key: IA-S3 secret_key to use when making the given request.
:returns: Archive.org use info.
"""
u = "https://s3.us.archive.org"
p = {"check_auth": 1}
r = requests.get(u, params=p, auth=auth.S3Auth(access_key, secret_key), timeout=10)
r.raise_for_status()
j = r.json()
if j.get("error"):
raise AuthenticationError(j.get("error"))
else:
return j
|