-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.py
More file actions
209 lines (158 loc) · 4.87 KB
/
driver.py
File metadata and controls
209 lines (158 loc) · 4.87 KB
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
### General Depdencies ###
import sendgrid
import os
import json
import base64
from flask import request
import subprocess
import smtplib
from PIL import Image
### API Dependencies ###
import SDEClient
# TODO Database query to remove from classchecker
def enroll(onyen, password, course):
"""
Calls driver.rb to login to course.
Example: drive('kanye', 'kim', 'RAPP 101-001')
"""
print("INFO: Calling enroll for %s for %s" % (onyen, course))
args = ['ruby', 'driver.rb', onyen, password, course]
print(subprocess.call(args))
def verify_onyen(onyen, password):
"""
Returns YE for a success and NO for a failure
"""
print("INFO: Attempting to authenticate Onyen %s" % onyen)
p = subprocess.Popen(["ruby", "verifyOnyen.rb", onyen, password], stdout=subprocess.PIPE)
out, err = p.communicate()
result = out[-3:].decode('utf-8')[0:2]
if result == "YE":
return True
else:
return False
def class_checker(course):
"""
Signs special sendgrid account up for ClassChecker
Example: class_checker('CHEM 262-001')
"""
args = ['ruby', 'classchecker.rb', course]
subprocess.call(args)
def untrack(course):
"""
Works just like class_checker, but untracks the course instead.
"""
args = ['ruby', 'untrack.rb', course]
subprocess.call(args)
def send_email(recipient, subject, body, attachment=None):
"""
Sends an e-mail without an attachment using Sendgrid's V3 Web API
Example: send_email('kanye.west@live.unc.edu, 'Eighteen years', 'She got yo ass for eighteen years'
"""
sg = sendgrid.SendGridAPIClient(apikey = "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")
if attachment != None:
encoded_image = base64.b64encode(open(attachment, "rb").read()).decode('utf-8')
data = {
"personalizations": [
{
"to": [
{
"email": recipient
}
],
"subject": subject
}
],
"from": {
"email": "swap@drop.enroll"
},
"content": [
{
"type": "text/plain",
"value": body
}
],
"attachments": [
{
"content": encoded_image,
"filename": attachment,
"name": "EnrollmentResult",
"type": "png"
}
],
}
else:
data = {
"personalizations": [
{
"to": [
{
"email": recipient
}
],
"subject": subject
}
],
"from": {
"email": "swap@drop.enroll"
},
"content": [
{
"type": "text/plain",
"value": body
}
]
}
response = sg.client.mail.send.post(request_body=data)
return response.status_code, response.body, response.headers
print("SEND EMAIL TO %s" % data[personalizations][0]["to"])
def parse_email(envelope):
"""
Takes a sendgrid inbound parse e-mail object. Returns from, to, subject, body and status.
Status is a string which is either "open", "closed" or "wait list".
"""
to_address = envelope['to'][0]
from_address = envelope['from']
print("From: %s" % (from_address))
subject = request.form.get('subject')
text = request.form.get('text')
print("Subject is %s" % subject)
print("Text is %s" % text)
course, status = parse_body(text)
return from_address, to_address, subject, text, course, status
def parse_body(text):
"""
Reads an e-mail from classchecker and returns the course and status so
it's ready to go for the driver.
"""
if "to open" in text.lower():
status = "open"
elif "wait list" in text.lower():
status = "wait list"
elif "to closed" in text.lower():
status = "closed"
else:
status = "not sendgrid"
if "has changed from" in text.lower(): # Probably from coursicle
if text[8] == "H": # Honors class
course = text[0:13]
else:
course = text[0:12]
else:
course = None
return course, status
def check_color(image):
im = Image.open(image)
pix = im.load()
the_pixel = pix[551, 493]
print(the_pixel)
# If red
if the_pixel == (223, 72, 37, 255):
print("Pixel is red.")
return False
# If green
elif the_pixel == (88, 158, 19, 255):
print("Pixel is green.")
return True
else:
print("Image is neither red nor green.")
return None