Make cookie loading a little more pythonic.

This commit is contained in:
Adam Coddington 2015-05-13 22:00:47 -07:00
parent 4daa34f310
commit 0419012e15

View file

@ -105,17 +105,27 @@ class PyiCloudService(object):
os.mkdir(self._cookie_directory) os.mkdir(self._cookie_directory)
# Set path for cookie file # Set path for cookie file
cookiefile = self.user.get('apple_id') cookiefile = os.path.join(
cookiefile = os.path.join(self._cookie_directory, ''.join([c for c in cookiefile if match(r'\w', c)])) self._cookie_directory,
''.join([c for c in self.user.get('apple_id') if match(r'\w', c)])
)
webKBCookie = None
# Check if cookie file already exists # Check if cookie file already exists
if os.path.isfile(cookiefile): try:
# Get cookie data from file # Get cookie data from file
with open(cookiefile, 'rb') as f: with open(cookiefile, 'rb') as f:
webKBCookie = pickle.load(f) webKBCookie = pickle.load(f)
self.session.cookies = requests.utils.cookiejar_from_dict(webKBCookie) self.session.cookies = requests.utils.cookiejar_from_dict(
else: webKBCookie
webKBCookie = None )
except IOError:
# This just means that the file doesn't exist; that's OK!
pass
except Exception as e:
logger.exception(
"Unexpected error occurred while loading cookies: %s" % (e, )
)
data = dict(self.user) data = dict(self.user)
data.update({'id': self.params['id'], 'extended_login': False}) data.update({'id': self.params['id'], 'extended_login': False})