-
Notifications
You must be signed in to change notification settings - Fork 0
/
urfu_timetable.py
163 lines (150 loc) · 3.7 KB
/
urfu_timetable.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
# -*- coding: utf8 -*-
import urllib2
import urllib
from urlparse import urlparse
from HTMLParser import HTMLParser
BASE_HOST="http://urfu.ru/"
BASE_URL="http://urfu.ru/student/schedule/"
class UstuScheduleParser(HTMLParser):
@staticmethod
def parse(url):
f=urllib2.urlopen(url)
s=f.read()
f.close()
fp=UstuScheduleParser()
fp.feed(s)
fp.close()
return fp.matches
def __init__(self):
HTMLParser.__init__(self)
self.matches={}
self.in_div=0
self.capture=None
self.last=None
self.level=0
def handle_starttag(self, tag, attrs):
tag = tag.lower()
attrs=dict(attrs)
if tag=='div':
if "tx_ustu_schedule" in attrs.get("class",''):
self.in_div=1
return
elif self.in_div==1:
self.level+=1
if self.in_div!=1: return
if tag=='a':
self.capture=''
self.last=attrs['href']
def handle_data(self,data):
if self.capture is not None:
self.capture+=data
def handle_endtag(self,tag):
if self.in_div!=1: return
tag = tag.lower()
if tag=='div':
if self.level==0:
self.in_div=2
else:
self.level-=1
return
if tag=='a':
self.matches[self.capture.decode('utf8').strip()]=self.last
self.capture=None
self.last=None
def colName(classes):
return [x.strip()[3:] for x in classes.split() if x.startswith('td-')][0]
COLUMNS='npair time sbname type initials aname'.split()
def rowDictToList(row):
return tuple(row.get(k,'?') for k in COLUMNS)
class TimetableParser(HTMLParser):
@staticmethod
def parse(url):
f=urllib2.urlopen(url)
s=f.read()
f.close()
fp=TimetableParser()
fp.feed(s)
fp.close()
return fp.matches
def __init__(self):
HTMLParser.__init__(self)
self.matches={}
self.in_div=0
self.level=0
self.colaccum=None
self.last=None
self.capture=None
self.row=None
def handle_starttag(self, tag, attrs):
tag = tag.lower()
attrs=dict(attrs)
if tag=='div':
if "tx_ustu_schedule" in attrs.get("class",''):
self.in_div=1
return
elif self.in_div==1:
self.level+=1
if self.in_div!=1: return
if tag=='h1':
self.capture=''
self.last=None
elif tag=='tr':
self.row={}
elif tag=='td':
name=colName(attrs['class'])
self.row[None]=name
self.capture=''
if name=='npair':
self.colaccum={}
if 'rowspan' in attrs:
self.colaccum[None]=name
def handle_data(self,data):
if self.capture is not None:
self.capture+=data
def handle_endtag(self,tag):
if self.in_div!=1: return
tag = tag.lower()
if tag=='div':
if self.level==0:
self.in_div=2
else:
self.level-=1
return
if tag=='h1':
self.matches[self.capture.decode('utf8').strip()]=self.last=[]
self.capture=None
elif tag=='tr':
for key in self.colaccum:
if key not in self.row:
self.row[key]=self.colaccum[key]
self.last.append(self.row)
self.row=None
elif tag=='td':
name=self.row[None]
value=self.capture.decode('utf8').strip()
self.row[name]=value
if None in self.colaccum:
self.colaccum[name]=value
del self.colaccum[None]
self.capture=None
del self.row[None]
def getFaculties():
return UstuScheduleParser.parse(BASE_URL)
def getFacultyGroupList(url):
return UstuScheduleParser.parse(url)
def getGroupTimetable1(url):
return TimetableParser.parse(url)
def main():
print 'Downloading faculty list'
faculties=getFaculties()
for name,url in faculties.iteritems():
if u'РТФ' in name:
print 'downloading RTF group list'
for name,url in getFacultyGroupList(BASE_HOST+url).iteritems():
if '220207' in name:
print 'downloading 220207 group timetable'
for day,rows in getGroupTimetable1(BASE_HOST+url).iteritems():
print '====',day
for row in rows:
print '\t'.join(rowDictToList(row))
if __name__=="__main__":main()