forked from Pezmc/CS-Timetable-to-iCal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetable.php
192 lines (140 loc) · 4.64 KB
/
timetable.php
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
<?php
// Needed to parse the dom
include_once('vendor/simple_html_dom.php');
//////////
function getCachedFileOrFalse($cache_file, $time=3600) {
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - $time ))) {
return file_get_contents($cache_file);
} else {
return false;
}
}
/**
* Download a url or get it from the cache
*/
function getCachedURL($url, $cache_file=null, $time=3600) {
// Choose the cache filename and folder
if(!$cache_file) $cache_file = md5($url).'.txt';
$cache_file = 'cache/'.$cache_file;
$data = getCachedFileOrFalse($cache_file, $time);
if(!$data) {
$data = file_get_contents($url);
file_put_contents($cache_file, $data, LOCK_EX);
}
return $data;
}
////////
function getCachedTimetablesList() {
$cache_file = 'cache/timetables.json';
$data = getCachedFileOrFalse($cache_file);
if($data) {
return json_decode($data, TRUE);
}
$data = getTimetableURLs();
file_put_contents($cache_file, json_encode($data), LOCK_EX);
return $data;
}
// --- Parse the timetables list into all the possible timetable combinations ---
function getTimetableURLs() {
// Download the timetable page
$timetablesHTML = getCachedURL("http://studentnet.cs.manchester.ac.uk/ugt/timetable/");
$html = str_get_html($timetablesHTML);
$timetables = array();
// Find all the lists
foreach($html->find('div[id=content] ul') as $list) {
// Only the ones with titles
$titles = $list->find('h2');
if(!empty($titles)) {
$title = "";
//$timetables[$title] = array();
// For every timetable
foreach ($list->find('li, h2') as $item) {
if($item->tag == 'h2') {
$title = $item->plaintext;
continue;
}
// For every semmester
$hrefs = array();
foreach ($item->find('a') as $a) {
$img = $a->find('img');
if(empty($img)) {
$hrefs[$a->plaintext] = $a->href;
}
}
// Get the timetable name
$name = trim($item->find('text',7)->plaintext,'[] ');
$timetables[$title][$name] = $hrefs;
}
}
}
return $timetables;
}
function getSubjectsFromTimetableHTML($timetableHTML) {
// Set a list of possible subjects
$subjectList = array();
$lecturesHTML = $timetableHTML->find('div.timetablebackground ul li');
foreach($lecturesHTML as $lectureHTML) {
$lectureHTML = explode(" ", $lectureHTML->plaintext, 2);
$subjectList[$lectureHTML[0]] = $lectureHTML[1];
}
return $subjectList;
}
function getTimetableFromTimetableHTML($timetableHTML) {
// Our timetable instance
$timetable = new Timetable();
// Extract the subject names
foreach(getSubjectsFromTimetableHTML($timetableHTML) as $id => $name) {
$timetable->addSubject($id, $name);
}
// Parse the timetable HTML
$tableHTML = $timetableHTML->find('div[id=timetabletable] table', 0);
$rowNumber = 0;
foreach($tableHTML->find('tr') as $row) {
$rowNumber++;
// Skip title, filter and day rows
if($rowNumber <= 3) continue;
// Skip notes and key
if($rowNumber >= 14) continue;
// Read in each column
$time = "";
$columnID = 0;
foreach($row->find('td') as $column) {
$columnID++;
if($columnID <= 1) {
$time = $column->plaintext;
continue;
}
foreach($column->find('div') as $lectureHTML) {
$subject = SubjectFactoryFromHTML::build($lectureHTML);
if($subject && $subject->isValid()) $timetable->addEvent($columnID-1, $time, $subject);
}
}
}
return $timetable;
}
function getCachedTimetable($url) {
$cache_file = 'cache/timetable_'.md5($url).'.txt';
// Check in our object cache first
$timetableText = getCachedFileOrFalse($cache_file);
if($timetableText && ($timetable = unserialize($timetableText)))
return $timetable;
// Download and parse a (cache) of the timetable
$timetablesHTML = getCachedURL($url);
$html = str_get_html($timetablesHTML);
// Create and cache timetable
$timetable = getTimetableFromTimetableHTML($html);
file_put_contents($cache_file, serialize($timetable));
return $timetable;
}
function getTimetableFor($year, $group, $semester) {
// Possible timetables
$timetables = getCachedTimetablesList();
// Unable to find the one the user is looking for
if(empty($timetables[$year][$group][$semester]))
throw new Exception("There is no known timetable by $year, $group, $semester...");
$url = 'http://studentnet.cs.manchester.ac.uk/ugt/timetable/'.$timetables[$year][$group][$semester];
$url = htmlspecialchars_decode($url);
// Get a timetable from cache or create from new
return getCachedTimetable($url);
}
?>