Cookies Import and Export
Python3: - Added cookies import and export - Changed HomeSFR.login () behavior if there's no username and password - Changed HomeSFR.__str__ () if there's no username - Added HomeSFR.get_cookies () - Set autologin always to False is there no credential to use
This commit is contained in:
parent
9ef7b163a1
commit
7745571fcd
|
@ -11,13 +11,13 @@ This is a wrap aroud website, this could stop working without prior notice
|
||||||
### Get image
|
### Get image
|
||||||
### Get video
|
### Get video
|
||||||
## Manage logs
|
## Manage logs
|
||||||
## Import/Export cookies
|
## Logout method
|
||||||
|
|
||||||
authors = (
|
authors = (
|
||||||
'Gilles "Almtesh" Émilien MOREL',
|
'Gilles "Almtesh" Émilien MOREL',
|
||||||
)
|
)
|
||||||
name = 'pyhomesfr'
|
name = 'pyhomesfr'
|
||||||
version = '0.8-20160521'
|
version = '0.9-20160523'
|
||||||
|
|
||||||
# Settable modes
|
# Settable modes
|
||||||
MODE_OFF = 0
|
MODE_OFF = 0
|
||||||
|
@ -62,11 +62,13 @@ def bytes2image (b):
|
||||||
return (r)
|
return (r)
|
||||||
|
|
||||||
class HomeSFR ():
|
class HomeSFR ():
|
||||||
def __init__ (self, username, password, debug = False, autologin = True):
|
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
|
||||||
'''
|
'''
|
||||||
Sets the class with username and password
|
Sets the class with username and password couple, or cookies
|
||||||
|
Both user/password and cookies can be set, the cookies will be used first
|
||||||
The debug parameter defines if the class will write debug messages to stdout, if False, the stdout will never be writen by the class
|
The debug parameter defines if the class will write debug messages to stdout, if False, the stdout will never be writen by the class
|
||||||
The autologin parameter defines if the class will manage the login by itself, if False, the user must call login () to login and test_login () to check the login
|
The autologin parameter defines if the class will manage the login by itself, if False, the user must call login () to login and test_login () to check the login
|
||||||
|
The autologin paramater will always be False if no username and password are defined, and the login () method will always return False
|
||||||
'''
|
'''
|
||||||
self.DEBUG = debug
|
self.DEBUG = debug
|
||||||
if self.DEBUG:
|
if self.DEBUG:
|
||||||
|
@ -74,14 +76,25 @@ class HomeSFR ():
|
||||||
print ('Authors:')
|
print ('Authors:')
|
||||||
for i in authors:
|
for i in authors:
|
||||||
print (' - ' + i)
|
print (' - ' + i)
|
||||||
|
if username != None:
|
||||||
print ('init with username ' + username)
|
print ('init with username ' + username)
|
||||||
|
if cookies != None:
|
||||||
|
print ('init with cookies')
|
||||||
print ('debug = ' + str (debug))
|
print ('debug = ' + str (debug))
|
||||||
print ('autologin = ' + str (autologin))
|
print ('autologin = ' + str (autologin))
|
||||||
|
|
||||||
|
if (username == None or password == None) and cookies == None:
|
||||||
|
raise TypeError ('You must define either username AND password or cookies')
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
|
if self.username != None and self.password != None:
|
||||||
self.autologin = autologin
|
self.autologin = autologin
|
||||||
|
else:
|
||||||
|
self.autologin = False
|
||||||
|
if cookies == None:
|
||||||
self.cookies = CookieJar ()
|
self.cookies = CookieJar ()
|
||||||
|
else:
|
||||||
|
self.cookies = cookies
|
||||||
self.opener = request.build_opener (request.HTTPCookieProcessor (self.cookies))
|
self.opener = request.build_opener (request.HTTPCookieProcessor (self.cookies))
|
||||||
|
|
||||||
# Specific configuration
|
# Specific configuration
|
||||||
|
@ -129,7 +142,10 @@ class HomeSFR ():
|
||||||
'''
|
'''
|
||||||
Shows name, version, defined user and debug state
|
Shows name, version, defined user and debug state
|
||||||
'''
|
'''
|
||||||
|
if self.username != None:
|
||||||
return (name + ' ' + version + '\nUser: ' + self.username + '\nDebug: ' + str (self.DEBUG))
|
return (name + ' ' + version + '\nUser: ' + self.username + '\nDebug: ' + str (self.DEBUG))
|
||||||
|
else:
|
||||||
|
return (name + ' ' + version + '\nUser: Unknown (auth from cookie class)\nDebug: ' + str (self.DEBUG))
|
||||||
|
|
||||||
def test_login (self):
|
def test_login (self):
|
||||||
'''
|
'''
|
||||||
|
@ -152,8 +168,10 @@ class HomeSFR ():
|
||||||
'''
|
'''
|
||||||
Logs in the HomeBySFR service
|
Logs in the HomeBySFR service
|
||||||
Call this function first or exception will be raised
|
Call this function first or exception will be raised
|
||||||
Return True if the login was a success, False either
|
Returns True if the login was a success, False either
|
||||||
|
This method will always return False if no username and password are defined
|
||||||
'''
|
'''
|
||||||
|
if self.username != None and self.password != None:
|
||||||
self.opener.open (self.auth_referer)
|
self.opener.open (self.auth_referer)
|
||||||
data = self.auth_extra_fields
|
data = self.auth_extra_fields
|
||||||
data [self.auth_user_field] = self.username
|
data [self.auth_user_field] = self.username
|
||||||
|
@ -166,6 +184,8 @@ class HomeSFR ():
|
||||||
if self.DEBUG:
|
if self.DEBUG:
|
||||||
print ('Auth redirected to ' + a.geturl ())
|
print ('Auth redirected to ' + a.geturl ())
|
||||||
return (a.geturl () == self.base_url + self.auth_ok)
|
return (a.geturl () == self.base_url + self.auth_ok)
|
||||||
|
else:
|
||||||
|
return (False)
|
||||||
|
|
||||||
def set_mode (self, mode):
|
def set_mode (self, mode):
|
||||||
'''
|
'''
|
||||||
|
@ -265,6 +285,13 @@ class HomeSFR ():
|
||||||
r.append (self.get_sensor (i))
|
r.append (self.get_sensor (i))
|
||||||
return (tuple (r))
|
return (tuple (r))
|
||||||
|
|
||||||
|
def get_cookies (self):
|
||||||
|
'''
|
||||||
|
Returns the CookieJar as it is now, for further use
|
||||||
|
It's strongly recommended to use this method only before a object delete
|
||||||
|
'''
|
||||||
|
return (self.cookies)
|
||||||
|
|
||||||
class Sensor:
|
class Sensor:
|
||||||
'''
|
'''
|
||||||
Class used to read easily the sensors
|
Class used to read easily the sensors
|
||||||
|
|
Loading…
Reference in New Issue