2014-03-10 05:18:05 +00:00
# Author: jkaberg <joel.kaberg@gmail.com>, based on fuzemans work (https://github.com/RuudBurger/CouchPotatoServer/blob/develop/couchpotato/core/downloaders/rtorrent/main.py)
# URL: http://code.google.com/p/sickbeard/
#
2014-11-12 16:43:14 +00:00
# This file is part of SickGear.
2014-03-10 05:18:05 +00:00
#
2014-11-12 16:43:14 +00:00
# SickGear is free software: you can redistribute it and/or modify
2014-03-10 05:18:05 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2014-11-12 16:43:14 +00:00
# SickGear is distributed in the hope that it will be useful,
2014-03-10 05:18:05 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2014-11-12 16:43:14 +00:00
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
import xmlrpclib
from sickbeard import helpers , TORRENT_LABEL , TORRENT_PATH
2014-03-10 05:18:05 +00:00
from sickbeard . clients . generic import GenericClient
from lib . rtorrent import RTorrent
2016-11-13 00:39:23 +00:00
class RtorrentAPI ( GenericClient ) :
2014-03-10 05:18:05 +00:00
def __init__ ( self , host = None , username = None , password = None ) :
2016-12-09 15:41:49 +00:00
if host and host . startswith ( ' scgi: ' ) :
2016-11-13 00:39:23 +00:00
username = password = None
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
super ( RtorrentAPI , self ) . __init__ ( ' rTorrent ' , host , username , password )
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
# self.url = self.host
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
def _get_auth ( self ) :
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
self . auth = None
if self . host :
try :
2016-12-09 15:41:49 +00:00
if self . host and self . host . startswith ( ' scgi: ' ) :
2016-11-13 00:39:23 +00:00
self . username = self . password = None
self . auth = RTorrent ( self . host , self . username , self . password , True )
except ( AssertionError , xmlrpclib . ProtocolError ) as e :
pass
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
return self . auth
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
def _add_torrent ( self , cmd , * * kwargs ) :
torrent = None
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
if self . auth :
try :
# Send magnet to rTorrent
if ' file ' == cmd :
torrent = self . auth . load_torrent ( kwargs [ ' file ' ] )
elif ' magnet ' == cmd :
2016-12-09 15:41:49 +00:00
torrent = self . auth . load_magnet ( kwargs [ ' uri ' ] , kwargs [ ' btih ' ] )
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
if torrent :
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
if TORRENT_LABEL :
torrent . set_custom ( 1 , TORRENT_LABEL )
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
if TORRENT_PATH :
torrent . set_directory ( TORRENT_PATH )
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
torrent . start ( )
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
except ( StandardError , Exception ) as e :
pass
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
return any ( [ torrent ] )
2014-03-10 05:18:05 +00:00
def _add_torrent_file ( self , result ) :
2016-11-13 00:39:23 +00:00
if result :
return self . _add_torrent ( ' file ' , file = result . content )
return False
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
def _add_torrent_uri ( self , result ) :
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
if result :
return self . _add_torrent ( ' magnet ' , uri = result . url , btih = result . hash )
return False
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
#def _set_torrent_ratio(self, name):
2014-03-10 05:18:05 +00:00
# if not name:
2014-03-25 05:57:24 +00:00
# return False
2014-03-10 05:18:05 +00:00
#
# if not self.auth:
2014-03-25 05:57:24 +00:00
# return False
2014-03-10 05:18:05 +00:00
#
# views = self.auth.get_views()
#
# if name not in views:
2014-03-25 05:57:24 +00:00
# self.auth.create_group(name)
2014-03-10 05:18:05 +00:00
# group = self.auth.get_group(name)
# ratio = int(float(sickbeard.TORRENT_RATIO) * 100)
#
# try:
2014-03-25 05:57:24 +00:00
# if ratio > 0:
#
# # Explicitly set all group options to ensure it is setup correctly
# group.set_upload('1M')
# group.set_min(ratio)
# group.set_max(ratio)
# group.set_command('d.stop')
# group.enable()
# else:
# # Reset group action and disable it
# group.set_command()
# group.disable()
2014-03-10 05:18:05 +00:00
#
# except:
2014-03-25 05:57:24 +00:00
# return False
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
# return True
2014-03-10 05:18:05 +00:00
2016-03-09 11:16:02 +00:00
def test_authentication ( self ) :
2014-03-10 05:18:05 +00:00
try :
self . _get_auth ( )
2016-11-13 00:39:23 +00:00
if None is self . auth :
return False , ' Error: Unable to get %s authentication, check your config! ' % self . name
return True , ' Success: Connected and Authenticated '
except ( StandardError , Exception ) :
return False , ' Error: Unable to connect to %s ' % self . name
2014-03-25 05:57:24 +00:00
2014-03-10 05:18:05 +00:00
2016-11-13 00:39:23 +00:00
api = RtorrentAPI ( )