-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.py
202 lines (167 loc) · 5.62 KB
/
conn.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
import requests
import time
import os
import csv
from lib import (
log_to_console,
first_date,
get_datetimes,
get_meal_plan_name,
get_session_value,
check_session_value
)
import database
from datetime import datetime
from bs4 import BeautifulSoup
from twilio.rest import Client
def verify_skey_integrity(skey):
"""Verifies the integrity of the skey."""
payload = {
'cid': 105,
'skey': skey,
'acct': 1
}
count = 0
while count < 10:
try:
response = requests.get(
"https://tigerspend.rit.edu/statementdetail.php",
payload
)
break
except ConnectionError:
# there has to be a better way to do this
# look into using something other than the
# vanilla requests library
count += 1
time.sleep(.1)
if len(response.history) != 0:
return False
return True
def force_retrieve_spending(skey, acct, format_output = 'csv', cid = 105):
"""Return user spending information.
This should be used very carefully, as it does not check for values."""
_, _, end_date = [date.strftime("%Y-%m-%d") for date in get_datetimes()]
# send TigerSpend the payload details and get CSV back
payload = {
'skey': skey,
'format': format_output,
'startdate': first_date.strftime("%Y-%m-%d"),
'enddate': end_date,
'acct': acct,
'cid': cid
}
try:
response = requests.get(
"https://tigerspend.rit.edu/statementdetail.php",
payload
)
except requests.exceptions.ConnectionError:
log_to_console("Failed to connect to TigerSpend!")
return '[]'
print (response.url)
lines = response.content.decode(response.encoding).splitlines()
reader = csv.reader(lines)
result = list(reader)
#try:
# json_result = json.loads(result)
#except json.JSONDecodeError:
# print (f'failed lol {result}')
# return '[]'
return result
def get_formatted_spending(sess_data: database.SessionData, plan_id: int):
"""Return an array of spending information in the form of Purchase items"""
response = force_retrieve_spending(sess_data.skey, plan_id)
result = []
for item in response:
if item[0] == "Date":
continue
if 'transaction' in item[0]:
return None
date = datetime.strptime(item[0], "%m/%d/%Y %I:%M%p")
location = item[1]
amount = -1 * float(item[2])
new_balance = float(item[3])
result.append(database.Purchases(
uid=sess_data.uid,
dt=date,
location=location,
amount=amount,
new_balance=new_balance,
plan_id=plan_id,
pid=time.time()
))
return result
def post_to_pings(subject_uuid, username, body):
"""POST to pings.csh.rit.edu (created by Ethan Fergussen | @ethanf108)"""
headers = {
"Authorization": "Bearer " + str(os.getenv("PINGS_TOKEN"))
}
payload = {
"username": username,
"body": body
}
try:
response = requests.post(
f"https://pings.csh.rit.edu/service/route/{subject_uuid}/ping",
timeout=7,
headers=headers,
json=payload
)
except requests.exceptions.ChunkedEncodingError:
log_to_console("Failed to access os env var 'PINGS_TOKEN'!")
return ""
return response.status_code
def get_user_plans(skey, cid=105):
"""Get a list of all the user's plans and return the first one."""
payload = {
'skey': skey,
'cid': cid
}
response = requests.get(
"https://tigerspend.rit.edu/statementnew.php",
payload
)
soup = BeautifulSoup(response.content, 'html.parser')
try:
options = soup.find(id="select-account").find_all('option')
except AttributeError:
log_to_console(f"Ran into error while finding user accounts, {response.url}")
return [( plan.attrs['value'], get_meal_plan_name(int(plan.attrs['value'])) ) for plan in options]
def get_account_info(skey, cid=105):
"""Get the user's account information from their account page."""
if check_session_value('id'):
return [
get_session_value("id"),
database.get_user(get_session_value("id")).first_name,
database.get_user(get_session_value("id")).last_name,
]
payload = {
'skey': skey,
'cid': cid
}
response = requests.get(
"https://tigerspend.rit.edu/statementnew.php", payload)
soup = BeautifulSoup(response.content, 'html.parser')
options = soup.find("div", {"class": "jsa_account-info"}).find_all("b")
name = options[0].getText().replace("'", "").split(" ")
options[1] = str(options[1]).strip("<b>")
options[1] = str(options[1]).strip("</b>") # lmao this is so cursed
account_data = [
str(name[0][0]).lower() + str(name[1]).lower() + str(options[1]).strip('X'), # account_id
str(name[0]).replace("'", ""), # first_name
str(name[1]).replace("'", ""), # last_name
]
return account_data # [account_id, first_name, last_name]
def send_twilio_message(phone_number, message):
"""Send a message to a phone number using Twilio."""
account_sid = os.getenv("TWILIO_ACCOUNT_SID")
auth_token = os.getenv("TWILIO_AUTH_TOKEN")
twilio_number = os.getenv("TWILIO_NUMBER")
client = Client(account_sid, auth_token)
message = client.messages.create(
to=f"+1{phone_number}",
from_=f"+1{twilio_number}",
body=message
)
return message