-
Notifications
You must be signed in to change notification settings - Fork 1
/
studweb.py
151 lines (128 loc) · 4.91 KB
/
studweb.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mechanize import Browser
import feide_login
import pushnotify
import shelve
import config
import parse
import mail
import re
def main():
br = Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.addheaders = config.USER_AGENT
d = shelve.open("results.cache")
for school in config.SCHOOLS:
points, courses = crawl(br, config.BASE_URL + school, school)
if not points:
continue
if config.VERBOSE:
printstdout(school, points, courses)
# We have data stored from before
# We will check if for new courses
# Data format: shelve: school = {points: num, courses: []}
if school in d:
try:
c = new_courses(courses, d[school]['courses'])
except:
c = None
if c:
# Update stored courses and points
d[school] = {} # Cleaning to be able to update
d[school]['courses'] = courses
d[school]['points'] = points
# Notify if new courses and notifications are turned on
if config.NOTIFY:
notify(c)
else:
# No data from before. Storing it
data = {}
data['points'] = points
data['courses'] = courses
d[school] = data
else:
# No data from before. Storing it
data = {}
data['points'] = points
data['courses'] = courses
d[school] = data
d.close()
def crawl(br, page, school):
response = br.open(page)
if config.FEIDE:
response, br = feide_login.browser_login(br, school)
if not response:
return None, None
else:
br.select_form("fnrForm")
br.form['fodselsnr'] = config.LOGINID
br.form['pinkode'] = config.PIN
response = br.submit() # Submit form and logg in
if school == "HiOA":
menuexpand = config.MENUCOL + 'Innsyn'
selectgrade = config.MENUEXP + 'Resultater'
logout = config.MENUEXP + 'Logg ut'
elif school == "UiO":
menuexpand = config.MENUCOL + 'Se opplysninger om deg'
selectgrade = config.MENUEXP + 'Resultater'
logout = config.MENUEXP + 'Logg ut'
response = br.follow_link(text=menuexpand) # Press the link "Innsyn"
response = br.follow_link(text=selectgrade) # Press the link "Resultater"
if config.HIDEFAILED:
response = br.select_form(nr=0)
br.form['rbVurdFilter'] = ["true"]
response = br.submit()
br.follow_link(text=logout)
return parse.parsepage(response.get_data()) # Get the grades
def printstdout(school, points, courses):
print "Courses for %s:" % school.encode('utf-8')
for course in courses:
if config.LIMITCOURSE and not \
re.compile(config.COURSEREGEX).match(course['course']):
pass
elif course['name']:
print "%s, Grade: %s, %s, %s" % (course['name'].encode('utf-8'),
course['grade'].encode('utf-8'),
course['points'].encode('utf-8'),
course['semester'].encode('utf-8'))
elif course['grade'] and course['points']:
print "Unknown course: %s, %s" % (course['grade'].encode('utf-8'),
course['points'].encode('utf-8'))
print "Total points: %s" % points.encode('utf-8')
# Printing the average grades (and hacking it into lettergrade)
grades = {'5': 'A', '4': 'B', '3': 'C', '2': 'D', '1': 'E', '0': 'F'}
average = parse.average_grade(courses)
print "Average grade: %0.2f == %s\n" % (average, grades[str(average)[0]])
def notify(new_results):
print "Notifying new courses"
if config.NOTIFY_SERVICE == 'email':
client = mail.mail()
elif config.NOTIFY_SERVICE in ['nma', 'prowl', 'pushover']:
client = pushnotify.get_client(config.NOTIFY_SERVICE,
application='StudWeb')
client.add_key(config.NOTIFY_APIKEY)
text = "New exam results: "
for result in new_results:
text = text + "Course: %s, grade: %s; " % \
(result.get('name', 'Unknown'), result.get('grade', 'Unknown'))
if config.VERBOSE:
print "Notiying new course %s" % result['name']
client.notify(text, "New exam results")
print ""
def new_courses(courses, stored_courses):
"""
Checks for coursecodes allready existing
Tobeimplemeted: checking if valid
"""
new = []
for course in courses:
if not any(c['course'] == course['course'] for c in stored_courses):
new.append(course)
return new
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print "Exited..."