-
Notifications
You must be signed in to change notification settings - Fork 1
/
rosaparser.php
376 lines (296 loc) · 13.7 KB
/
rosaparser.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
class RosaParser
{
static $url = "https://shop.rosaski.com/hotels/[[hotelId]]/?action=searching&dateFrom=[[dateStart]]&dateTo=[[dateEnd]]&adultCount=[[person]]&childCount=0&foodTypes%5B%5D=[[foodType]]";
private $foodTypes = [
1 => [
"name" => "Без завтрака",
"value" => 18170,
],
2 => [
"name" => "Завтрак",
"value" => 18168,
]
];
private $hotelIds = [
0 => [
"name" => "Radisson Роза Хутор",
"id" => 'radisson-rosa-khutor',
],
1 => [
"name" => "Park Inn Роза Хутор",
"id" => 'park-inn-by-radisson-rosa-khutor',
],
2 => [
"name" => "Mercure Роза Хутор",
"id" => 'mercure-rosa-khutor',
],
3 => [
"name" => "Golden Tulip Роза Хутор",
"id" => 'golden-tulip-rosa-khutor',
],
];
private $dateStart, $dateEnd, $interval;
private $log;
public function __construct($dateStart, $dateEnd = null, $interval)
{
include_once __DIR__ . '/lib/html_parser/simple_html_dom.php';
$this->set_dates($dateStart, $dateEnd, $interval);
$this->log = [
"source" => "rosaski.com",
"requestDate" => date("Y-m-d"),
"data" => [],
"rooms_category" => [],
];
}
public function __get($var)
{
return false;
}
public function load($filename)
{
$this->log = json_decode(file_get_contents($filename));
$this->dateStart = $this->log["requestDate"];
$this->dateEnd = $this->log["requestDate"];
$this->interval = 1;
}
/*
returns list of hotels to set proper Id to fetch data
*/
public function get_hotels()
{
return $this->hotelIds;
}
public function get_hotel_name( $id)
{
if (array_key_exists($id, $this->$hotelIds)) {
return $this->hotelIds[$id]["name"];
} else {
throw new Exception("ERROR: hotel not found. Use get_hotels() to see proper IDs");
}
}
public function set_dates($dateStart, $dateEnd = null, $interval=1)
{
$this->dateStart = $dateStart;
$this->dateEnd = $dateEnd;
$this->interval = $interval;
}
public function extract_all_data($test = false)
{
$dateStart = date_create_from_format('Y-m-d', $this->dateStart);
$dateEnd = date_create_from_format('Y-m-d', $this->dateEnd);
$foodTypes = $this->foodTypes;
$interval = $this->interval;
for ($i = clone ($dateStart); $i <= $dateEnd; $i->modify("+1 day")) {
$date2 = clone ($i);
$date2->modify("+{$interval} day");
foreach ($this->get_hotels() as $hotel) {
$hotelId = $hotel["id"];
$hotelName = $hotel["name"];
foreach ($foodTypes as $food ) {
$foodType = $food["name"];
for ($person = 1; $person < 3; $person++) {
try {
$url = str_replace(
array(
"[[hotelId]]",
"[[dateStart]]",
"[[dateEnd]]",
"[[person]]",
"[[foodType]]"
),
array(
$hotelId,
$i->format('d.m.Y'),
$date2->format('d.m.Y'),
$person,
$food["value"]
),
self::$url
);
if ($test) {
echo $url . "<BR>";
continue;
}
$path = __DIR__ . "/cache/" . md5($url) . date("Y-m-d") . ".cache";
if (!is_file($path) || filesize($path) <= 0) {
file_put_contents($path, file_get_contents($url));
}
$html = file_get_html($path);
if (!$html) {
continue;
}
foreach ($html->find('div.offersTable tbody > tr.mainTable ') as $room) {
//$tariffs = [];
$tmp = trim( $room->find('td.n1 > h3.roomName', 0)->plaintext );
$tariffs = '';
foreach ( $room->find('td.n2 > ul > li span' ) as $tariff) {
$tariffs .= $tariff->plaintext." ";
}
if (strlen($tmp) > 1) {
$room_name = $tmp;
}
$price = trim( $room->find("td.n4 > span",0)->plaintext );
$this->log["data"][$hotelName][$room_name][$person][$foodType][trim($tariffs)][$i->format("d-m-Y")]=$price;
}
} catch (Exception $e) {
}
}
}
}
}
}
public function get_log()
{
return $this->log;
}
private function normalize(array &$array, $size, $offset=0)
{
for ($i = $offset; $i < $size; $i++) {
if (!array_key_exists($i, $array)) {
$array[$i] = " ";
}
}
ksort($array);
}
public function save_to_xls()
{
include_once __DIR__ . '/lib/xls_writer/xlsxwriter.class.php';
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "res.xlsx";
header('Content-disposition: attachment; filename="' . XLSXWriter::sanitize_filename($filename) . '"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$writer = new XLSXWriter();
$writer->setAuthor('Ozon.travel vs. m.puzko');
$dateStart = date_create_from_format('Y-m-d', $this->dateStart);
$dateEnd = date_create_from_format('Y-m-d', $this->dateEnd);
$interval = $this->interval;
$header = [
0 => "Отель",
1 => "Номер",
2 => "Человек",
3 => "Питание",
4 => "Тариф"
];
//$dates = [];
for ($i = clone ($dateStart); $i <= $dateEnd; $i->modify("+1 day")) {
$header[] = $i->format("d-m-Y");
//$dates[] = $i->format("d-m-Y");
}
$writer->writeSheetRow('Sheet1', $header); //sheet name here
// $this->log["data"][$hotelName][$room_name][$person][$foodType][$i->format("d-m-Y")]=$price;
foreach ($this->log["data"] as $hotel_name => $rooms_list) {
foreach ($rooms_list as $room_name => $persons) {
foreach ($persons as $person_qty => $food_types) {
foreach ($food_types as $food_type => $tariffs) {
foreach ($tariffs as $tariff_name => $dates) {
$row = [
0 => $hotel_name,
1 => $room_name,
2 => $person_qty,
3 => $food_type,
4 => $tariff_name
];
foreach ($dates as $date => $price) {
$date_index = array_search($date, $header);
$row[$date_index] = $price;
/* foreach ($tariffs as $tariff) {
$inserted = 0;
foreach ($rows as &$row) {
if ( !array_key_exists($date_index, $row) && $inserted == 0 ) {
$row[$date_index] = $tariff["price"];
$inserted = 1;
}
}
if ( $inserted == 0 ) {
$rows[] = [
0 => $hotel_name,
1 => $room_name,
2 => $person_qty,
$date_index => $tariff["price"],
];
}
}*/
}
$this->normalize($row, count($header),5);
$writer->writeSheetRow('Sheet1', $row);
}
}
}
}
}
$writer->writeToStdOut();
exit(0);
}
public function write_data($folder = '')
{
$filename = sprintf(
"rosaski_com_req%s_start%s_end%s.json",
date("Y-m-d"),
$this->dateStart,
$this->dateEnd
);
file_put_contents(__DIR__."/{$folder}/{$filename}", json_encode($this->log));
}
public function save_to_xls_local($folder = '')
{
include_once __DIR__ . '/lib/xls_writer/xlsxwriter.class.php';
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "res".date("Y.m.d").".xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$writer = new XLSXWriter();
$writer->setAuthor('rosaski.com vs. m.puzko');
$dateStart = date_create_from_format('Y-m-d', $this->dateStart);
$dateEnd = date_create_from_format('Y-m-d', $this->dateEnd);
$interval = $this->interval;
$header = [
0 => "Отель",
1 => "Номер",
2 => "Человек",
3 => "Питание",
4 => "Тариф"
];
//$dates = [];
for ($i = clone ($dateStart); $i <= $dateEnd; $i->modify("+1 day")) {
$header[] = $i->format("d-m-Y");
//$dates[] = $i->format("d-m-Y");
}
$writer->writeSheetRow('Sheet1', $header); //sheet name here
// $this->log["data"][$hotelName][$room_name][$person][$foodType][$i->format("d-m-Y")]=$price;
foreach ($this->log["data"] as $hotel_name => $rooms_list) {
foreach ($rooms_list as $room_name => $persons) {
foreach ($persons as $person_qty => $food_types) {
foreach ($food_types as $food_type => $tariffs) {
foreach ($tariffs as $tariff_name => $dates) {
$row = [
0 => $hotel_name,
1 => $room_name,
2 => $person_qty,
3 => $food_type,
4 => $tariff_name
];
foreach ($dates as $date => $price) {
$date_index = array_search($date, $header);
$row[$date_index] = $price;
}
$this->normalize($row, count($header),5);
$writer->writeSheetRow('Sheet1', $row);
}
}
}
}
}
$writer->writeToFile(__DIR__."/{$folder}/{$filename}");
}
}