Log out when finished
This commit is contained in:
committed by
Ruud
parent
c29a8b47d6
commit
afea12c7c0
@@ -57,31 +57,26 @@ class SynologyRPC(object):
|
|||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.session_name = "DownloadStation"
|
self.session_name = "DownloadStation"
|
||||||
if username and password:
|
|
||||||
if self._login() == True:
|
|
||||||
log.info( ">Login ok" )
|
|
||||||
else:
|
|
||||||
log.error( ">Login failed" )
|
|
||||||
elif username or password:
|
|
||||||
log.error('User or password missing, not using authentication.')
|
|
||||||
|
|
||||||
def _login(self):
|
def _login(self):
|
||||||
args = {'api': 'SYNO.API.Auth', 'account': self.username, 'passwd': self.password, 'version': 2,
|
if self.username and self.password:
|
||||||
|
args = {'api': 'SYNO.API.Auth', 'account': self.username, 'passwd': self.password, 'version': 2,
|
||||||
'method': 'login', 'session': self.session_name, 'format': 'sid'}
|
'method': 'login', 'session': self.session_name, 'format': 'sid'}
|
||||||
response = self._req(self.Authurl, args)
|
response = self._req(self.Authurl, args)
|
||||||
if response['success'] == True:
|
if response['success'] == True:
|
||||||
self.sid = response['data']['sid']
|
self.sid = response['data']['sid']
|
||||||
log.info( "Sid=%s", self.sid)
|
log.debug( "Sid=%s", self.sid)
|
||||||
return response['success']
|
return response
|
||||||
|
elif username or password:
|
||||||
|
log.error('User or password missing, not using authentication.')
|
||||||
|
return False
|
||||||
|
|
||||||
def _logout(self):
|
def _logout(self):
|
||||||
args = {'api':'SYNO.API.Auth', 'version':1, 'method':'logout', 'session':self.session_name}
|
args = {'api':'SYNO.API.Auth', 'version':1, 'method':'logout', 'session':self.session_name, '_sid':self.sid}
|
||||||
response = self._req(self.Authurl, args)
|
return self._req(self.Authurl, args)
|
||||||
return response['success']
|
|
||||||
|
|
||||||
def _req(self, url, args):
|
def _req(self, url, args):
|
||||||
req_url = url + '?' + urllib.urlencode(args)
|
req_url = url + '?' + urllib.urlencode(args)
|
||||||
#print "Trying URL: ", req_url
|
|
||||||
try:
|
try:
|
||||||
req_open = urllib2.urlopen(req_url)
|
req_open = urllib2.urlopen(req_url)
|
||||||
response = json.loads(req_open.read())
|
response = json.loads(req_open.read())
|
||||||
@@ -100,7 +95,16 @@ class SynologyRPC(object):
|
|||||||
|
|
||||||
def add_torrent_uri(self, torrent):
|
def add_torrent_uri(self, torrent):
|
||||||
log.info( "Adding torrent URL %s", torrent)
|
log.info( "Adding torrent URL %s", torrent)
|
||||||
args = {'api':'SYNO.DownloadStation.Task', 'version':1, 'method':'create', 'uri':torrent, '_sid':self.sid}
|
response = {}
|
||||||
response = self._req(self.DLurl, args)
|
# login
|
||||||
|
login = self._login()
|
||||||
|
if len(login) > 0 and login['success'] == True:
|
||||||
|
log.info("Login success, adding torrent")
|
||||||
|
args = {'api':'SYNO.DownloadStation.Task', 'version':1, 'method':'create', 'uri':torrent, '_sid':self.sid}
|
||||||
|
response = self._req(self.DLurl, args)
|
||||||
|
self._logout()
|
||||||
|
else:
|
||||||
|
log.error("Couldn't login to Synology, %s", login)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user