Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- script to manage rtorrent based on Sonarr status
- """
- import xmlrpclib
- import ssl
- from subprocess import call
- import time
- import requests
- def main():
- # Create an object to represent our server. Use the login information in the XMLRPC Login Details section here.
- server_url = "@@https://user:pw@rtorrent-ip:rpc-port/RPC2@@";
- server = xmlrpclib.Server(server_url, context=ssl._create_unverified_context());
- label = "@@tv-sonarr@@" #rtorrent label #
- api_key = "@@api-key@@"
- sonarr_url = "@@http://sonarr-ip:port/api@@"
- #grab Sonarr history
- res = requests.get("{}/history".format(sonarr_url),
- params={"apikey":api_key,"sortKey":"date","pageSize":50,"sortDir":"desc" })
- history = res.json()
- # Get torrents in the main view
- mainview = server.download_list("", "main")
- delete_list = list()
- #loops through all torrents,
- for torrent in mainview:
- if server.d.custom1(torrent) == label and is_complete(server.d.name(torrent), history):
- delete_list.append({"torrent":torrent,
- "dir":dir_norm(server.d.base_path(torrent)),
- "name":server.d.name(torrent)})
- #remove torrent from rtorrent then delete dl directory
- for dl in delete_list:
- print dl
- server.d.erase(dl["torrent"])
- time.sleep(20)
- call(["rm","-R",dl["dir"]])
- #translate docker path to linux path
- def dir_norm(dir):
- return dir.replace("/data/", "@@/dl/folder/path/@@", 1)
- #matches torrent to sonarr records and
- #returns true if import complete
- def is_complete(name, history):
- for item in history["records"]:
- if item["eventType"] == "downloadFolderImported":
- if item["sourceTitle"] in name:
- print name
- return True
- return False
- if __name__ == "__main__":
- # execute only if run as a script
- main()
Add Comment
Please, Sign In to add comment