fixing some lint errors
This commit is contained in:
@@ -9,13 +9,14 @@ import urllib2
|
|||||||
import httplib
|
import httplib
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
log = CPLog(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class TransmissionRPC(object):
|
class TransmissionRPC(object):
|
||||||
|
|
||||||
"""TransmissionRPC lite library"""
|
"""TransmissionRPC lite library"""
|
||||||
|
|
||||||
|
log = CPLog(__name__)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
host='localhost',
|
host='localhost',
|
||||||
@@ -41,7 +42,7 @@ class TransmissionRPC(object):
|
|||||||
'couchpotato-transmission-client/1.0')]
|
'couchpotato-transmission-client/1.0')]
|
||||||
urllib2.install_opener(opener)
|
urllib2.install_opener(opener)
|
||||||
elif username or password:
|
elif username or password:
|
||||||
log.debug('User or password missing, not using authentication.'
|
self.log.debug('User or password missing, not using authentication.'
|
||||||
)
|
)
|
||||||
self.session = self.get_session()
|
self.session = self.get_session()
|
||||||
|
|
||||||
@@ -54,62 +55,62 @@ class TransmissionRPC(object):
|
|||||||
try:
|
try:
|
||||||
open_request = urllib2.urlopen(request)
|
open_request = urllib2.urlopen(request)
|
||||||
response = json.loads(open_request.read())
|
response = json.loads(open_request.read())
|
||||||
log.debug('response: '
|
self.log.debug('response: '
|
||||||
+ str(json.dumps(response).encode('utf-8')))
|
+ str(json.dumps(response).encode('utf-8')))
|
||||||
if response['result'] == 'success':
|
if response['result'] == 'success':
|
||||||
log.debug(u'Transmission action successfull')
|
self.log.debug(u'Transmission action successfull')
|
||||||
return response['arguments']
|
return response['arguments']
|
||||||
else:
|
else:
|
||||||
log.debug('Unknown failure sending command to Transmission. Return text is: '
|
self.log.debug('Unknown failure sending command to Transmission. Return text is: '
|
||||||
+ response['result'])
|
+ response['result'])
|
||||||
return False
|
return False
|
||||||
except httplib.InvalidURL, e:
|
except httplib.InvalidURL, err:
|
||||||
log.error(u'Invalid Transmission host, check your config %s'
|
self.log.error(u'Invalid Transmission host, check your config %s'
|
||||||
% e)
|
% err)
|
||||||
return False
|
return False
|
||||||
except urllib2.HTTPError, e:
|
except urllib2.HTTPError, err:
|
||||||
if e.code == 401:
|
if err.code == 401:
|
||||||
log.error(u'Invalid Transmission Username or Password, check your config'
|
self.log.error(u'Invalid Transmission Username or Password, check your config'
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
elif e.code == 409:
|
elif err.code == 409:
|
||||||
msg = str(e.read())
|
msg = str(err.read())
|
||||||
try:
|
try:
|
||||||
self.session_id = \
|
self.session_id = \
|
||||||
re.search('X-Transmission-Session-Id:\s*(\w+)',
|
re.search('X-Transmission-Session-Id:\s*(\w+)',
|
||||||
msg).group(1)
|
msg).group(1)
|
||||||
log.debug('X-Transmission-Session-Id: '
|
self.log.debug('X-Transmission-Session-Id: '
|
||||||
+ self.session_id)
|
+ self.session_id)
|
||||||
|
|
||||||
# #resend request with the updated header
|
# #resend request with the updated header
|
||||||
|
|
||||||
return self._request(ojson)
|
return self._request(ojson)
|
||||||
except:
|
except:
|
||||||
log.error(u'Unable to get Transmission Session-Id %s'
|
self.log.error(u'Unable to get Transmission Session-Id %s'
|
||||||
% e)
|
% err)
|
||||||
else:
|
else:
|
||||||
log.error(u'TransmissionRPC HTTPError: %s' % e)
|
self.log.error(u'TransmissionRPC HTTPError: %s' % err)
|
||||||
except urllib2.URLError, e:
|
except urllib2.URLError, err:
|
||||||
log.error(u'Unable to connect to Transmission %s' % e)
|
self.log.error(u'Unable to connect to Transmission %s' % err)
|
||||||
|
|
||||||
def get_session(self):
|
def get_session(self):
|
||||||
post_data = {'method': 'session-get', 'tag': self.tag}
|
post_data = {'method': 'session-get', 'tag': self.tag}
|
||||||
return self._request(post_data)
|
return self._request(post_data)
|
||||||
|
|
||||||
def add_torrent_uri(self, torrent, arguments={}):
|
def add_torrent_uri(self, torrent, arguments):
|
||||||
arguments['filename'] = torrent
|
arguments['filename'] = torrent
|
||||||
post_data = {'arguments': arguments, 'method': 'torrent-add',
|
post_data = {'arguments': arguments, 'method': 'torrent-add',
|
||||||
'tag': self.tag}
|
'tag': self.tag}
|
||||||
return self._request(post_data)
|
return self._request(post_data)
|
||||||
|
|
||||||
def add_torrent_file(self, torrent, arguments={}):
|
def add_torrent_file(self, torrent, arguments):
|
||||||
arguments['metainfo'] = torrent
|
arguments['metainfo'] = torrent
|
||||||
post_data = {'arguments': arguments, 'method': 'torrent-add',
|
post_data = {'arguments': arguments, 'method': 'torrent-add',
|
||||||
'tag': self.tag}
|
'tag': self.tag}
|
||||||
return self._request(post_data)
|
return self._request(post_data)
|
||||||
|
|
||||||
def set_torrent(self, id, arguments={}):
|
def set_torrent(self, torrent_id, arguments):
|
||||||
arguments['ids'] = id
|
arguments['ids'] = torrent_id
|
||||||
post_data = {'arguments': arguments, 'method': 'torrent-set',
|
post_data = {'arguments': arguments, 'method': 'torrent-set',
|
||||||
'tag': self.tag}
|
'tag': self.tag}
|
||||||
return self._request(post_data)
|
return self._request(post_data)
|
||||||
@@ -118,11 +119,12 @@ class TransmissionRPC(object):
|
|||||||
class Transmission(Downloader):
|
class Transmission(Downloader):
|
||||||
|
|
||||||
type = ['torrent', 'magnet']
|
type = ['torrent', 'magnet']
|
||||||
|
log = CPLog(__name__)
|
||||||
|
|
||||||
def download(
|
def download(
|
||||||
self,
|
self,
|
||||||
data={},
|
data,
|
||||||
movie={},
|
movie,
|
||||||
manual=False,
|
manual=False,
|
||||||
filedata=None,
|
filedata=None,
|
||||||
):
|
):
|
||||||
@@ -132,14 +134,14 @@ class Transmission(Downloader):
|
|||||||
or not self.isCorrectType(data.get('type')):
|
or not self.isCorrectType(data.get('type')):
|
||||||
return
|
return
|
||||||
|
|
||||||
log.debug('Sending "%s" to Transmission.', data.get('name'))
|
self.log.debug('Sending "%s" to Transmission.', data.get('name'))
|
||||||
log.debug('Type "%s" to Transmission.', data.get('type'))
|
self.log.debug('Type "%s" to Transmission.', data.get('type'))
|
||||||
|
|
||||||
# Load host from config and split out port.
|
# Load host from config and split out port.
|
||||||
|
|
||||||
host = self.conf('host').split(':')
|
host = self.conf('host').split(':')
|
||||||
if not isInt(host[1]):
|
if not isInt(host[1]):
|
||||||
log.error('Config properties are not filled in correctly, port is missing.'
|
self.log.error('Config properties are not filled in correctly, port is missing.'
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -153,23 +155,28 @@ class Transmission(Downloader):
|
|||||||
) else 1)}
|
) else 1)}
|
||||||
|
|
||||||
if not filedata or data.get('type') != 'magnet':
|
if not filedata or data.get('type') != 'magnet':
|
||||||
log.error('Failed sending torrent, no data')
|
self.log.error('Failed sending torrent, no data')
|
||||||
|
|
||||||
# Send request to Transmission
|
# Send request to Transmission
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tc = TransmissionRPC(host[0], port=host[1],
|
trpc = TransmissionRPC(host[0], port=host[1],
|
||||||
username=self.conf('username'),
|
username=self.conf('username'),
|
||||||
password=self.conf('password'))
|
password=self.conf('password'))
|
||||||
if data.get('type') == 'magnet' or data.get('magnet') != None:
|
if data.get('type') == 'magnet' or data.get('magnet') \
|
||||||
remote_torrent = tc.add_torrent_uri(data.get('magnet'), arguments=params)
|
!= None:
|
||||||
|
remote_torrent = trpc.add_torrent_uri(data.get('magnet'),
|
||||||
|
arguments=params)
|
||||||
else:
|
else:
|
||||||
remote_torrent = tc.add_torrent_file(b64encode(filedata), arguments=params)
|
remote_torrent = \
|
||||||
|
trpc.add_torrent_file(b64encode(filedata),
|
||||||
|
arguments=params)
|
||||||
|
|
||||||
# Change settings of added torrents
|
# Change settings of added torrents
|
||||||
tc.set_torrent(remote_torrent['torrent-added']['hashString'
|
|
||||||
|
trpc.set_torrent(remote_torrent['torrent-added']['hashString'
|
||||||
], torrent_params)
|
], torrent_params)
|
||||||
return True
|
return True
|
||||||
except Exception, e:
|
except Exception, err:
|
||||||
log.error('Failed to change settings for transfer: %s', e)
|
self.log.error('Failed to change settings for transfer: %s', err)
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user