-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaidvantage.py
359 lines (295 loc) · 12.4 KB
/
aidvantage.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
"""Aidvantage scraper.
This module provides a class for scraping data from the Aidvantage website.
It uses Selenium to interact with the website and Pandas to store the data.
Usage:
av = Aidvantage(username, password, ssn, dob)
loans = av.get_account_details()
transactions = av.get_transactions(loans[list(loans.keys())[0]].name)
print(transactions)
Requirements:
- selenium
- pandas
- requests
- attrs
- chromedriver (installed and in your PATH)
Example:
from aidvantage import Aidvantage
from os import environ
av = Aidvantage(
username=environ["AIDVANTAGE_USER"],
password=environ["AIDVANTAGE_PASS"],
ssn=environ["AIDVANTAGE_SSN"],
dob=environ["AIDVANTAGE_DOB"]
)
loans = av.get_account_details()
transactions = av.get_transactions(loans[list(loans.keys())[0]].name)
print(transactions)
"""
from contextlib import suppress
from decimal import Decimal
from enum import Enum
from attrs import define, field
from pandas import DataFrame
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support import expected_conditions
def balance_to_float(balance: str) -> Decimal:
"""Converts the Aidvantage balance to a decimal."""
return Decimal(balance.lstrip("$").replace(',', ''))
def apr_to_float(apr: str) -> Decimal:
"""Converts the Aidvantage interest rate to a decimal."""
return Decimal(float(apr.rstrip("%")) / 100)
@define
class LoanDetails:
"""Data class representing a loan's detail summary."""
name: str = field(alias='Loan')
balance: Decimal = field(alias="CurrentBalance", converter=balance_to_float)
apr: Decimal = field(alias='InterestRate', converter=apr_to_float)
due_date: str = field(alias='DueDate')
@define
class PageDetail:
"""Data class representing a page within the website."""
matching_text: str
link_text: str | None
@define
class UserLogin:
"""Data class representing a user's login."""
username: str
password: str
ssn: str
dob: str
class Aidvantage:
"""Web scraper for the Aidvantage website."""
class CurrentPage(Enum):
"""Various pages within the website."""
HOME_PAGE = PageDetail("Welcome to Aidvantage!", "")
GOV_DISCLAIMER = PageDetail(
"You are accessing a U.S. Federal Government computer system", None)
LOGIN_PAGE = PageDetail("Forgot User ID Forgot Password", "Log in")
ADDITIONAL_INFO = PageDetail(
"Please provide the information below so we can verify your account", None)
ACCOUNT_SUMMARY = PageDetail(
(
"This is an attempt to collect a debt and any information obtained will "
"be used for that purpose"
),
"Account Summary")
ACCOUNT_HISTORY = PageDetail(
(
"The information contained on this page is current as of the day "
"the information is requested"
),
"Account History"
)
LOAN_DETAILS = PageDetail("All Loan Details", "Loan Details")
UNKNOWN = PageDetail("Not a known page type.", None)
EXPIRED = PageDetail("Your session has expired.", None)
@staticmethod
def get_current_page(driver: WebDriver) -> "Aidvantage.CurrentPage":
"""Gets the current page from the driver."""
page_text = driver.find_element(By.TAG_NAME, 'body').text
for page_choice in Aidvantage.CurrentPage:
assert isinstance(page_choice.value, PageDetail)
if page_choice.value.matching_text in page_text:
return page_choice
return Aidvantage.CurrentPage.UNKNOWN
@staticmethod
def go_to_page(driver: WebDriver, page: "Aidvantage.CurrentPage") -> None:
"""Navigates to the specified page."""
if Aidvantage.CurrentPage.get_current_page(driver) == page:
return
if page.value.link_text is None:
raise ValueError(f"{page} has no link text.")
driver.find_element(By.PARTIAL_LINK_TEXT, page.value.link_text).click()
def __init__(
self, login: UserLogin, driver: WebDriver | None = None
) -> None:
self.__username: str = login.username
self.__password: str = login.password
self.__ssn: str = login.ssn
self.__dob: str = login.dob
self.home_page = "https://aidvantage.studentaid.gov"
if driver is None:
driver = webdriver.Chrome()
self.driver: WebDriver = driver
# Manual call to get things going.
self.driver.get(self.home_page)
self.driver.implicitly_wait(5)
def __del__(self) -> None:
"""Cleans up the driver."""
self.driver.quit()
def go_to_page(self, page: "Aidvantage.CurrentPage") -> None:
"""Navigates to the specified page."""
Aidvantage.CurrentPage.go_to_page(self.driver, page)
self._do_filler_steps()
def get_account_balances(self) -> dict[str, Decimal]:
"""Gets the account balances for all loans."""
loans = self.get_account_details()
return {name: loan.balance for name, loan in loans.items()}
def get_transactions(self, loan: str) -> DataFrame:
"""Gets the transaction history for a given loan.
Args:
loan (str): The name of the loan to get transactions for.
Returns:
DataFrame: A DataFrame containing the transaction history.
"""
wait = WebDriverWait(self.driver, 10) # Wait up to 10 seconds
# Get to the right page.
if (
Aidvantage.CurrentPage.get_current_page(self.driver)
is not Aidvantage.CurrentPage.ACCOUNT_HISTORY
):
self._require_login()
self.go_to_page(Aidvantage.CurrentPage.ACCOUNT_SUMMARY)
# Find recent payments section, then account history.
elem = self.driver.find_element(By.ID, "divRecentPayments")
elem.find_element(By.PARTIAL_LINK_TEXT, "Account History").click()
# Display history by Loan.
for elem_id, visible_text in [
("SelctedHistType", "By Loan"),
("ddl_Loan", loan),
("SelectedDateRange", "Life of Loan"),
]:
elem = self.driver.find_element(By.ID, elem_id)
wait.until(expected_conditions.visibility_of_element_located((By.ID, elem_id)))
Select(elem).select_by_visible_text(visible_text)
# Parse unpaid principle column from table.
return self._get_table_from_page("tblByLoans")
def get_account_details(self) -> dict[str, LoanDetails]:
"""Get the loan details of every loan."""
self._require_login()
Aidvantage.CurrentPage.go_to_page(self.driver, Aidvantage.CurrentPage.LOAN_DETAILS)
# Get account table.
table = self.driver.find_element(By.ID, "tblAllLoanDetails")
# Get table header.
header_list = []
header_elem = table.find_element(By.TAG_NAME, "thead").find_elements(By.TAG_NAME, "th")
for item in header_elem:
header_list.append(item.text.replace(' ', ''))
# Rows
loans = {}
rows = table.find_element(By.TAG_NAME, "tbody").find_elements(By.TAG_NAME, "tr")
for row in rows:
# Align headers to columns.
data_list = [td.text for td in row.find_elements(By.TAG_NAME, 'td')]
data_dict = dict(zip(header_list, data_list))
loans[data_dict['Loan']] = LoanDetails(**data_dict)
return loans
def _is_logged_in(self) -> bool:
"""Checks if the user is logged in."""
current_page = Aidvantage.CurrentPage.get_current_page(self.driver)
if current_page in [
Aidvantage.CurrentPage.EXPIRED,
Aidvantage.CurrentPage.LOGIN_PAGE,
Aidvantage.CurrentPage.HOME_PAGE,
]:
return False
if current_page in [
Aidvantage.CurrentPage.ACCOUNT_HISTORY,
Aidvantage.CurrentPage.ACCOUNT_SUMMARY,
Aidvantage.CurrentPage.ADDITIONAL_INFO,
Aidvantage.CurrentPage.LOAN_DETAILS,
]:
return True
if current_page in [
Aidvantage.CurrentPage.GOV_DISCLAIMER,
Aidvantage.CurrentPage.ADDITIONAL_INFO,
]:
self._do_filler_steps()
return True
# Normal page without login
with suppress(NoSuchElementException):
if self.driver.find_element(By.LINK_TEXT, 'Log in'):
return False
# Login expired.
with suppress(NoSuchElementException):
if self.driver.find_element(By.LINK_TEXT, "Account Summary"):
return True
for id_string in [
"user-id", # On the login page.
"account-number" # Additional info page.
]:
with suppress(NoSuchElementException):
if self.driver.find_elements(By.ID, id_string):
return False
# No rules match.
raise ValueError("Unknown login state.")
def _require_login(self):
if self._is_logged_in():
return
# Go to login page. Link exists on most pages.
self.go_to_page(Aidvantage.CurrentPage.LOGIN_PAGE)
# Fill-in user/pass.
elem = self.driver.find_element(By.ID, 'user-id')
elem.send_keys(self.__username)
elem = self.driver.find_element(By.ID, 'password')
elem.send_keys(self.__password)
# Click login.
elem = self.driver.find_element(By.ID, 'Submit')
elem.click()
if (
Aidvantage.CurrentPage.get_current_page(self.driver)
is Aidvantage.CurrentPage.ADDITIONAL_INFO
):
# Fill-in social-security number.
elem = self.driver.find_element(By.ID, "lblSSN1")
elem.send_keys(self.__ssn)
# Fill-in date of birth.
elem = self.driver.find_element(By.ID, 'dob1')
elem.send_keys(self.__dob)
# Submit
elem = self.driver.find_element(By.ID, 'Submit')
elem.click()
# Must be logged in by this point.
if not self._is_logged_in():
raise RuntimeError
def _do_filler_steps(self) -> None:
"""If there was a filler step, do it before returning."""
choices = {
self.CurrentPage.GOV_DISCLAIMER: self._accept_gov_comp_access,
}
while (current_page := Aidvantage.CurrentPage.get_current_page(self.driver)) in choices:
choices[current_page]()
def _accept_gov_comp_access(self) -> None:
accept_button = self.driver.find_element(By.ID, "Accept")
accept_button.click()
def _get_table_from_page(self, table_id: str) -> DataFrame:
# Get table.
table = self.driver.find_element(By.ID, table_id)
# Get table header.
header_list = []
header_elem = table.find_element(By.TAG_NAME, "thead").find_elements(By.TAG_NAME, "th")
for item in header_elem:
header_list.append(item.text.replace(' ', ''))
# Rows
data: dict[str, list] = {header: [] for header in header_list}
rows = table.find_element(By.TAG_NAME, "tbody").find_elements(By.TAG_NAME, "tr")
for row in rows:
# Align headers to columns.
data_list = [td.text for td in row.find_elements(By.TAG_NAME, 'td')]
# Align rows to data.
while len(data_list) > len(header_list):
if data_list[0] == '':
data_list.pop(0)
elif data_list[-1] == '':
data_list.pop(-1)
else:
raise ValueError("Columns do not match rows.")
if data_list == ['']:
continue
if len(data_list) != len(header_list):
assert False
data_dict = dict(zip(header_list, data_list))
for key, value in data_dict.items():
data[key].append(value)
return DataFrame(data)
@staticmethod
def _download_as_text(url: str) -> str:
response = requests.get(url, timeout=20)
response.raise_for_status() # Check for download errors
# Process the content in memory
return response.text