forked from swentel/gentsefeesten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gf-od.php
420 lines (353 loc) · 11.6 KB
/
gf-od.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
// This converts the events from the open data website of Gent for use in the
// Android application.
// https://datatank.stad.gent/4/toerisme/gentsefeestenevents.json
// https://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json
// https://datatank.stad.gent/4/toerisme/gentsefeestencategorien.json
// Download the events file locally in a file called 'events.json'.
// You can ignore the other files, their data is small and defined in the
// arrays in the app.
// Get content.
if (!file_exists('events.json')) {
print "'events.json' file not found.\n";
exit;
}
$json = file_get_contents('events.json');
// Event uuids - ids.
if (file_exists('uuid_events.json')) {
$uuid_events = unserialize(file_get_contents('uuid_events.json'));
}
else {
$uuid_events = array();
}
$locations_sum = array();
$locations_decoded = json_decode(file_get_contents('gentsefeestenlocaties.json'));
$locations = array();
$i = 1;
foreach ($locations_decoded as $location) {
$location = (array) $location;
$locations[$location['@id']] = $location;
$locations[$location['@id']]['locatie_id'] = $i;
$locations_sum[] = array(
'id' => $i,
'name' => $location['name']->nl,
);
if (strpos($location['name']->nl, 'Baafs') !== FALSE || strpos($location['name']->nl, 'Brewery') !== FALSE || strpos($location['name']->nl, 'Stage') !== FALSE) {
print $location['name']->nl . ' - ' . $i . "\n";
}
// Salsabar, tref.club -> baudelo
//if ($i == 8 || $i == 6 || $i == 5 || $i == 88 || $i == 351 || $i == 383) {
if ($i == 4 || $i == 8 || $i == 6 || $i == 5 || $i == 88 || $i == 284 || $i == 396) {
$locations[$location['@id']]['locatie_id'] = 7;
}
// Sint jacobs
if (strpos($location['name']->nl, 'Sint-Jacobs') !== FALSE || strpos($location['name']->nl, 'Walter De Buckplein') !== FALSE) {
$locations[$location['@id']]['locatie_id'] = 4;
}
// Luisterplein
if ($i == 530) {
$locations[$location['@id']]['locatie_id'] = 14;
}
// Kinky
if (strpos($location['name']->nl, 'Kinky') !== FALSE || strpos($location['name']->nl, 'Charlatan') !== FALSE) {
$locations[$location['@id']]['locatie_id'] = 15;
}
// Special
if ($i == 723) {
$locations[$location['@id']]['locatie_id'] = 22;
}
// Sint-baafs
if ($i == 204 || $i == 203) {
$locations[$location['@id']]['locatie_id'] = 2;
}
// Willem
if ($i == 20 || $i == 21) {
$locations[$location['@id']]['locatie_id'] = 19;
}
$i++;
}
//print_r($locations_decoded);
//return;
$cats = array();
$cats_decoded = json_decode(file_get_contents('gentsefeestencategorien.json'));
$ii = 1;
foreach ($cats_decoded as $caty) {
$caty = (array) $caty;
$cats[$caty['@id']] = $caty;
$cats[$caty['@id']]['categorie_id'] = $ii;
$ii++;
}
//print_r($cats);
//file_put_contents('catssum', print_r($cats, 1));
//die();
//print_r($locations);
//print_r($locations_sum);
//file_put_contents('locationssum', print_r($locations_sum, 1));
//die();
// Debugging
$debug = isset($argv[1]) ? TRUE : FALSE;
// Decode.
$decode = json_decode($json);
// Create sql queries.
$statements = "";
//print_r($decode);
//die();
$number = 0;
$lines = array();
$unique_dates = array();
foreach ($decode as $key => $new_event) {
$array = (array) $new_event;
if (empty($new_event->startDate)) {
//print "Empty start date: " . $new_event->name->nl . "<br />";
continue;
}
if ($new_event->name->nl == '22 jaar Gentse Feesten @ Kinky Star') {
continue;
}
// The scheme is different, convert it.
$event = new stdClass();
$full_unix = strtotime($new_event->startDate);
$unix_day = strtotime(date('d-m-Y', $full_unix));
//$full_unix += 7200;
$startuur = date('G:i', $full_unix);
$einduur = '';
if (!empty($new_event->endDate)) {
$end_full_unix = strtotime($new_event->endDate);
//$end_full_unix += 7200;
$einduur = date('G:i', $end_full_unix);
}
// 'all day' is einduur 5:59 and startuur not on 6:00
if ($einduur == '5:59' && $startuur == '6:00') {
$startuur = '';
$einduur = '';
}
if ($einduur == '5:59' && $startuur != '6:00') {
$einduur = date('G:i', $full_unix);
}
// Switch date one day back.
$hour = date('G', $full_unix);
$sorting = $full_unix;
if ($hour >= 0 && $hour < 6) {
$full_unix -= 86400;
$unix_day = strtotime(date('d-m-Y', $full_unix));
}
$event->startuur = $startuur;
$event->einduur = $einduur;
$event->tijdstip_sortering = $sorting;
$event->datum = $unix_day;
$event->titel = $new_event->name->nl;
$event->omschrijving = !empty($new_event->description->nl) ? $new_event->description->nl : '';
$event->url = isset($new_event->url) ? $new_event->url : '';
$event->gratis = isset($new_event->isAccessibleForFree) ? (int) $new_event->isAccessibleForFree : 0;
$location = $new_event->location;
if (isset($locations[$location])) {
//print "location found";
$event->locatie = $locations[$location]['name']->nl;
$event->locatie_id = $locations[$location]['locatie_id'];
$event->straat = !empty($locations[$location]['address']->streetAddress) ? $locations[$location]['address']->streetAddress : '';
$event->huisnummer = '';
}
else {
$event->location = "";
$event->locatie_id = 0;
$event->straat = '';
//print "location not set\n";
}
// Ignore location id 383
if (!empty($event->locatie_id) && $event->locatie_id == 383) {
continue;
}
// ID.
$uuid = $array['@id'];
if (isset($uuid_events[$uuid])) {
$id = $uuid_events[$uuid];
}
else {
$id_number = count($uuid_events);
$id_number++;
$id = $id_number;
$uuid_events[$uuid] = $id;
}
$event->id = $id;
// Categories
$event->categorie_naam = '';
$event->categorie_id = 0;
$catje = isset($new_event->theme[0]) ? $new_event->theme[0] : 'nope';
//print $catje . "\n";
if (isset($cats[$catje])) {
$event->categorie_naam = $cats[$catje]['name']->nl;
$event->categorie_id = $cats[$catje]['categorie_id'];
}
// Price
if (!empty($new_event->offers[0]->price)) {
$event->prijs = $new_event->offers[0]->price;
}
else {
$event->prijs = '';
}
$event->prijs_vvk = '';
// Lat and long are not exported.
$event->latitude = '';
$event->longitude = '';
$event->korting = 0;
$event->festival = 0;
// Image.
$event->afbeelding = '';
/*if (!empty($new_event->image->thumbnailUrl)) {
$event->afbeelding = $new_event->image->thumbnailUrl;
}*/
if ($debug) {
//if (strpos($event->titel, 'Soul Shakers (BE)') !== FALSE) {
//if (strpos($event->titel, 'Ertebrekers') !== FALSE) {
//if (strpos($event->titel, 'Helder') !== FALSE) {
if (strpos($event->titel, 'Ponykamp') !== FALSE) {
//if (strpos($event->titel, 'SHHT') !== FALSE) {
//if (strpos($event->titel, '25e wandelzoektocht Gentse Feesten') !== FALSE) {
//if (strpos($event->titel, 'De fantastische Anna') !== FALSE) {
print_r($new_event);
print_r($event);
print "-------------------------------------------\n";
//print "$full_unix - $unix_day\n";
}
continue;
}
// The data contains too much info
if ($event->datum < 1563408000 || $event->datum > 1564272000) {
continue;
}
// Omschrijving.
// They used html this since 2015, so do some fiddling on it.
$description = $event->omschrijving;
$description = html_entity_decode($description, ENT_QUOTES);
// Replace p and br tags.
$description = str_replace(array('<p>', '<br />', '<br>', '</p>'), array('', "\n", "\n", "\n"), $description);
// Replace
$description = str_replace(' ', ' ', $description);
// Now convert our newlines.
$description = str_replace("\r", "", trim($description));
$description = str_replace("\n", "|NEWLINE|", $description);
$event->omsch = $description;
// Locatie.
$loc = isset($event->locatie) ? trim($event->locatie) : '';
if (!empty($event->straat)) {
$street = trim($event->straat);
if (!empty($event->huisnummer)) {
$street .= " " . $event->huisnummer;
}
$street = trim($street);
if ($street != $loc) {
$loc .= "\n" . $street;
}
}
$loc = str_replace("\r", "", $loc);
$loc = str_replace("\n", "|NEWLINE|", $loc);
$event->locatie = $loc;
// Prijs
if (!empty($event->prijs)) {
$event->prijs = "€ " . $event->prijs;
}
// Keep an array of unique dates.
$udate = $event->datum;
if (!isset($unique_dates[$udate])) {
$unique_dates[$udate] = date('d m Y', $udate);
}
$query = "('" . my_mysql_escape_string($event->titel) . "',";
$query .= "'" . $event->id . "',";
$query .= "'" . $event->gratis . "',";
$query .= "'" . my_mysql_escape_string($event->prijs) . "',";
$query .= "'" . my_mysql_escape_string($event->prijs_vvk) . "',";
$query .= "'" . my_mysql_escape_string($event->omsch) . "',";
//$query .= "'" . ($event->datum + 7200) . "',";
$query .= "'" . ($event->datum) . "',";
$hour_string = "";
if (!empty($event->startuur)) {
$hour_string = $event->startuur;
if (!empty($event->einduur)) {
$hour_string .= ' - ' . $event->einduur;
}
}
$query .= "'" . $hour_string . "',";
$query .= "'" . my_mysql_escape_string($event->startuur) . "',";
// Date sort is broken in so many ways. We thus take the timestamp
// and add the sort which is in the format of hhmm (wihout leading 0)
// for times before 12 and add calculate a sort which we can
// actually use for decent sorting.
$sort = $event->tijdstip_sortering;
$timestamp = 0;
$hours = 0;
if (!empty($sort)) {
//$minutes = substr($sort, -2);
//if (strlen($sort) == 3) {
// $hours = substr($sort, 0, 1);
//echo "$hours - $sort\n";
//}
//else {
// $hours = substr($sort, 0, 2);
//echo "$hours - $sort\n";
//}
// In case hours is after midnight, until 5 in the morning
// add 24 hours more for sorting.
//if ($hours == '00' || $hours < 5) {
// $hours = 24 + $hours;
//}
//$total = ($hours * 3600) + $minutes;
//$timestamp = $event->datum + $total + 7200; // + two hours because datum is in GMT.
$timestamp = $sort;
//echo "$event->datum - $hours - $sort - $total - $timestamp\n";
}
/*if ($event->id == 13235) {
print $timestamp . "\n";
die();
}*/
// Korting is an array or false.
$korting = $event->korting;
if (is_array($korting)) {
$korting = implode(', ', $korting);
}
else {
$korting = '';
}
// Media.
$media = '';
if (!empty($event->afbeelding)) {
$media = $event->afbeelding;
}
//if (!empty($event->videos) && strpos($event->videos->input, 'youtube') !== FALSE) {
// $media['video'] = $event->videos->input;
//}
$query .= "" . $timestamp . ",";
$query .= "'" . my_mysql_escape_string($event->categorie_naam) . "',";
$query .= "'" . $event->categorie_id . "',";
$query .= "'" . my_mysql_escape_string($event->url) . "',";
$query .= "'" . $event->locatie_id . "',";
$query .= "'" . my_mysql_escape_string($event->locatie) . "',";
$query .= "'" . $event->latitude . "',";
$query .= "'" . $event->longitude . "',";
$query .= "'" . my_mysql_escape_string($korting) . "',";
$query .= "'" . $event->festival . "',";
$query .= "'" . $media . "'";
$query .= ")";
if ($event->festival) {
print $event->titel . "\n";
}
//print_r($event);
$lines[] = $query;
$number++;
if ($number == 5) {
$number = 0;
$statements .= implode(":SPLIT:", $lines) . "\n";
$lines = array();
}
}
if (!empty($lines)) {
$statements .= implode(",", $lines);
}
function my_mysql_escape_string($string) {
return str_replace("'", "''", $string);
}
krsort($unique_dates);
print_r($unique_dates);
// Write to file.
file_put_contents('events-2019.data', $statements);
// Write
file_put_contents('uuid_events.json', serialize($uuid_events));