api ¤
Aria2 API.
This module defines the API class, which makes use of a JSON-RPC client to provide higher-level methods to interact easily with a remote aria2c process.
Classes:
-
API
–A class providing high-level methods to interact with a remote aria2c process.
API ¤
API(client: Client | None = None)
A class providing high-level methods to interact with a remote aria2c process.
This class is instantiated with a reference to a Client
instance. It then uses this client to call remote procedures, or remote methods. While the client methods reflect exactly what aria2c is providing through JSON-RPC, this class's methods allow for easier / faster control of the remote process. It also wraps the information the client retrieves in Python object, like Download
, allowing for even more Pythonic interactions, without worrying about payloads, responses, JSON, etc..
Parameters:
-
client
(Client | None
, default:None
) –An instance of the aria2p.client.Client class.
Methods:
-
add
–Add a download (guess its type).
-
add_magnet
–Add a download with a Magnet URI.
-
add_metalink
–Add a download with a Metalink file.
-
add_torrent
–Add a download with a torrent file (usually .torrent extension).
-
add_uris
–Add a download with a URL (or more).
-
autopurge
–Purge completed, removed or failed downloads from the queue.
-
copy_files
–Copy downloaded files to another directory.
-
get_download
–Get a
Download
object thanks to its GID. -
get_downloads
–Get a list of
Download
object thanks to their GIDs. -
get_global_options
–Get the global options.
-
get_options
–Get options for each of the given downloads.
-
get_stats
–Get the stats of the remote aria2c process.
-
listen_to_notifications
–Start listening to aria2 notifications via WebSocket.
-
move
–Move a download in the queue, relatively to its current position.
-
move_down
–Move a download down in the queue.
-
move_files
–Move downloaded files to another directory.
-
move_to
–Move a download in the queue, with absolute positioning.
-
move_to_bottom
–Move a download to the bottom of the queue.
-
move_to_top
–Move a download to the top of the queue.
-
move_up
–Move a download up in the queue.
-
parse_input_file
–Parse a file with URIs or an aria2c input file.
-
pause
–Pause the given (active) downloads.
-
pause_all
–Pause all (active) downloads.
-
purge
–Purge completed, removed or failed downloads from the queue.
-
remove
–Remove the given downloads from the list.
-
remove_all
–Remove all downloads from the list.
-
remove_files
–Remove downloaded files.
-
resume
–Resume (unpause) the given downloads.
-
resume_all
–Resume (unpause) all downloads.
-
retry_downloads
–Resume failed downloads from where they left off with new GIDs.
-
search
–Not implemented.
-
set_global_options
–Set global options.
-
set_options
–Set options for specific downloads.
-
split_input_file
–Helper to split downloads in an input file.
-
stop_listening
–Stop listening to notifications.
Source code in src/aria2p/api.py
38 39 40 41 42 43 44 45 |
|
add ¤
Add a download (guess its type).
If the provided URI is in fact a file-path, and is neither a torrent or a metalink, then we read its lines and try to add each line as a download, recursively.
Parameters:
-
uri
(str
) –The URI or file-path to add.
-
options
(OptionsType | None
, default:None
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
position
(int | None
, default:None
) –The position where to insert the new download in the queue. Start at 0 (top).
Returns:
Source code in src/aria2p/api.py
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 |
|
add_magnet ¤
add_magnet(
magnet_uri: str,
options: OptionsType | None = None,
position: int | None = None,
) -> Download
Add a download with a Magnet URI.
Parameters:
-
magnet_uri
(str
) –The Magnet URI.
-
options
(OptionsType | None
, default:None
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
position
(int | None
, default:None
) –The position where to insert the new download in the queue. Start at 0 (top).
Returns:
-
Download
–The newly created download object.
Source code in src/aria2p/api.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
add_metalink ¤
add_metalink(
metalink_file_path: str | Path,
options: OptionsType | None = None,
position: int | None = None,
) -> list[Download]
Add a download with a Metalink file.
Parameters:
-
metalink_file_path
(str | Path
) –The path to the Metalink file.
-
options
(OptionsType | None
, default:None
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
position
(int | None
, default:None
) –The position where to insert the new download in the queue. Start at 0 (top).
Returns:
Source code in src/aria2p/api.py
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 |
|
add_torrent ¤
add_torrent(
torrent_file_path: str | Path,
uris: list[str] | None = None,
options: OptionsType | None = None,
position: int | None = None,
) -> Download
Add a download with a torrent file (usually .torrent extension).
Parameters:
-
torrent_file_path
(str | Path
) –The path to the torrent file.
-
uris
(list[str] | None
, default:None
) –A list of URIs used for Web-seeding.
-
options
(OptionsType | None
, default:None
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
position
(int | None
, default:None
) –The position where to insert the new download in the queue. Start at 0 (top).
Returns:
-
Download
–The newly created download object.
Source code in src/aria2p/api.py
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 |
|
add_uris ¤
add_uris(
uris: list[str],
options: OptionsType | None = None,
position: int | None = None,
) -> Download
Add a download with a URL (or more).
Parameters:
-
uris
(list[str]
) –A list of URIs that point to the same resource.
-
options
(OptionsType | None
, default:None
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
position
(int | None
, default:None
) –The position where to insert the new download in the queue. Start at 0 (top).
Returns:
-
Download
–The newly created download object.
Source code in src/aria2p/api.py
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 |
|
autopurge ¤
autopurge() -> bool
Purge completed, removed or failed downloads from the queue.
Deprecated. Use purge
instead.
Returns:
-
bool
–Success or failure of the operation.
Source code in src/aria2p/api.py
558 559 560 561 562 563 564 565 566 567 |
|
copy_files staticmethod
¤
copy_files(
downloads: list[Download],
to_directory: str | Path,
force: bool = False,
) -> list[bool]
Copy downloaded files to another directory.
Parameters:
-
downloads
(list[Download]
) –the list of downloads for which to move files.
-
to_directory
(str | Path
) –The target directory to copy files into.
-
force
(bool
, default:False
) –Whether to move files even if download is not complete.
Returns:
Source code in src/aria2p/api.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
|
get_download ¤
Get a Download
object thanks to its GID.
Parameters:
-
gid
(str
) –The GID of the download to get.
Returns:
-
Download
–The retrieved download object.
Source code in src/aria2p/api.py
255 256 257 258 259 260 261 262 263 264 |
|
get_downloads ¤
Get a list of Download
object thanks to their GIDs.
Parameters:
-
gids
(list[str] | None
, default:None
) –The GIDs of the downloads to get. If None, return all the downloads.
Returns:
Source code in src/aria2p/api.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
get_global_options ¤
get_global_options() -> Options
Get the global options.
Returns:
-
Options
–The global aria2c options.
Source code in src/aria2p/api.py
584 585 586 587 588 589 590 |
|
get_options ¤
Get options for each of the given downloads.
Parameters:
Returns:
Source code in src/aria2p/api.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
|
get_stats ¤
get_stats() -> Stats
Get the stats of the remote aria2c process.
Returns:
-
Stats
–The global stats returned by the remote process.
Source code in src/aria2p/api.py
625 626 627 628 629 630 631 |
|
listen_to_notifications ¤
listen_to_notifications(
threaded: bool = False,
on_download_start: Callable | None = None,
on_download_pause: Callable | None = None,
on_download_stop: Callable | None = None,
on_download_complete: Callable | None = None,
on_download_error: Callable | None = None,
on_bt_download_complete: Callable | None = None,
timeout: int = 5,
handle_signals: bool = True,
) -> None
Start listening to aria2 notifications via WebSocket.
This method differs from Client.listen_to_notifications
in that it expects callbacks accepting two arguments, api
and gid
, instead of only gid
. Accepting api
allows to use the high-level methods of the API
class.
Stop listening to notifications with the API.stop_listening
method.
Parameters:
-
threaded
(bool
, default:False
) –Whether to start the listening loop in a thread or not (non-blocking or blocking).
-
on_download_start
(Callable | None
, default:None
) –Callback for the
onDownloadStart
event. -
on_download_pause
(Callable | None
, default:None
) –Callback for the
onDownloadPause
event. -
on_download_stop
(Callable | None
, default:None
) –Callback for the
onDownloadStop
event. -
on_download_complete
(Callable | None
, default:None
) –Callback for the
onDownloadComplete
event. -
on_download_error
(Callable | None
, default:None
) –Callback for the
onDownloadError
event. -
on_bt_download_complete
(Callable | None
, default:None
) –Callback for the
onBtDownloadComplete
event. -
timeout
(int
, default:5
) –Timeout when waiting for data to be received. Use a small value for faster reactivity when stopping to listen. Default is 5 seconds.
-
handle_signals
(bool
, default:True
) –Whether to add signal handlers to gracefully stop the loop on SIGTERM and SIGINT.
Source code in src/aria2p/api.py
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 |
|
move ¤
Move a download in the queue, relatively to its current position.
Parameters:
-
download
(Download
) –The download object to move.
-
pos
(int
) –The relative position (1 to move down, -1 to move up, -2 to move up two times, etc.).
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
289 290 291 292 293 294 295 296 297 298 299 |
|
move_down ¤
Move a download down in the queue.
Parameters:
-
download
(Download
) –The download object to move.
-
pos
(int
, default:1
) –Number of times to move down. With negative values, will move up (use move or move_up instead).
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
330 331 332 333 334 335 336 337 338 339 340 |
|
move_files staticmethod
¤
move_files(
downloads: list[Download],
to_directory: str | Path,
force: bool = False,
) -> list[bool]
Move downloaded files to another directory.
Parameters:
-
downloads
(list[Download]
) –the list of downloads for which to move files.
-
to_directory
(str | Path
) –The target directory to move files to.
-
force
(bool
, default:False
) –Whether to move files even if download is not complete.
Returns:
Source code in src/aria2p/api.py
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
|
move_to ¤
Move a download in the queue, with absolute positioning.
Parameters:
-
download
(Download
) –The download object to move.
-
pos
(int
) –The absolute position in the queue where to move the download. 0 for top, -1 for bottom.
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
move_to_bottom ¤
Move a download to the bottom of the queue.
Parameters:
-
download
(Download
) –The download object to move.
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
353 354 355 356 357 358 359 360 361 362 |
|
move_to_top ¤
Move a download to the top of the queue.
Parameters:
-
download
(Download
) –The download object to move.
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
342 343 344 345 346 347 348 349 350 351 |
|
move_up ¤
Move a download up in the queue.
Parameters:
-
download
(Download
) –The download object to move.
-
pos
(int
, default:1
) –Number of times to move up. With negative values, will move down (use move or move_down instead).
Returns:
-
int
–The new position of the download.
Source code in src/aria2p/api.py
318 319 320 321 322 323 324 325 326 327 328 |
|
parse_input_file ¤
Parse a file with URIs or an aria2c input file.
Parameters:
Returns:
-
InputFileContentsType
–List of tuples containing list of URIs and dictionary with aria2c options.
Source code in src/aria2p/api.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
|
pause ¤
Pause the given (active) downloads.
Parameters:
-
downloads
(list[Download]
) –The list of downloads to pause.
-
force
(bool
, default:False
) –Whether to pause immediately without contacting servers or not.
Returns:
-
list[OperationResult]
–Success or failure of the operation for each given download.
Source code in src/aria2p/api.py
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 |
|
pause_all ¤
Pause all (active) downloads.
Parameters:
-
force
(bool
, default:False
) –Whether to pause immediately without contacting servers or not.
Returns:
-
bool
–Success or failure of the operation to pause all downloads.
Source code in src/aria2p/api.py
506 507 508 509 510 511 512 513 514 515 516 |
|
purge ¤
purge() -> bool
Purge completed, removed or failed downloads from the queue.
Returns:
-
bool
–Success or failure of the operation.
Source code in src/aria2p/api.py
550 551 552 553 554 555 556 |
|
remove ¤
remove(
downloads: list[Download],
force: bool = False,
files: bool = False,
clean: bool = True,
) -> list[OperationResult]
Remove the given downloads from the list.
Parameters:
-
downloads
(list[Download]
) –The list of downloads to remove.
-
force
(bool
, default:False
) –Whether to force the removal or not.
-
files
(bool
, default:False
) –Whether to remove downloads files as well.
-
clean
(bool
, default:True
) –Whether to remove the aria2 control file as well.
Returns:
-
list[OperationResult]
–Success or failure of the operation for each given download.
Source code in src/aria2p/api.py
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 |
|
remove_all ¤
Remove all downloads from the list.
Parameters:
-
force
(bool
, default:False
) –Whether to force the removal or not.
Returns:
-
bool
–Success or failure of the operation to remove all downloads.
Source code in src/aria2p/api.py
468 469 470 471 472 473 474 475 476 477 |
|
remove_files staticmethod
¤
Remove downloaded files.
Parameters:
-
downloads
(list[Download]
) –the list of downloads for which to remove files.
-
force
(bool
, default:False
) –Whether to remove files even if download is not complete.
Returns:
Source code in src/aria2p/api.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
resume ¤
Resume (unpause) the given downloads.
Parameters:
Returns:
-
list[OperationResult]
–Success or failure of the operation for each given download.
Source code in src/aria2p/api.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
resume_all ¤
resume_all() -> bool
Resume (unpause) all downloads.
Returns:
-
bool
–Success or failure of the operation to resume all downloads.
Source code in src/aria2p/api.py
542 543 544 545 546 547 548 |
|
retry_downloads ¤
Resume failed downloads from where they left off with new GIDs.
Parameters:
-
downloads
(list[Download]
) –The list of downloads to remove.
-
clean
(bool
, default:False
) –Whether to remove the aria2 control file as well.
Returns:
-
list[OperationResult]
–Success or failure of the operation for each given download.
Source code in src/aria2p/api.py
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 |
|
search ¤
Not implemented.
Search and return Download
objects based on multiple patterns.
Parameters:
Raises:
-
NotImplementedError
–This method is not implemented yet.
Source code in src/aria2p/api.py
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 |
|
set_global_options ¤
set_global_options(options: OptionsType) -> bool
Set global options.
Parameters:
-
options
(OptionsType
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with.
Returns:
-
bool
–Success or failure of the operation for changing global options.
Source code in src/aria2p/api.py
611 612 613 614 615 616 617 618 619 620 621 622 623 |
|
set_options ¤
Set options for specific downloads.
Parameters:
-
options
(OptionsType
) –An instance of the
Options
class or a dictionary containing aria2c options to create the download with. -
downloads
(list[Download]
) –The list of downloads to set the options for.
Returns:
Source code in src/aria2p/api.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
split_input_file ¤
Helper to split downloads in an input file.
Parameters:
Yields:
Source code in src/aria2p/api.py
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
|
stop_listening ¤
stop_listening() -> None
Stop listening to notifications.
If the listening loop was threaded, this method will wait for the thread to finish. The time it takes for the thread to finish will depend on the timeout given while calling listen_to_notifications
.
Source code in src/aria2p/api.py
793 794 795 796 797 798 799 800 801 802 803 |
|