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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 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 670 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 702 703 704 705
|
"""Radarr API."""
from __future__ import annotations
from datetime import date as dt, datetime
from typing import Any
from aiohttp.client import ClientSession
from .const import (
ALL,
DATE,
EVENT_TYPE,
IS_VALID,
MOVIE_ID,
NOTIFICATION,
PAGE,
PAGE_SIZE,
PATH,
SORT_DIRECTION,
SORT_KEY,
TERM,
TITLE,
HTTPMethod,
)
from .exceptions import ArrException
from .models.host_configuration import PyArrHostConfiguration
from .models.radarr import (
RadarrAltTitle,
RadarrBlocklist,
RadarrBlocklistMovie,
RadarrCalendarItem,
RadarrCommands,
RadarrCredit,
RadarrEventType,
RadarrExtraFile,
RadarrHistory,
RadarrImportList,
RadarrImportListActionType,
RadarrImportListMovie,
RadarrIndexerFlag,
RadarrManualImport,
RadarrMovie,
RadarrMovieEditor,
RadarrMovieFile,
RadarrMovieHistory,
RadarrNamingConfig,
RadarrNotification,
RadarrParse,
RadarrQueue,
RadarrQueueDetail,
RadarrRelease,
RadarrRename,
RadarrRestriction,
RadarrSortKeys,
RadarrTagDetails,
)
from .models.request import Command, RootFolder, SortDirection
from .request_client import RequestClient
class RadarrClient(RequestClient): # pylint: disable=too-many-public-methods
"""API client for Radarr endpoints."""
__name__ = "Radarr"
def __init__( # pylint: disable=too-many-arguments
self,
host_configuration: PyArrHostConfiguration | None = None,
session: ClientSession | None = None,
hostname: str | None = None,
ipaddress: str | None = None,
url: str | None = None,
api_token: str | None = None,
port: int = 7878,
ssl: bool | None = None,
verify_ssl: bool | None = None,
base_api_path: str | None = None,
request_timeout: float = 60,
raw_response: bool = False,
api_ver: str = "v3",
) -> None:
"""Initialize Radarr API."""
super().__init__(
port,
request_timeout,
raw_response,
api_ver,
host_configuration,
session,
hostname,
ipaddress,
url,
api_token,
ssl,
verify_ssl,
base_api_path,
)
async def async_get_movies(
self,
movieid: int | None = None,
tmdb: bool = False,
) -> RadarrMovie | list[RadarrMovie]:
"""Get information about movies.
Include an id for a specific movie or leave black for all.
tmdb: Use TMDB ID.
"""
return await self._async_request(
f"movie{'' if movieid is None or tmdb else f'/{movieid}'}",
params=None if movieid is None else {"tmdbid": movieid},
datatype=RadarrMovie,
)
async def async_add_movies(
self, data: RadarrMovie | list[RadarrMovie]
) -> RadarrMovie | list[RadarrMovie]:
"""Add movie to the database."""
return await self._async_request(
f"movie{'/import' if isinstance(data, list) else ''}",
data=data,
datatype=RadarrMovie,
method=HTTPMethod.POST,
)
async def async_edit_movies(
self, data: RadarrMovie | RadarrMovieEditor, move_files: bool = False
) -> RadarrMovie | list[RadarrMovie]:
"""Edit movie properties of multiple movies at once."""
params = {"moveFiles": str(move_files)}
return await self._async_request(
f"movie{'' if isinstance(data, RadarrMovie) else '/editor'}",
params=params if isinstance(data, RadarrMovie) else None,
data=data,
datatype=RadarrMovie,
method=HTTPMethod.PUT,
)
async def async_delete_movies(
self,
ids: int | list[int],
delete_files: bool = False,
add_exclusion: bool = False,
) -> None:
"""Delete movies (and optionally files).
ids: include an integer to delete one movie or a list for mass deletion
"""
data: dict[str, str | list[int]] = {
"deleteFiles": str(delete_files),
"addImportExclusion": str(add_exclusion),
}
if isinstance(ids, list):
data["movieIds"] = ids
return await self._async_request(
"movie/editor" if isinstance(ids, list) else f"movie/{ids}",
params=None if isinstance(ids, list) else data,
data=data if isinstance(ids, list) else None,
method=HTTPMethod.DELETE,
)
async def async_import_movies(self, data: list[RadarrMovie]) -> list[RadarrMovie]:
"""Import movies in bulk.
It allows movies to be bulk added to the Radarr database.
"""
return await self._async_request(
"movie/import",
data=data,
datatype=RadarrMovie,
method=HTTPMethod.POST,
)
async def async_delete_movie_file(self, movieid: int) -> None:
"""Delete a moviefile by its database id."""
return await self._async_request(
f"moviefile/{movieid}",
method=HTTPMethod.DELETE,
)
async def async_lookup_movie(
self, term: str, tmdb: bool = True
) -> list[RadarrMovie]:
"""Lookup information about movie.
tmdb: Use TMDB IDs. Set to False to use IMDB.
"""
return await self._async_request(
"movie/lookup",
params={TERM: f"{'tmdb' if tmdb else 'imdb'}:{term}"},
datatype=RadarrMovie,
)
async def async_lookup_movie_files(
self, ids: list[int]
) -> RadarrMovieFile | list[RadarrMovieFile]:
"""Get movie file information for multiple movie files."""
return await self._async_request(
f"moviefile{'' if isinstance(ids, list) else f'/{ids}'}",
params={"movieFileIds": ids} if isinstance(ids, list) else None,
datatype=RadarrMovieFile,
)
async def async_get_history(
self,
page: int = 1,
page_size: int = 20,
sort_key: RadarrSortKeys = RadarrSortKeys.DATE,
event_type: RadarrEventType | None = None,
) -> RadarrHistory:
"""Get movie history.
Args:
page: Page to be returned.
page_size: Number of results per page.
sort_key: date, id, movieid, title, sourcetitle, path, ratings, or quality
(Others do not apply)
"""
params = {
PAGE: page,
PAGE_SIZE: page_size,
SORT_KEY: sort_key.value,
}
if event_type and event_type in RadarrEventType:
params[EVENT_TYPE] = event_type.value
return await self._async_request(
"history",
params=params,
datatype=RadarrHistory,
)
async def async_get_history_since(
self,
date: datetime | None = None,
movieid: int | None = None,
event_type: RadarrEventType | None = None,
) -> list[RadarrMovieHistory]:
"""Get history since specified date.
movieid: include to search history by movie id (date will not apply)
Radarr permits a naked query but its required here to avoid excessively large
data sets where filtering should be used instead
"""
if date is None and movieid is None:
raise ArrException(self, "Either date or movieid is required")
params: dict[str, int | str] = {}
if isinstance(date, datetime):
params[DATE] = date.strftime("%Y-%m-%d")
elif movieid is not None:
params[MOVIE_ID] = movieid
if event_type and event_type in RadarrEventType:
params[EVENT_TYPE] = event_type.value
return await self._async_request(
f"history/{'since' if isinstance(date, datetime) else 'movie'}",
params=params,
datatype=RadarrMovieHistory,
)
async def async_get_import_lists(
self, listid: int | None = None
) -> RadarrImportList | list[RadarrImportList]:
"""Get information about import lists."""
return await self._async_request(
f"importlist{'' if listid is None else f'/{listid}'}",
datatype=RadarrImportList,
)
async def async_edit_import_list(self, data: RadarrImportList) -> RadarrImportList:
"""Edit an importlist."""
return await self._async_request(
"importlist",
data=data,
datatype=RadarrImportList,
method=HTTPMethod.PUT,
)
async def async_add_import_list(self, data: RadarrImportList) -> RadarrImportList:
"""Add import list."""
return await self._async_request(
"importlist", data=data, datatype=RadarrImportList, method=HTTPMethod.POST
)
async def async_test_import_lists(
self, data: RadarrImportList | None = None
) -> bool:
"""Test all import lists."""
_res = await self._async_request(
f"importlist/test{ALL if data is None else ''}",
data=None if data is None else data,
method=HTTPMethod.POST,
)
if data is None:
for item in _res:
if item[IS_VALID] is False:
return False
return True
async def async_get_import_list_movies(self) -> list[RadarrImportListMovie]:
"""Get list of movies on configured import lists."""
return await self._async_request(
"importlist/movie",
datatype=RadarrImportListMovie,
)
async def async_get_extra_file(self, movieid: int) -> list[RadarrExtraFile]:
"""Get extra files info from specified movie id."""
return await self._async_request(
"extrafile",
params={MOVIE_ID: movieid},
datatype=RadarrExtraFile,
)
async def async_get_restrictions(
self, restrictionid: int | None = None
) -> RadarrRestriction | list[RadarrRestriction]:
"""Get indexer restrictions."""
return await self._async_request(
f"restriction{'' if restrictionid is None else f'/{restrictionid}'}",
datatype=RadarrRestriction,
)
async def async_edit_restriction(
self, data: RadarrRestriction
) -> RadarrRestriction:
"""Edit indexer restriction."""
return await self._async_request(
"restriction",
data=data,
datatype=RadarrRestriction,
method=HTTPMethod.PUT,
)
async def async_add_restriction(self, data: RadarrRestriction) -> RadarrRestriction:
"""Add indexer restriction."""
return await self._async_request(
"restriction",
data=data,
datatype=RadarrRestriction,
method=HTTPMethod.POST,
)
async def async_delete_restriction(self, restrictionid: int) -> None:
"""Delete indexer restriction."""
return await self._async_request(
f"restriction/{restrictionid}",
datatype=RadarrRestriction,
method=HTTPMethod.DELETE,
)
async def async_get_credits(
self, creditid: int | None = None, movieid: int | None = None
) -> RadarrCredit | list[RadarrCredit]:
"""Get credits."""
return await self._async_request(
f"credit{'' if creditid is None else f'/{creditid}'}",
params=None if movieid is None else {MOVIE_ID: movieid},
datatype=RadarrCredit,
)
async def async_get_alt_titles(
self, alttitleid: int | None = None, movieid: int | None = None
) -> RadarrAltTitle | list[RadarrAltTitle]:
"""Get alternate movie titles."""
return await self._async_request(
f"alttitle{'' if alttitleid is None else f'/{alttitleid}'}",
params=None if movieid is None else {MOVIE_ID: movieid},
datatype=RadarrAltTitle,
)
async def async_get_indexer_flags(self) -> list[RadarrIndexerFlag]:
"""Get indexer flags."""
return await self._async_request(
"indexerflag",
datatype=RadarrIndexerFlag,
)
async def async_importlist_action(
self, action: RadarrImportListActionType
) -> dict[str, Any]:
"""Perform import list action."""
return await self._async_request(
f"importlist/action/{action.value}",
method=HTTPMethod.POST,
)
async def async_get_naming_config(self) -> RadarrNamingConfig:
"""Get information about naming configuration."""
return await self._async_request("config/naming", datatype=RadarrNamingConfig)
async def async_edit_naming_config(
self, data: RadarrNamingConfig
) -> RadarrNamingConfig:
"""Edit Settings for file and folder naming."""
return await self._async_request(
"config/naming",
data=data,
datatype=RadarrNamingConfig,
method=HTTPMethod.PUT,
)
async def async_get_tags_details(
self, tagid: int | None = None
) -> RadarrTagDetails | list[RadarrTagDetails]:
"""Get information about tag details.
id: Get tag details matching id. Leave blank for all.
"""
return await self._async_request(
f"tag/detail{'' if tagid is None else f'/{tagid}'}",
datatype=RadarrTagDetails,
)
async def async_get_blocklist(
self,
page: int = 1,
page_size: int = 20,
sort_dir: SortDirection = SortDirection.DEFAULT,
sort_key: RadarrSortKeys = RadarrSortKeys.DATE,
) -> RadarrBlocklist:
"""Return blocklisted releases.
Args:
page: Page to be returned.
page_size: Number of results per page.
sort_key: date, id, movieid, title, path, sourcetitle, ratings, or quality
(Others do not apply)
"""
params = {
PAGE: page,
PAGE_SIZE: page_size,
SORT_DIRECTION: sort_dir.value,
SORT_KEY: sort_key.value,
}
return await self._async_request(
"blocklist",
params=params,
datatype=RadarrBlocklist,
)
async def async_get_blocklist_movie(
self,
bocklistid: int,
) -> list[RadarrBlocklistMovie]:
"""Retrieve blocklisted releases that are tied to a given movie in the database."""
return await self._async_request(
"blocklist/movie",
params={MOVIE_ID: bocklistid},
datatype=RadarrBlocklistMovie,
)
async def async_get_queue( # pylint: disable=too-many-arguments
self,
page: int = 1,
page_size: int = 20,
sort_dir: SortDirection = SortDirection.DEFAULT,
sort_key: RadarrSortKeys = RadarrSortKeys.TIMELEFT,
include_unknown_movie_items: bool = False,
include_movie: bool = False,
) -> RadarrQueue:
"""Return a json object list of items in the queue.
Args:
page: Page to be returned.
page_size: Number of results per page.
include_unknown_movie_items: Include unknown movie items.
"""
params = {
PAGE: page,
PAGE_SIZE: page_size,
SORT_DIRECTION: sort_dir.value,
SORT_KEY: sort_key.value,
"includeUnknownMovieItems": str(include_unknown_movie_items),
"includeMovie": str(include_movie),
}
return await self._async_request("queue", params=params, datatype=RadarrQueue)
async def async_get_queue_details(
self,
include_unknown_movie_items: bool = False,
include_movie: bool = True,
) -> list[RadarrQueueDetail]:
"""Get details of all items in queue."""
params = {
"includeUnknownMovieItems": str(include_unknown_movie_items),
"includeMovie": str(include_movie),
}
return await self._async_request(
"queue/details",
params=params,
datatype=RadarrQueueDetail,
)
async def async_get_notifications(
self, notifyid: int | None = None
) -> RadarrNotification | list[RadarrNotification]:
"""Get information about notification.
id: Get notification matching id. Leave blank for all.
"""
return await self._async_request(
f"notification{'' if notifyid is None else f'/{notifyid}'}",
datatype=RadarrNotification,
)
async def async_edit_notification(
self, data: RadarrNotification
) -> RadarrNotification:
"""Edit a notification."""
return await self._async_request(
NOTIFICATION,
data=data,
datatype=RadarrNotification,
method=HTTPMethod.PUT,
)
async def async_add_notification(
self, data: RadarrNotification
) -> RadarrNotification:
"""Add a notification."""
return await self._async_request(
NOTIFICATION,
data=data,
datatype=RadarrNotification,
method=HTTPMethod.POST,
)
async def async_test_notifications(
self, data: RadarrNotification | None = None
) -> bool:
"""Test a notification configuration."""
_res = await self._async_request(
f"notification/test{ALL if data is None else ''}",
data=None if data is None else data,
method=HTTPMethod.POST,
)
if data is None:
for item in _res:
if item[IS_VALID] is False:
return False
return True
async def async_parse(self, title: str) -> RadarrParse:
"""Return the movie with matching file name."""
params = {TITLE: title}
return await self._async_request("parse", params=params, datatype=RadarrParse)
async def async_radarr_command( # pylint: disable=too-many-arguments
self,
command: RadarrCommands,
clientid: int | None = None,
copymode: bool = True,
files: list[int] | None = None,
path: str | None = None,
movieid: int | list[int] | None = None,
) -> Command:
"""Send a command to Radarr.
Specify clientid for DownloadedMoviesScan (Optional)
Specify files for RenameFiles
Specify path for DownloadedMoviesScan (Optional)
Specify movieid for:
RefreshMovie (Optional),
RenameMovie (list[int]),
RescanMovie (Optional),
MovieSearch
"""
data: dict[str, str | int | list[int]] = {"name": command.value}
if clientid is not None:
data["downloadClientId"] = clientid
if files is not None:
data["files"] = files
if path is not None:
data[PATH] = path
if movieid is not None:
if command == RadarrCommands.RENAME_MOVIE:
data["movieIds"] = movieid
else:
data[MOVIE_ID] = movieid
if command is RadarrCommands.DOWNLOADED_MOVIES_SCAN:
data["importMode"] = "Copy" if copymode else "Move"
return await self._async_request(
"command",
data=data,
datatype=Command,
method=HTTPMethod.POST,
)
async def async_get_calendar(
self,
start_date: dt | None = None,
end_date: dt | None = None,
unmonitored: bool = True,
) -> list[RadarrCalendarItem]:
"""Get a list of movies based on calendar parameters."""
params = {"unmonitored": str(unmonitored)}
if start_date:
params["start"] = str(start_date)
if end_date:
params["end"] = str(end_date)
return await self._async_request(
"calendar",
params=params,
datatype=RadarrCalendarItem,
)
async def async_get_release(
self, movieid: int | None = None
) -> list[RadarrRelease]:
"""Search indexers for specified fields."""
return await self._async_request(
"release",
params=None if movieid is None else {MOVIE_ID: movieid},
datatype=RadarrRelease,
)
async def async_download_release(
self, guid: str, indexerid: int
) -> list[RadarrRelease]:
"""Add a previously searched release to the download client.
If the release is
still in the search cache (30 minute cache). If the release is not found
in the cache it will return a 404.
guid: Recently searched result guid
"""
return await self._async_request(
"release",
data={"guid": guid, "indexerId": indexerid},
datatype=RadarrRelease,
method=HTTPMethod.POST,
)
async def async_push_release(self, data: RadarrRelease) -> list[RadarrRelease]:
"""Push release."""
return await self._async_request(
"release/push",
data=data,
datatype=RadarrRelease,
method=HTTPMethod.POST,
)
async def async_get_rename(self, movieid: int) -> list[RadarrRename]:
"""Get files matching specified id that are not properly renamed yet."""
return await self._async_request(
"rename",
params={MOVIE_ID: movieid},
datatype=RadarrRename,
)
async def async_get_manual_import(
self,
downloadid: str,
folder: str | None = None,
filterexistingfiles: bool = True,
) -> list[RadarrManualImport]:
"""Get manual import."""
params = {
"downloadId": downloadid,
"filterExistingFiles": str(filterexistingfiles),
"folder": folder if folder is not None else "",
}
return await self._async_request(
"manualimport", params=params, datatype=RadarrManualImport
)
async def async_edit_manual_import(
self, data: RadarrManualImport
) -> list[RadarrManualImport]:
"""Get manual import."""
return await self._async_request(
"manualimport",
data=data,
datatype=RadarrManualImport,
method=HTTPMethod.PUT,
)
async def async_get_root_folders(
self, folderid: int | None = None
) -> RootFolder | list[RootFolder]:
"""Get information about root folders."""
return await self._async_request(
f"rootfolder{'' if folderid is None else f'/{folderid}'}",
datatype=RootFolder,
)
async def async_get_release_profiles(self, profileid: int | None = None) -> Any:
"""Get release profiles."""
raise NotImplementedError()
async def async_edit_release_profile(self, data: Any) -> Any:
"""Edit release profile."""
raise NotImplementedError()
async def async_delete_release_profile(self, profileid: int) -> Any:
"""Delete release profiles."""
raise NotImplementedError()
async def async_add_release_profile(self, data: Any) -> Any:
"""Add release profile."""
raise NotImplementedError()
async def async_delete_metadata_profile(self, profileid: int) -> Any:
"""Delete a metadata profile."""
raise NotImplementedError()
|