Permalink
Browse files

Merge branch 'transmission' of https://github.com/ghuntley/maraschino

Conflicts:
	Maraschino.py
  • Loading branch information...
2 parents d22a245 + dff020e commit 17cad1e537397503a5f3a3d185d15e5fe966f0e6 @mrkipling mrkipling committed Feb 5, 2012
View
@@ -47,6 +47,7 @@
from modules.sabnzbd import *
from modules.sickbeard import *
from modules.trakt import *
+from modules.transmission import *
@app.route('/')
@requires_auth
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2008-2011 Erik Svensson <erik.public@gmail.com>
+# Licensed under the MIT license.
+
+from transmissionrpc.constants import DEFAULT_PORT, DEFAULT_TIMEOUT, PRIORITY, RATIO_LIMIT, LOGGER
+from transmissionrpc.error import TransmissionError, HTTPHandlerError
+from transmissionrpc.httphandler import HTTPHandler, DefaultHTTPHandler
+from transmissionrpc.torrent import Torrent
+from transmissionrpc.session import Session
+from transmissionrpc.client import Client
+from transmissionrpc.utils import add_stdout_logger
+
+__author__ = u'Erik Svensson <erik.public@gmail.com>'
+__version__ = u'0.8'
+__copyright__ = u'Copyright (c) 2008-2011 Erik Svensson'
+__license__ = u'MIT'

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2008-2011 Erik Svensson <erik.public@gmail.com>
+# Licensed under the MIT license.
+
+class TransmissionError(Exception):
+ """
+ This exception is raised when there has occurred an error related to
+ communication with Transmission. It is a subclass of Exception.
+ """
+ def __init__(self, message='', original=None):
+ Exception.__init__(self)
+ self._message = message
+ self.original = original
+
+ def __str__(self):
+ if self.original:
+ original_name = type(self.original).__name__
+ return '%s Original exception: %s, "%s"' % (self.message, original_name, str(self.original))
+ else:
+ return self.message
+
+class HTTPHandlerError(Exception):
+ """
+ This exception is raised when there has occurred an error related to
+ the HTTP handler. It is a subclass of Exception.
+ """
+ def __init__(self, httpurl=None, httpcode=None, httpmsg=None, httpheaders=None, httpdata=None):
+ Exception.__init__(self)
+ self.url = ''
+ self.code = 600
+ self._message = ''
+ self.headers = {}
+ self.data = ''
+ if isinstance(httpurl, (str, unicode)):
+ self.url = httpurl
+ if isinstance(httpcode, (int, long)):
+ self.code = httpcode
+ if isinstance(httpmsg, (str, unicode)):
+ self._message = httpmsg
+ if isinstance(httpheaders, dict):
+ self.headers = httpheaders
+ if isinstance(httpdata, (str, unicode)):
+ self.data = httpdata
+
+ def __repr__(self):
+ return '<HTTPHandlerError %d, %s>' % (self.code, self._message)
+
+ def __str__(self):
+ return 'HTTPHandlerError %d: %s' % (self.code, self._message)
+
+ def __unicode__(self):
+ return u'HTTPHandlerError %d: %s' % (self.code, self._message)
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2011 Erik Svensson <erik.public@gmail.com>
+# Licensed under the MIT license.
+
+import sys, httplib, urllib2
+
+from transmissionrpc.error import HTTPHandlerError
+
+class HTTPHandler(object):
+ """
+ Prototype for HTTP handling.
+ """
+ def set_authentication(self, uri, login, password):
+ """
+ Transmission use basic authentication in earlier versions and digest
+ authentication in later versions.
+
+ * uri, the authentication realm URI.
+ * login, the authentication login.
+ * password, the authentication password.
+ """
+ raise NotImplementedError("Bad HTTPHandler, failed to implement set_authentication.")
+
+ def request(self, url, query, headers, timeout):
+ """
+ Implement a HTTP POST request here.
+
+ * url, The URL to request.
+ * query, The query data to send. This is a JSON data string.
+ * headers, a dictionary of headers to send.
+ * timeout, requested request timeout in seconds.
+ """
+ raise NotImplementedError("Bad HTTPHandler, failed to implement request.")
+
+class DefaultHTTPHandler(HTTPHandler):
+ """
+ The default HTTP handler provided with transmissionrpc.
+ """
+ def __init__(self):
+ HTTPHandler.__init__(self)
+
+ def set_authentication(self, uri, login, password):
+ password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
+ password_manager.add_password(realm=None, uri=uri, user=login, passwd=password)
+ opener = urllib2.build_opener(
+ urllib2.HTTPBasicAuthHandler(password_manager)
+ , urllib2.HTTPDigestAuthHandler(password_manager)
+ )
+ urllib2.install_opener(opener)
+
+ def request(self, url, query, headers, timeout):
+ request = urllib2.Request(url, query, headers)
+ try:
+ if (sys.version_info[0] == 2 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
+ response = urllib2.urlopen(request, timeout=timeout)
+ else:
+ response = urllib2.urlopen(request)
+ except urllib2.HTTPError, error:
+ if error.fp is None:
+ raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs))
+ else:
+ raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs), error.read())
+ except urllib2.URLError, error:
+ # urllib2.URLError documentation is horrendous!
+ # Try to get the tuple arguments of URLError
+ if hasattr(error.reason, 'args') and isinstance(error.reason.args, tuple) and len(error.reason.args) == 2:
+ raise HTTPHandlerError(httpcode=error.reason.args[0], httpmsg=error.reason.args[1])
+ else:
+ raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' % (error.reason))
+ except httplib.BadStatusLine, error:
+ raise HTTPHandlerError(httpmsg='httplib.BadStatusLine: %s' % (error.line))
+ return response.read()
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2008-2011 Erik Svensson <erik.public@gmail.com>
+# Licensed under the MIT license.
+
+class Session(object):
+ """
+ Session is a class holding the session data for a Transmission daemon.
+
+ Access the session field can be done through attributes.
+ The attributes available are the same as the session arguments in the
+ Transmission RPC specification, but with underscore instead of hyphen.
+ ``download-dir`` -> ``download_dir``.
+ """
+
+ def __init__(self, fields=None):
+ self.fields = {}
+ if fields is not None:
+ self.update(fields)
+
+ def update(self, other):
+ """Update the session data from a session arguments dictionary"""
+
+ fields = None
+ if isinstance(other, dict):
+ fields = other
+ elif isinstance(other, Session):
+ fields = other.fields
+ else:
+ raise ValueError('Cannot update with supplied data')
+
+ for key, value in fields.iteritems():
+ self.fields[key.replace('-', '_')] = value
+
+ def __getattr__(self, name):
+ try:
+ return self.fields[name]
+ except KeyError:
+ raise AttributeError('No attribute %s' % name)
+
+ def __str__(self):
+ text = ''
+ for key in sorted(self.fields.keys()):
+ text += "% 32s: %s\n" % (key[-32:], self.fields[key])
+ return text
Oops, something went wrong.

0 comments on commit 17cad1e

Please sign in to comment.