This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
session.py
366 lines (293 loc) · 11.7 KB
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
"""
Manage the LendingClub user session and all raw HTTP calls to the LendingClub site.
This will almost always be accessed through the API calls in
:class:`lendingclub.LendingClub` instead of directly.
"""
"""
The MIT License (MIT)
Copyright (c) 2013 Jeremy Gillick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import re
import requests
import getpass
import time as time
from bs4 import BeautifulSoup
from requests.exceptions import *
class Session:
email = None
__pass = None
__logger = None
last_response = None
session_timeout = 10
""" Minutes until the session expires.
The session will attempt to reauth before the next HTTP call after timeout."""
base_url = 'https://www.lendingclub.com/'
""" The root URL that all paths are appended to """
last_request_time = 0
""" The timestamp of the last HTTP request """
__session = None
def __init__(self, email=None, password=None, logger=None):
self.email = email
self.__pass = password
self.__logger = logger
def __log(self, message):
"""
Log a debugging message
"""
if self.__logger:
self.__logger.debug(message)
def __continue_session(self):
"""
Check if the time since the last HTTP request is under the
session timeout limit. If it's been too long since the last request
attempt to authenticate again.
"""
now = time.time()
diff = abs(now - self.last_request_time)
timeout_sec = self.session_timeout * 60 # convert minutes to seconds
if diff >= timeout_sec:
self.__log('Session timed out, attempting to authenticate')
self.authenticate()
def set_logger(self, logger):
"""
Have the Session class send debug logging to your python logging logger.
Set to None stop the logging.
Parameters
----------
logger : `Logger <http://docs.python.org/2/library/logging.html>`_
The logger to send debug output to.
"""
self.__logger = logger
def build_url(self, path):
"""
Build a LendingClub URL from a URL path (without the domain).
Parameters
----------
path : string
The path part of the URL after the domain. i.e. https://www.lendingclub.com/<path>
"""
url = '{0}{1}'.format(self.base_url, path)
url = re.sub('([^:])//', '\\1/', url) # Remove double slashes
return url
def authenticate(self, email=None, password=None):
"""
Authenticate with LendingClub and preserve the user session for future requests.
This will raise an exception if the login appears to have failed, otherwise it returns True.
Since Lending Club doesn't seem to have a login API, the code has to try to decide if the login
worked or not by looking at the URL redirect and parsing the returned HTML for errors.
Parameters
----------
email : string
The email of a user on Lending Club
password : string
The user's password, for authentication.
Returns
-------
boolean
True on success or throws an exception on failure.
Raises
------
session.AuthenticationError
If authentication failed
session.NetworkError
If a network error occurred
"""
# Get email and password
if email is None:
email = self.email
else:
self.email = email
if password is None:
password = self.__pass
else:
self.__pass = password
# Get them from the user
if email is None:
email = raw_input('Email:')
self.email = email
if password is None:
password = getpass.getpass()
self.__pass = password
self.__log('Attempting to authenticate: {0}'.format(self.email))
# Start session
self.__session = requests.Session()
self.__session.headers = {
'Referer': 'https://www.lendingclub.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31'
}
# Set last request time to now
self.last_request_time = time.time()
# Send login request to LC
payload = {
'login_email': email,
'login_password': password
}
response = self.post('/account/login.action', data=payload, redirects=False)
# Get URL redirect URL and save the last part of the path as the endpoint
response_url = response.url
if response.status_code == 302:
response_url = response.headers['location']
endpoint = response_url.split('/')[-1]
# Debugging
self.__log('Status code: {0}'.format(response.status_code))
self.__log('Redirected to: {0}'.format(response_url))
self.__log('Cookies: {0}'.format(str(response.cookies.keys())))
# Show query and data that the server received
if 'x-echo-query' in response.headers:
self.__log('Query: {0}'.format(response.headers['x-echo-query']))
if 'x-echo-data' in response.headers:
self.__log('Data: {0}'.format(response.headers['x-echo-data']))
# Parse any errors from the HTML
soup = BeautifulSoup(response.text, "html5lib")
errors = soup.find(id='master_error-list')
if errors:
errors = errors.text.strip()
# Remove extra spaces and newlines from error message
errors = re.sub('\t+', '', errors)
errors = re.sub('\s*\n+\s*', ' * ', errors)
if errors == '':
errors = None
# Raise error
if errors is not None:
raise AuthenticationError(errors)
# Redirected back to the login page...must be an error
if endpoint == 'login.action':
raise AuthenticationError('Unknown! Redirected back to the login page without an error message')
return True
def is_site_available(self):
"""
Returns true if we can access LendingClub.com
This is also a simple test to see if there's a network connection
Returns
-------
boolean
True or False
"""
try:
response = requests.head(self.base_url)
status = response.status_code
return 200 <= status < 400 # Returns true if the status code is greater than 200 and less than 400
except Exception:
return False
def request(self, method, path, query=None, data=None, redirects=True):
"""
Sends HTTP request to LendingClub.
Parameters
----------
method : {GET, POST, HEAD, DELETE}
The HTTP method to use: GET, POST, HEAD or DELETE
path : string
The path that will be appended to the domain defined in :attr:`base_url`.
query : dict
A dictionary of query string parameters
data : dict
A dictionary of POST data values
redirects : boolean
True to follow redirects, False to return the original response from the server.
Returns
-------
requests.Response
A `requests.Response <http://docs.python-requests.org/en/latest/api/#requests.Response>`_ object
"""
# Check session time
self.__continue_session()
try:
url = self.build_url(path)
method = method.upper()
self.__log('{0} request to: {1}'.format(method, url))
if method == 'POST':
request = self.__session.post(url, params=query, data=data, allow_redirects=redirects)
elif method == 'GET':
request = self.__session.get(url, params=query, data=data, allow_redirects=redirects)
elif method == 'HEAD':
request = self.__session.head(url, params=query, data=data, allow_redirects=redirects)
elif method == 'DELETE':
request = self.__session.delete(url, params=query, data=data, allow_redirects=redirects)
else:
raise SessionError('{0} is not a supported HTTP method'.format(method))
self.last_response = request
self.__log('Status code: {0}'.format(request.status_code))
# Update session time
self.last_request_time = time.time()
except (RequestException, ConnectionError, TooManyRedirects, HTTPError) as e:
raise NetworkError('{0} failed to: {1}'.format(method, url), e)
except Timeout:
raise NetworkError('{0} request timed out: {1}'.format(method, url), e)
return request
def post(self, path, query=None, data=None, redirects=True):
"""
POST request wrapper for :func:`request()`
"""
return self.request('POST', path, query, data, redirects)
def get(self, path, query=None, redirects=True):
"""
GET request wrapper for :func:`request()`
"""
return self.request('GET', path, query, None, redirects)
def head(self, path, query=None, data=None, redirects=True):
"""
HEAD request wrapper for :func:`request()`
"""
return self.request('HEAD', path, query, None, redirects)
def clear_session_order(self):
"""
Clears any existing order in the LendingClub.com user session.
"""
self.get('/portfolio/confirmStartNewPortfolio.action')
def json_success(self, json):
"""
Check the JSON response object for the success flag
Parameters
----------
json : dict
A dictionary representing a JSON object from lendingclub.com
"""
if type(json) is dict and 'result' in json and json['result'] == 'success':
return True
return False
class SessionError(Exception):
"""
Base exception class for :mod:`lendingclub.session`
Parameters
----------
value : string
The error message
origin : Exception
The original exception, if this exception was caused by another.
"""
value = 'Unknown error'
origin = None
def __init__(self, value, origin=None):
self.value = value
self.origin = origin
def __str__(self):
if self.origin is None:
return repr(self.value)
else:
return '{0} (from {1})'.format(repr(self.value), repr(self.origin))
class AuthenticationError(SessionError):
"""
Authentication failed
"""
pass
class NetworkError(SessionError):
"""
An error occurred while making an HTTP request
"""
pass