Skip to content

Instantly share code, notes, and snippets.

@bbolli
Created August 31, 2010 18:43
Show Gist options
  • Save bbolli/559493 to your computer and use it in GitHub Desktop.
Save bbolli/559493 to your computer and use it in GitHub Desktop.
def submit_to_transmission(self, enclosure):
"""Submit the enclosure to the Transmission RPC interface"""
conn = httplib.HTTPConnection('localhost:9091')
# the href must not be URL-encoded, or Transmission will barf
url = '/transmission/rpc?method=torrent-add&filename=' + enclosure.href
headers = {'User-Agent': 'fetcher/0.3 (+http://drbeat.li/py/)'}
self.log('Submitting "%s" to Transmission' % enclosure.href)
self.log('* Url: %s' % url, 1)
if self.dry_run:
return
while True:
try:
self.log('* Headers: %r' % headers, 2)
conn.request('GET', url, headers=headers)
response = conn.getresponse()
self.log(response.getheaders(), 3)
reply = response.read()
self.log(reply, 3)
except (httplib.HTTPException, socket.error), e:
return self.log('! during submission: %s' % e)
if response.status == 200: # ok
break
elif response.status == 409: # Transmission's CSRF avoidance
h = 'X-Transmission-Session-Id'
headers[h] = response.getheader(h)
else: # error
return self.log('! %d %s\n%s\n\n%s' % (
response.status, response.reason,
'\n'.join(['%s: %s' % hv for hv in response.getheaders()]),
reply
))
try:
reply = eval(reply, {}, {})
except Exception, e:
self.log(reply)
reply = {'result': 'exception: %s' % e}
if reply['result'] == 'success':
return True
self.log(reply)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment