kamegami

cleanrtorrent.py

Sep 25th, 2020
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. script to manage rtorrent based on Sonarr status
  4. """
  5.  
  6. import xmlrpclib
  7. import ssl
  8. from subprocess import call
  9. import time
  10. import requests
  11.  
  12. def main():
  13.     # Create an object to represent our server. Use the login information in the XMLRPC Login Details section here.
  14.     server_url = "@@https://user:pw@rtorrent-ip:rpc-port/RPC2@@";
  15.     server = xmlrpclib.Server(server_url, context=ssl._create_unverified_context());
  16.     label = "@@tv-sonarr@@" #rtorrent label #
  17.     api_key = "@@api-key@@"
  18.     sonarr_url = "@@http://sonarr-ip:port/api@@"
  19.     #grab Sonarr history
  20.     res = requests.get("{}/history".format(sonarr_url),
  21.                        params={"apikey":api_key,"sortKey":"date","pageSize":50,"sortDir":"desc" })
  22.     history = res.json()
  23.  
  24.     # Get torrents in the main view
  25.     mainview = server.download_list("", "main")
  26.     delete_list = list()
  27.  
  28.     #loops through all torrents,
  29.     for torrent in mainview:
  30.         if server.d.custom1(torrent) == label and is_complete(server.d.name(torrent), history):
  31.             delete_list.append({"torrent":torrent,
  32.                                 "dir":dir_norm(server.d.base_path(torrent)),
  33.                                 "name":server.d.name(torrent)})
  34.     #remove torrent from rtorrent then delete dl directory
  35.     for dl in delete_list:
  36.         print dl
  37.         server.d.erase(dl["torrent"])
  38.         time.sleep(20)
  39.         call(["rm","-R",dl["dir"]])
  40.  
  41. #translate docker path to linux path
  42. def dir_norm(dir):
  43.     return dir.replace("/data/", "@@/dl/folder/path/@@", 1)
  44.  
  45. #matches torrent to sonarr records and
  46. #returns true if import complete
  47. def is_complete(name, history):
  48.     for item in history["records"]:
  49.         if item["eventType"] == "downloadFolderImported":
  50.             if item["sourceTitle"] in name:
  51.                 print name
  52.                 return True
  53.     return False
  54.  
  55. if __name__ == "__main__":
  56.     # execute only if run as a script
  57.     main()
  58.  
Add Comment
Please, Sign In to add comment