def get_sync_jobs(config: ConfigParser) -> List[SyncJob]: sync_jobs: List[SyncJob] = [] for section_name in config.sections(): section = config[section_name] job_opts = {"name": section_name} for option_name in section: job_opts[option_name] = section[option_name] try: job_type = JobType(job_opts["type"]) if job_type == JobType.Sonarr: sync_jobs.append(SonarrSyncJob.parse_obj(job_opts)) elif job_type == JobType.Radarr: sync_jobs.append(RadarrSyncJob.parse_obj(job_opts)) elif job_type == JobType.Lidarr: sync_jobs.append(LidarrSyncJob.parse_obj(job_opts)) except ValidationError as e: logger.error("%s: config error", section_name) logger.error(e) raise return sync_jobs
def start_sync_job(job: SyncJob, dry_run: bool = False) -> None: logger.debug("starting %s job", job.name) with Api( job_type=job.type, url=job.source_url, api_key=job.source_key, headers=job.source_headers, ) as source_api, Api( job_type=job.type, url=job.dest_url, api_key=job.dest_key, headers=job.dest_headers, ) as dest_api: source_status = source_api.status() dest_status = dest_api.status() if not source_status or not dest_status: logger.error("failed %s job", job.name) raise Exception("failed to check stauts") source_tags = source_api.tag() source_profiles = source_api.profile() dest_profiles = dest_api.profile() dest_metadata_profiles = dest_api.metadata() dest_languages = dest_api.language() source_content = source_api.content() dest_content = dest_api.content() content_diff = calculate_content_diff( job=job, source_content=source_content, source_tags=source_tags, source_profiles=source_profiles, dest_content=dest_content, ) content_payloads = get_content_payloads( job=job, content=content_diff, dest_profiles=dest_profiles, dest_metadata_profiles=dest_metadata_profiles, dest_languages=dest_languages, ) sync_content(content=content_payloads, dest_api=dest_api, dry_run=dry_run)
def _response_json(self, response: Response, url: str) -> Any: if not response.ok: raise Exception( f"failed to check status for {url} got {response.status_code}") if not response.text: logger.error("%s response_text: %s", url, response.text) raise Exception( f"no response in status for {url}. Is the server set up correctly?" ) return response.json()
def main(config: ConfigParser, dry_run: bool = False) -> None: sync_jobs = get_sync_jobs(config) logger.debug(sync_jobs) for job in sync_jobs: name = job.name try: logger.info("%s: starting", name) start_sync_job(job, dry_run) logger.info("%s: finished", name) except Exception as e: logger.error("%s: error", name) logger.error(e)
def sync_content(content: ContentItems, dest_api: Api, dry_run: bool = False) -> None: for item in content: post_json = None if not dry_run: post_json = dest_api.save(content_item=item) if not post_json and not dry_run: logger.error("failed to sync %s", get_debug_title(item)) raise Exception(f"Failed to create {get_debug_title(item)}") else: logger.info("synced %s%s", get_debug_title(item), " (dry-run)" if dry_run else "")