Skip to content

Instantly share code, notes, and snippets.

@idolpx
Created May 3, 2020 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idolpx/28acca12a3137355e1e776e473bb8eae to your computer and use it in GitHub Desktop.
Save idolpx/28acca12a3137355e1e776e473bb8eae to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# Jackett .magnet file loader for Transmission
# - Sometimes Jackett can't get a torrent file to save to the watch folder
# - and instead will save a .magnet file containing the magnet link.
# - Transmission does not recognize this file so it ignores it.
# - This script will feed it to Transmission via RPC.
# - Setup a cron job to run it every so often and you are good to go.
#
# author: Jaime Idolpx (jaime@idolpx.com)
# date: 5.2.2020
import urllib.request
import json
import os
# Transmission RPC URL
tm_url = "http://192.168.1.220:9091/transmission/rpc"
# Transmission watch folder where Jackett saves .magnet files
tm_watch = "/mnt/scratch/transmission/watch"
############################
tm_session = {
"method": "session-get"
}
tm_add = {
"arguments": {
"filename": "",
"download-dir": ""
},
"method": "torrent-add",
}
# Send Magnet Link to Transmission
def magnetAdd(magnetLink, downloadDir):
req = urllib.request.Request(tm_url)
req.add_header('X-Transmission-Session-Id', tm_session_id)
tm_add['arguments']['filename'] = magnetLink
tm_add['arguments']['download-dir'] = downloadDir
data = json.dumps(tm_add)
response = urllib.request.urlopen(req, data.encode('utf-8'))
print(data)
# Get X-Transmission-Session-Id
req = urllib.request.Request(tm_url)
try: urllib.request.urlopen(req).read()
except urllib.error.URLError as e:
tm_session_id = e.info()['X-Transmission-Session-Id']
# Get Transmission Download Dir
req = urllib.request.Request(tm_url)
req.add_header('X-Transmission-Session-Id', tm_session_id)
data = json.dumps(tm_session)
response = urllib.request.urlopen(req, data.encode('utf-8'))
tm_session = json.loads(response.read())
tm_ddir = tm_session['arguments']['download-dir']
# Find .magnet files and send to Transmission
for root, dirs, files in os.walk(tm_watch):
for file in files:
if file.endswith(".magnet"):
inFile = open(os.path.join(root, file), 'r')
magnet = inFile.read()
download_dir = tm_ddir + root.replace(tm_watch, '')
inFile.close()
print("path: {}, file: {}".format(root, file))
print("download: {}".format(download_dir))
print("-----------------------------------")
magnetAdd(magnet, download_dir)
os.rename(os.path.join(root, file), os.path.join(root, file) + '.added')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment