From 93304aebd2fa2580278d3db73d28870fc5be6b06 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 12 Jul 2020 17:44:20 +0200 Subject: Decrease cpu usage (every 15 seconds), fix incorrect doc --- automedia.py | 58 ++++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/automedia.py b/automedia.py index b4c7d41..e514dea 100755 --- a/automedia.py +++ b/automedia.py @@ -201,40 +201,19 @@ def get_finished_torrents(torrents): filtered_torrents.append(torrent) return filtered_torrents -def get_unfinished_torrents(torrents): - filtered_torrents = [] - for torrent in torrents: - if abs(100.0 - torrent.progress) > 0.001: - filtered_torrents.append(torrent) - return filtered_torrents - -def get_matching_torrents_by_name(torrents1, torrents2): - matching_torrents = [] +def get_non_matching_torrents_by_name(torrents1, torrents2): + non_matching_torrents = [] for torrent1 in torrents1: + found_match = False for torrent2 in torrents2: if torrent1.id == torrent2.id: - matching_torrents.append(torrent1.name) - return matching_torrents + found_match = True + break + + if not found_match: + non_matching_torrents.append(torrent1.name) -def get_html_items_progress(download_dir, tracked_html): - items = [] - for html in tracked_html: - item_dir = os.path.join(download_dir, html.title) - try: - for item in os.listdir(item_dir): - finished = os.path.isfile(os.path.join(item_dir, item, ".finished")) - items.append(HtmlItemProgress(html.title + "/" + item, finished)) - except FileNotFoundError: - pass - return items - -def get_matching_html_items_by_name(html_items1, html_items2): - matching_items = [] - for html_item1 in html_items1: - for html_item2 in html_items2: - if html_item1.name == html_item2.name: - matching_items.append(html_item1.name) - return matching_items + return non_matching_torrents def get_rss_from_episode_info(episode_name_raw, episode_info): submitter_query_text = '' @@ -496,6 +475,7 @@ def plugin_list(plugin_path, url, latest): show_notification("Plugin failed", "Failed to json decode response of plugin {}, error: {}".format(plugin_name, str(e)), urgency="critical") return None +# TODO: Make asynchronous? def plugin_download(plugin_path, url, download_dir): process = subprocess.Popen([plugin_path, "download", url, download_dir], stdout=subprocess.PIPE, stderr=subprocess.PIPE) process.communicate() @@ -569,7 +549,6 @@ def sync_html(tracked_html, download_dir, session_id): if not items: return None - # Start downloading asynchronously using url. # A file called ".in_progress" should be added to the download directory when the download is in progress. # The ".in_progress" file should contain the url that was used to download the item. # A file called ".finished" should be added to the download directory when the download has finished. @@ -590,8 +569,7 @@ def sync_html(tracked_html, download_dir, session_id): html_update_latest(html_tracked_dir, tracked_html, name, url) latest = name - if not only_show_finished_notification: - show_notification("Download started", "{}/{}".format(tracked_html.title, name)) + show_notification("Download finished", "{}/{}".format(tracked_html.title, name)) return latest def sync(rss_config_dir, html_config_dir, download_dir, sync_rate_sec): @@ -606,8 +584,7 @@ def sync(rss_config_dir, html_config_dir, download_dir, sync_rate_sec): html_tracked_dir = os.path.join(html_config_dir, "tracked") # This is also check rate for html items check_torrent_status_rate_sec = 15 - unfinished_torrents = [] - unfinished_html_items = [] + prev_finished_torrents = [] # TODO: Remove this and keep a list of "in progress" html items in memory instead. session_id = uuid.uuid4().hex @@ -648,19 +625,12 @@ def sync(rss_config_dir, html_config_dir, download_dir, sync_rate_sec): # Check torrent status with sleeping until it's time to sync rss count = 0 while count < sync_rate_sec/check_torrent_status_rate_sec: - html_items = get_html_items_progress(download_dir, tracked_html) - finished_html_items = [html_item for html_item in html_items if html_item.finished] - newly_finished_html_items = get_matching_html_items_by_name(finished_html_items, unfinished_html_items) - for newly_finished_html_item in newly_finished_html_items: - show_notification("Download finished", newly_finished_html_item) - unfinished_html_items = [html_item for html_item in html_items if not html_item.finished] - torrents = get_torrent_progress(tc) finished_torrents = get_finished_torrents(torrents) - newly_finished_torrents = get_matching_torrents_by_name(finished_torrents, unfinished_torrents) + newly_finished_torrents = get_non_matching_torrents_by_name(prev_finished_torrents, finished_torrents) for newly_finished_torrent in newly_finished_torrents: show_notification("Download finished", newly_finished_torrent) - unfinished_torrents = get_unfinished_torrents(torrents) + prev_finished_torrents = finished_torrents time.sleep(check_torrent_status_rate_sec) count += 1 -- cgit v1.2.3