45
45
timetable_url = 'https://my.unsw.edu.au/active/studentTimetable/timetable.xml'
46
46
47
47
def getFlow (full_path ):
48
- print full_path
49
48
return OAuth2WebServerFlow (client_id = '52070605511-3e5l10hi90c8t4t3foa0aptmhe5psgsr.apps.googleusercontent.com' ,
50
49
client_secret = 'aKejP602Oz7Axrump73Oh1_R' ,
51
50
scope = 'https://www.googleapis.com/auth/calendar' ,
@@ -54,15 +53,23 @@ def getFlow(full_path):
54
53
def getGoogleRedirect (full_path ):
55
54
return getFlow (full_path ).step1_get_authorize_url ()
56
55
57
- def getTimetable (zUser , zPass ):
56
+ def getTimetable (zUser , zPass , semester ):
58
57
jar = cookielib .CookieJar ()
59
58
opener = urllib2 .build_opener (urllib2 .HTTPCookieProcessor (jar ))
60
59
# CSRF Token or something. We need to steal it from SSO
61
60
# into our cookie jar to go open the timetable page
62
61
stupid_thing = re .findall (r'_cNoOpConversation.*?"' , opener .open (login_url ).read ())[0 ].replace ('"' , '' )
63
62
data = {'username' : zUser , 'password' : zPass , '_eventId' : 'submit' , 'lt' : stupid_thing }
64
63
opener .open (login_url , urllib .urlencode (data ))
65
- return opener .open (timetable_url ).read ()
64
+ data = {}
65
+ if semester :
66
+ source = opener .open (timetable_url ).read ().replace ('\n ' , '' )
67
+ if "sectionHeading" not in source :
68
+ return ''
69
+ s = BeautifulSoup (source )
70
+ bsds = s .find ('input' , {'name' : 'bsdsSequence' })['value' ]
71
+ data = {'term' : semester , 'bsdsSubmit-commit' : 'Get Timetable' , 'bsdsSequence' : bsds }
72
+ return opener .open (timetable_url , urllib .urlencode (data )).read ()
66
73
67
74
def CreateClassEvent (title , content , where , start_time , end_time ):
68
75
event = {
@@ -80,22 +87,36 @@ def CreateClassEvent(title, content, where, start_time, end_time):
80
87
}
81
88
return event
82
89
83
- def export (f , source , zu , zp , code , full_path ):
84
- if f == 'use-login' and (not zu or not zp ):
85
- return "No zPass details or timetable source"
90
+ def getSemester (zu , zp ):
91
+ source = getTimetable (zu , zp , None )
92
+ if "sectionHeading" not in source :
93
+ return ("Bad timetable source, possibly incorrect login details or myunsw daily dose of downtime (12am-2am or whatever)" , {})
94
+
95
+ # parsing shit
96
+ s = BeautifulSoup (source .replace ("\n " ,"" ))
97
+ select_html = s .find ("select" , {'name' : 'term' })
98
+ select_html ['style' ] = 'width: 400px;'
99
+ return (None ,
100
+ {
101
+ 'semester_select_html' : select_html .prettify (),
102
+ 'source' : source
103
+ })
86
104
87
- if f == 'use-login' :
88
- print "getting timetable"
89
- f = getTimetable (zu , zp )
90
- print "got timetable!"
91
- else :
92
- f = source
105
+
106
+ def export (source , code , full_path ):
107
+ f = source .replace ('\r ' , '' )
93
108
94
109
if "sectionHeading" not in f :
95
110
return "Bad timetable source, possibly incorrect login details or myunsw daily dose of downtime (12am-2am or whatever)"
96
111
97
112
# parsing shit
98
113
s = BeautifulSoup (f .replace ("\n " ,"" ))
114
+ sem = re .sub (r'.*Semester (\d+) \S\S(\d+).*' , u'\\ 2s\\ 1' , s .find ("option" , {'selected' :'true' }).text )
115
+ title = sem + " Timetable"
116
+
117
+ if not re .match (r'\d\ds\d' , sem ):
118
+ current_time = datetime .datetime .now ()
119
+ sem = '%ds%d' % (current_time .year % 100 , 1 if current_time .month < 7 else 2 )
99
120
100
121
credentials = getFlow (full_path ).step2_exchange (code )
101
122
http = httplib2 .Http ()
@@ -108,8 +129,6 @@ def export(f, source, zu, zp, code, full_path):
108
129
####################################################
109
130
zp = ''
110
131
111
- sem = re .sub (u'.*Semester (\S+) \S\S(\S+).*' , u'\\ 2s\\ 1' , s .find ("option" , {'selected' :'true' }).text )
112
- title = sem + " Timetable"
113
132
114
133
115
134
# make gcal calendar
@@ -186,3 +205,7 @@ def export(f, source, zu, zp, code, full_path):
186
205
187
206
print "Probably success!"
188
207
208
+
209
+ def exportByScraping (zu , zp , semester , code , full_path ):
210
+ source = getTimetable (zu , zp , None )
211
+ return export (source , code , full_path )
0 commit comments