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:
Sasha MOREL 2016-05-23 09:23:26 +02:00
parent 9ef7b163a1
commit 7745571fcd
1 changed files with 48 additions and 21 deletions

View File

@ -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)
print ('init with username ' + username) if username != None:
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
self.autologin = autologin if self.username != None and self.password != None:
self.cookies = CookieJar () self.autologin = autologin
else:
self.autologin = False
if cookies == None:
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
''' '''
return (name + ' ' + version + '\nUser: ' + self.username + '\nDebug: ' + str (self.DEBUG)) if self.username != None:
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,20 +168,24 @@ 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
''' '''
self.opener.open (self.auth_referer) if self.username != None and self.password != None:
data = self.auth_extra_fields self.opener.open (self.auth_referer)
data [self.auth_user_field] = self.username data = self.auth_extra_fields
data [self.auth_pass_field] = self.password data [self.auth_user_field] = self.username
data = bytes (urlencode (data), 'UTF8') data [self.auth_pass_field] = self.password
if self.DEBUG: data = bytes (urlencode (data), 'UTF8')
print ('Cookies ' + str( len(self.cookies))) if self.DEBUG:
print ('Sending data ' + str (data)) print ('Cookies ' + str( len(self.cookies)))
a = self.opener.open (self.auth_post_url, data = data) print ('Sending data ' + str (data))
if self.DEBUG: a = self.opener.open (self.auth_post_url, data = data)
print ('Auth redirected to ' + a.geturl ()) if self.DEBUG:
return (a.geturl () == self.base_url + self.auth_ok) print ('Auth redirected to ' + a.geturl ())
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