-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbandexUSP.php
210 lines (146 loc) · 4.2 KB
/
bandexUSP.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
require_once("bandex.php");
class BandexUSP extends Bandex {
public function BandexUSP() {
date_default_timezone_set('America/Sao_Paulo');
}
var $days = array(
array('segunda', 'segunda', 'segunda-feira'),
array('terca', 'ter', 'terça-feira'),
array('quarta', 'quarta', 'quarta-feira'),
array('quinta', 'quinta', 'quinta-feira'),
array('sexta', 'sexta', 'sexta'),
array('sabado', 'bado', 'sábado'),
array('domingo', 'domingo', 'domingo')
);
var $meals = array(
array('almoco', 'almo', 'almoço'),
array('jantar', 'j', 'jantar')
);
var $restaurants = array(
array('central', 'central', 'cardapio.html'),
array('fisica', 'física', 'cardapiofisica.html'),
array('prefeitura', 'prefeitura', 'cardcocesp.html'),
array('quimica', 'química', 'cardapioquimica.html'),
array('clube', 'clube da universidade', 'carddoc.html')
);
var $start_date = NULL;
const MENU_BASE_URL = 'http://www.usp.br/coseas/';
const BALANCE_AUTH_URL = 'http://uspdigital.usp.br/rucard/autenticar';
const BALANCE_EXTRACT_URL = 'http://uspdigital.usp.br/rucard/extratoListar?codmnu=12';
public function get($ids, $options = array()) {
$menu = array();
$ids = $this->treatIDs($ids);
$options = $this->sanitize($options);
foreach ($ids as $id) {
if (!is_numeric($id)) {
foreach ($this->restaurants as $rId => $r)
if ($r[0] == trim($id)) {
$id = $rId;
break;
}
}
$menu[$this->restaurants[$id][0]] = $this->prettify(
$this->parse($id),
$options
);
}
return $menu;
}
private function parse($id) {
$text = $this->curl(bandexUSP::MENU_BASE_URL . $this->restaurants[$id][2]);
preg_match(
'/semana[^\d]+([\d\/]*)[^\d]*([\d\/]*)/i',
$text,
$period
);
unset($period[0]);
for ($i = 1; $i <= count($period); $i++) {
switch (substr_count($period[$i], '/')) {
case 1:
$period[$i] .= date('\/Y');
break;
case 0:
$period[$i] .= date('\/m\/Y');
break;
}
$period[$i] = date(bandexUSP::TIME_FORMAT, strtotime(str_replace('/', '-', $period[$i])));
}
$this->start_date = $period[1];
preg_match_all(
'/<td[^>]*>(.*?)<\/td>/mis',
$text,
$td
);
$menu = array();
foreach ($td[1] as $t) {
preg_match_all(
'/<font[^>]*>(.*?)<\/font>/mis',
strip_tags(
str_replace(array('span', 'div'), 'font', $t),
'<font><br><div>'
),
$m
);
foreach ($this->days as $i => $d)
if (isset($m[1][0]) && stripos($m[1][0], $d[1]) !== FALSE)
$menu[$i][] = $m[1];
}
return $menu;
}
private function prettify($menu, $options) {
$pretty = array();
foreach ($menu as $dayId => $day) {
if (!in_array($dayId, $options['days']))
continue;
foreach ($day as $mealId => $meal) {
if (!in_array($mealId, $options['meals']))
continue;
$elems = array();
foreach ($meal as $elId => $elem)
if ($elId > 0)
$elems = array_merge(
$elems,
explode('<br>', nl2br($elem, FALSE))
);
foreach ($elems as $elId => $elem) {
if (stripos($elem, '<font') !== FALSE)
$elem = preg_replace("/<font.*?>/i", "$1", $elem);
if (strlen($elem) > 3)
$elems[$elId] = trim($elem);
}
$elems = array_filter($elems);
$dId = ($options['time_format'] != 'numeric') ?
date($options['time_format'], strtotime($this->start_date) + 24*60*60*$dayId) :
$dayId;
$mId = ($options['meal_format'] == 'name') ?
$this->meals[$mealId][0] :
$mealId;
$pretty[$dId][$mId] = ($options['implode'] == TRUE) ?
implode(bandex::IMPLODE_SUBSTR, $elems) :
$elems;
}
}
foreach ($pretty as $day)
foreach ($day as $meal)
if (is_array($meal))
array_filter($meal);
return $pretty;
}
public function balance($nusp, $pass) {
$filename = sha1(date('u') . $nusp . $pass) . '.txt';
$text = $this->curl(bandexUSP::BALANCE_AUTH_URL . '?' .
http_build_query(array('codpes' => $nusp, 'senusu' => $pass)),
$filename
);
if (stripos($text, 'extrato') === FALSE)
return FALSE;
$text = $this->curl(bandexUSP::BALANCE_EXTRACT_URL, $filename);
preg_match(
'/atual[^<]*<[^>]*>[\s]*<[^>]*>([\d]*)/mis',
$text,
$balance
);
return $balance[1];
}
}