aboutsummaryrefslogtreecommitdiff
path: root/src/delete-if-unlinked.py
diff options
context:
space:
mode:
authorChristian Segundo2024-07-23 21:43:05 +0200
committerChristian Segundo2024-07-23 21:43:05 +0200
commit362af4e16d7cdfa1512a6f2f154f055907b4d900 (patch)
treef0e8817df1cad49bd466c60016cdd8b70913690c /src/delete-if-unlinked.py
parent076b6aefd5b17cccc34cc403b7b4f2ba4f093a57 (diff)
downloadtransmission-hacks-362af4e16d7cdfa1512a6f2f154f055907b4d900.tar.gz
add ci
Diffstat (limited to 'src/delete-if-unlinked.py')
-rw-r--r--src/delete-if-unlinked.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/delete-if-unlinked.py b/src/delete-if-unlinked.py
new file mode 100644
index 0000000..54df8ff
--- /dev/null
+++ b/src/delete-if-unlinked.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python3
+import click
+import os
+from transmission_rpc import Client
+from datetime import timedelta, datetime
+
+
+def convert_to_seconds(s):
+ units = {"s": "seconds", "m": "minutes",
+ "h": "hours", "d": "days", "w": "weeks"}
+ count = int(s[:-1])
+ unit = units[s[-1]]
+ td = timedelta(**{unit: count})
+ return td.seconds + 60 * 60 * 24 * td.days
+
+def has_links(torrent):
+ for file in torrent.get_files():
+ file_path = torrent.download_dir + '/' + file.name
+ if not os.path.exists(file_path):
+ continue
+ if os.stat(file_path).st_nlink > 1:
+ return True
+ return False
+
+
+@click.command()
+@click.option('--port', default=9091)
+@click.option('--host', default="localhost")
+@click.option('--tag', required=True)
+@click.option('--min-age', default='1w')
+def main(host, port, tag, min_age):
+ """ Deletes torrents older than the specified age if no file has a link. """
+
+ c = Client(host=host, port=port)
+ torrents = c.get_torrents()
+ for torrent in torrents:
+ if tag not in torrent.labels:
+ continue
+
+ specified_age = convert_to_seconds(min_age)
+ age_in_seconds = int((datetime.today().timestamp() -
+ torrent.done_date.timestamp()))
+
+ if age_in_seconds < specified_age:
+ continue
+
+ if not has_links(torrent):
+ print(f"Deleting {torrent.name}")
+ c.remove_torrent(torrent.id, delete_data=True)
+
+
+if __name__ == '__main__':
+ main()