From 28c06c51a7c043fdc2226df92468540dcfd60724 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 16 Feb 2020 07:15:35 +0100 Subject: Use torrent filename instead of rss title name This fixes opening rss media where the filename doesn't match the rss feed title --- automedia.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/automedia.py b/automedia.py index 7f0dbab..c0fa961 100755 --- a/automedia.py +++ b/automedia.py @@ -116,7 +116,7 @@ def get_tracked_rss(rss_tracked_dir, existing_tracked_rss): except FileNotFoundError: return [] -def rss_update_latest(rss_tracked_dir, rss, latest, url): +def rss_update_latest(rss_tracked_dir, rss, latest, url, filename): with open(os.path.join(rss_tracked_dir, rss.title, "latest"), "w") as file: file.write(latest) @@ -125,7 +125,7 @@ def rss_update_latest(rss_tracked_dir, rss, latest, url): file.write(updated) rss.json_data["updated"] = updated - rss.json_data["downloaded"].append({ "title": latest, "time": updated, "url": url }) + rss.json_data["downloaded"].append({ "title": latest, "filename": filename, "time": updated, "url": url }) with open(os.path.join(rss_tracked_dir, rss.title, "data"), "w") as file: json.dump(rss.json_data, file, indent=4) @@ -204,13 +204,12 @@ def start_torrent_daemon(download_dir): time.sleep(0.1) return process.returncode == 0 -def add_torrent(torrent_link): - process = subprocess.Popen(["transmission-remote", "--add", torrent_link], stderr=subprocess.PIPE) - _, stderr = process.communicate() - if process.returncode != 0: - if not only_show_finished_notification: - show_notification("Download failed", "Failed to download torrent: {}, error: {}".format(torrent_link, stderr.decode('utf-8')), urgency="critical") - return process.returncode == 0 +def add_torrent(torrent_link, tc): + try: + return tc.add_torrent(torrent_link) + except transmissionrpc.TransmissionError as e: + show_notification("Download failed", "Failed to download torrent: {}, Error: {}".format(torrent_link, str(e)), urgency="critical") + return None def get_torrent_progress(tc): torrent_progress = [] @@ -408,7 +407,7 @@ def get_downloaded_item_by_title(tracked_rss, title): return None # Return the title of the newest item -def sync_rss(tracked_rss): +def sync_rss(tracked_rss, tc): rss_tracked_dir = os.path.join(rss_config_dir, "tracked") feed = feedparser.parse(tracked_rss.link) if feed.bozo == 1: @@ -437,12 +436,14 @@ def sync_rss(tracked_rss): # If it fails, there will be an attempt to add them again after next sync cycle. latest = None for item in reversed(items): + torrent = add_torrent(link, tc) + if not torrent: + return latest + title = item["title"].replace("/", "_").strip() link = item["link"] - rss_update_latest(rss_tracked_dir, tracked_rss, title, link) + rss_update_latest(rss_tracked_dir, tracked_rss, title, link, torrent.name) - if not add_torrent(link): - return latest latest = title if not only_show_finished_notification: show_notification("Download started", latest) @@ -598,7 +599,7 @@ def sync(rss_config_dir, html_config_dir, download_dir, sync_rate_sec): tracked_rss = get_tracked_rss(rss_tracked_dir, tracked_rss) for rss in tracked_rss: print("{}: rss: Syncing {}".format(str(datetime.today().isoformat()), rss.title)) - sync_rss(rss) + sync_rss(rss, tc) # Add last synced timestamp. This together with "updated" file is used to remove series # that haven't updated in a long time (either finished series or axes series) with open(os.path.join(rss_tracked_dir, rss.title, "synced"), "w") as file: @@ -777,6 +778,9 @@ def get_downloaded_items(tracked_dir, is_html): if item.get("time"): if is_html: item["title"] = os.path.join(name, item["title"]) + filename = item.get("filename") + if filename: + item["title"] = filename downloaded_items.append(item) except OSError: pass -- cgit v1.2.3