def save_or_update(self, value: Optional[Dict]): result_ids: List[Any] = self.db.upsert(value, where('id') == value['id']) if result_ids.__len__() < 1: EventLogHelper.log_error(f"Error objects to {ANILIST_DATABASE}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL)
def __handle_response(self, media_collection_list: Optional[List[Dict]], anilist_store: AniListStore): try: for item in media_collection_list: for entry in item['entries']: anilist_store.save_or_update(entry) except Exception as e: EventLogHelper.log_error(f"Error handling response -> {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL)
def get_all(self) -> List[Optional[TorrentInfo]]: query_results: List[Optional[TorrentInfo]] = list() documents: List[Document] = self.db.all() try: for document in documents: data_class = self.model_helper.create_data_class(document) query_results.append(data_class) except Exception as e: EventLogHelper.log_error( f"Database value is not in a valid format {APP_DATABASE}\n" f"Details: {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL) return query_results
def __init__(self) -> None: super().__init__() try: self.config = json.loads(StorageUtil.read_file('config', 'plex.json')) auth = json.loads(StorageUtil.read_file("auth", "credentials.json")) self.plex = PlexServer(auth["url"], auth["token"]) except Exception as e: EventLogHelper.log_error( f"Encountered exception while initializing controller -> {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL )
def start_application(self) -> None: """ Application starting point :return: """ try: anime_list: List[Optional[MediaEntry]] = self.fetch_anime_list() print('-------------------------------------------------------') if anime_list: download_queue: DownloadableQueue = self.find_plex_show(anime_list) print('-------------------------------------------------------') if download_queue.contains_items(): print('-------------------------------------------------------') search_results = self.search_nyaa_for_shows(download_queue) if search_results is not None and len(search_results) > 0: for torrent_info in search_results: if torrent_info.anime_info is None: print() EventLogHelper.log_info( f"Skipping torrent without anime info -> {torrent_info}", self.__class__.__name__, inspect.currentframe().f_code.co_name ) continue downloaded_torrent = self.__find_downloadable_torrents(torrent_info) if downloaded_torrent is None or len(downloaded_torrent) < 1: queued: bool = self.__queue_downloaded_torrent_file(torrent_info) if not queued: self.__download_torrent_file(torrent_info) else: print() EventLogHelper.log_info( f"Skipping existing download -> {torrent_info.anime_info}", self.__class__.__name__, inspect.currentframe().f_code.co_name ) else: print() EventLogHelper.log_info( f"No new episodes to download, ending execution of script", self.__class__.__name__, inspect.currentframe().f_code.co_name ) print('-------------------------------------------------------') except Exception as e: EventLogHelper.log_error(f"Uncaught exception thrown -> {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name)
def save_or_update(self, value: Optional[Dict]): result_ids: List[Any] = list() try: result_ids += self.db.upsert(value, where('name') == value['name']) except Exception as e: EventLogHelper.log_error( f"Error saving or updating model to {value}\n" f"Details: {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL) if len(result_ids) < 1: EventLogHelper.log_error(f"Error objects to {APP_DATABASE}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL)
def __init__(self) -> None: super().__init__() try: __config = json.loads( StorageUtil.read_file('auth', 'credentials.json')) if __config is not None: credentials = __config["transmission"] self.client = Client(host=credentials["host"], port=credentials["port"], username=credentials["username"], password=credentials["password"]) else: self.client = Client() except Exception as e: EventLogHelper.log_error( f"Encountered exception while initializing controller -> {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name, logging.CRITICAL)
def __move_torrent_to_monitored_directory(self, torrent_info: TorrentInfo): try: StorageUtil.copy_or_move_file( filename=f"{torrent_info.name}.torrent", directory_path=self.app_config.build_parent_save_path( torrent_info.anime_info.anime_title ), destination_path=self.app_config.torrent_monitor_directory, keep_file=self.app_config.torrent_keep_file_after_queuing ) model = self.nyaa_model_helper.create_dictionary_class(torrent_info) self.app_store.save_or_update(model) except Exception as e: EventLogHelper.log_error( f"__move_torrent_to_monitored_directory -> StorageUtil.copy_or_move_file -> {e}", self.__class__.__name__, inspect.currentframe().f_code.co_name )
def download_torrent_file(torrent_info: TorrentInfo, config: AppConfig) -> bool: """ Downloads a .torrent file and saves it into the app/torrents/ directory :param config: configuration class :param torrent_info: :return: True if the operation was successful otherwise False """ try: print() torrent_file_name = f"{torrent_info.anime_info.file_name}.torrent" response: Response = get(url=torrent_info.download_url, allow_redirects=True, stream=True, timeout=30.0) if response.ok: StorageUtil.write_file_in_app( directory_path=config.build_parent_save_path( torrent_info.anime_info.anime_title), filename=torrent_file_name, contents=response, write_mode='wb') else: EventLogHelper.log_info( f"Requesting torrent for download failed : {response}\n" f"Retrying in 5 seconds..", "NyaaController", inspect.currentframe().f_code.co_name) sleep(5) NyaaController.download_torrent_file(torrent_info, config) print( '<------------------------------------------------------------>' ) except Exception as e: EventLogHelper.log_error( f"Encountered exception while downloading torrent file -> {e}", "NyaaController", inspect.currentframe().f_code.co_name, logging.CRITICAL) return False return True