Jump to content

Help with script to add Torrent File


Recommended Posts

So this code actually works, it will actually add the torrent to be downloaded.  However, the request responds back as failed.  Any ideas what could be causing this behavior?

{u'build': 25110}
{"build":25110,"error": "Unable to add torrent"}

import os
import requests
import re

def getAuthToken():
	UTORRENT_URL = 'http://%s:%s/gui/' % ('SERVER', 'PORT')
	REGEX_UTORRENT_TOKEN = r'<div[^>]*id=[\"\']token[\"\'][^>]*>([^<]*)</div>'

	auth = requests.auth.HTTPBasicAuth('USER', 'PASSWORD')
	r = requests.get(UTORRENT_URL_TOKEN, auth=auth)
	token = re.search(REGEX_UTORRENT_TOKEN, r.text).group(1)
	return token;

def addTorrent(fileLocation):
	token = getAuthToken()
	UTORRENT_URL = 'http://%s:%s/gui/' % ('SERVER', 'PORT')
	auth = requests.auth.HTTPBasicAuth('USER', 'PASSWORD')
	params = {'action':'add-file','token': token}
	files = {'torrent_file': open(fileLocation, 'r')}
	r = requests.post(url=UTORRENT_URL, auth=auth, params=params, files=files)
	print r.text
	print r.status_code
	print r.json()

def processDirectory(strDirectory):
	for file in os.listdir(strDirectory):
		if file.endswith(".torrent"):
			fullpath = strDirectory + "/" + file
			addTorrent(fileLocation = fullpath)
			#os.remove(strDirectory+ "/" + file)
processDirectory(strDirectory = "E:/Users1/Indigo/Downloads")


Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...