Repository navigation
Expand file tree
/
Copy pathingolstadt-brightyoga.pl
More file actions
executable file
·259 lines (209 loc) · 7.91 KB
/
Copy pathingolstadt-brightyoga.pl
File metadata and controls
executable file
·259 lines (209 loc) · 7.91 KB
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
#!/usr/bin/perl
# 2013,2018,2022, 2026 geierb@geierb.de
# AGPLv3
use strict;
use WWW::Mechanize;
use HTML::Entities;
use HTML::TreeBuilder;
use DateTime::Format::Strptime;
use DateTime::Format::ICal;
use DateTime::Format::ISO8601;
use Data::ICal;
use Data::ICal::Entry::Event;
use Data::ICal::Entry::TimeZone;
use Data::ICal::Entry::TimeZone::Daylight;
use Data::ICal::Entry::TimeZone::Standard;
use Time::HiRes;
use List::MoreUtils qw(first_index);
use Try::Tiny;
use utf8;
use Data::Dumper;
use warnings;
my @urls=(
{ 'url' => "http://127.0.0.1/offene-stunden-wochenplan/", 'ort' => "Bright Yoga, Griesmühlstraße 2, Ingolstadt" },
{ 'url' => "http://127.0.0.1/offene-stunden-wochenplan-stream/", 'ort' => "Livestream" } );
#my $beschreibungsUrl="https://www.brightyoga.de/termine/kursbeschreibungen/";
#my $ajaxUrl="https://www.brightyoga.de/wp-admin/admin-ajax.php";
my $ajaxUrl="http://127.0.0.1/wp-admin/admin-ajax.php";
my @dayNames=("sonntag", "montag", "dienstag", "mittwoch", "donnerstag", "freitag", "samstag");
my $today=DateTime->now('time_zone'=>'Europe/Berlin');
my $datumFormat=DateTime::Format::Strptime->new('pattern'=>'%d.%m.%Y %H:%M','time_zone'=>'Europe/Berlin');
binmode STDOUT, ":utf8"; # Gegen "wide character"-Warnungen
# convert datetime to DTSTART/DTEND property value
sub dt2icaldt {
my ($dt)=@_;
my $icalformatdt=DateTime::Format::ICal->format_datetime($dt);
if (my ($id,$string)=$icalformatdt=~/^TZID=(.+?):(.+)$/) {
return [ $string, {TZID => $id} ];
}
else {
return $icalformatdt;
}
}
# convert datetime to DTSTART/DTEND property value for allday events
sub dt2icaldt_fullday {
my ($dt)=@_;
return [ $dt->ymd(''),{VALUE=>'DATE'} ];
}
my $mech=WWW::Mechanize->new(autocheck => 1, cookie_jar => {});
$mech->add_header('Host' => 'www.brightyoga.de');
my @eventList;
foreach my $u (@urls) {
# Init
$mech->get($u->{'url'}) or die($!);
# Date
my $currentDate=DateTime->now->subtract(days=>1);
my $dateEnd=DateTime->now->add(days=>31);
# Category
my $root=HTML::TreeBuilder->new();
$root->ignore_unknown(0); # "time"-Tag wird sonst nicht erkannt
$root->parse_content($mech->content());
my $category=$root->look_down('_tag'=>'div', class=>'cbs-pagination')->look_down('_tag'=>'a')->attr('data-category');
# Nonce
my ($nonce)=$mech->content=~/var course_booking_system_ajax\s+=\s+.*\"nonce\":\"(.*?)\"/;
while ($currentDate lt $dateEnd) {
# Kalenderseite holen (ist eigentlich schon wegen "GET" von oben da, aber hab keine Lust auf Sonderbehandlung
$mech->post($ajaxUrl, ['action' => 'cbs_action_week', 'nonce' => $nonce, 'category' => $category, 'design' => 'list', 'date' => $currentDate->strftime('%Y-%m-%d'), 'direction' => 'next'] );
# Vorbereitung nächstes POST
# Nonce setzen (muss ich hier nicht, ist immer der selbe)
#($nonce)=$mech->content=~/var course_booking_system_ajax\s+=\s+.*\"nonce\":\"(.*?)\"/;
$currentDate->add(days=>7);
$root->parse_content($mech->content());
# alle Tage durchgehen
foreach my $column ($root->look_down('_tag'=>'div','class'=>'cbs-timetable-column')) {
# "Wochentag"
#my $day=$column->look_down('class'=>'cbs-timetable-column-title')->as_trimmed_text;
#my $date=$today->clone;
#my $dow=first_index { $_ eq lc($day) } @dayNames;
#$date->add(days=>($dow - $date->day_of_week) %7);
# "Wochentag (DD.MM.YYYY)
my $dmy=$column->look_down('class'=>'cbs-timetable-column')->look_down('_tag'=>'time')->attr('datetime');
my $date=DateTime::Format::ISO8601->parse_datetime($dmy);
foreach my $entry ($column->look_down('_tag'=>'li')) {
#print "EVENT\n";
my $event;
# Beginn
my $startTime=$entry->look_down('class'=>'timeslot-start')->as_trimmed_text;
$startTime=~/(\d+).(\d+)/;
$event->{'start'}=$date->clone;
$event->{'start'}->set_hour($1);
$event->{'start'}->set_minute($2);
$event->{'start'}->set_second(0);
# Ende
my $endTime=$entry->look_down('class'=>'timeslot-end')->as_trimmed_text;
$endTime=~/(\d+).(\d+)/;
$event->{'end'}=$date->clone;
$event->{'end'}->set_hour($1);
$event->{'end'}->set_minute($2);
$event->{'end'}->set_second(0);
# Titel
$event->{'title'}=$entry->look_down('_tag'=>'a')->attr('title');
# Anmeldelink
$event->{'url'}=$entry->look_down('_tag'=>'a')->attr('href');
# Freie Plätze
try {
$event->{'emptyseats'}=$entry->look_down('class'=>qr/^attendance/)->as_trimmed_text;
# leider pappt da das Datum hinten dran
$event->{'emptyseats'}=~s/$dmy$//;
};
# Abgesagte Veranstaltungen haben keine availability
next unless defined($event->{'emptyseats'});
# Subtitel
try {
$event->{'subtitle'}=$entry->look_down('class'=>'event-subtitle')->as_trimmed_text;
};
# User
$event->{'trainer'}=$entry->look_down('class'=>qr/trainer.*/)->as_trimmed_text;
# Stream oder Studio?
$event->{'location'}=$u->{'ort'};
if ($event->{'location'}=~/Livestream/) {
$event->{'emptyseats'}="Ohne Teilnehmerbegrenzung";
}
# Keine doppelten Einträge erzeugen
my $identical=0;
foreach my $e (@eventList) {
$identical=1;
foreach my $k (keys %$e) {
if ($e->{$k} ne $event->{$k}) {
$identical=0;
last;
}
}
last if ($identical==1);
}
push(@eventList,$event) if ($identical==0);
}
}
}
}
# Create Datestamp for dtstamp
my @stamp=localtime;
my $dstamp = sprintf("%d%02d%02dT%02d%02d%02dZ",
$stamp[5] + 1900,
$stamp[4] + 1,
$stamp[3],
$stamp[2],
$stamp[1],
$stamp[0]);
my $calendar=Data::ICal->new();
$calendar->add_properties(method=>"PUBLISH",
"X-PUBLISHED-TTL"=>"PT1H",
"X-WR-CALNAME"=>"Bright Yoga",
"X-WR-CALDESC"=>"Offene Stunden bei Bright Yoga");
# Add VTIMEZONE
my $tz="Europe/Berlin";
my $vtimezone=Data::ICal::Entry::TimeZone->new();
$vtimezone->add_properties(tzid=>$tz);
my $tzDaylight=Data::ICal::Entry::TimeZone::Daylight->new();
$tzDaylight->add_properties(
tzoffsetfrom => "+0100",
tzoffsetto => "+0200",
dtstart => "19700329T020000",
tzname => "CEST",
rrule => "FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU"
);
$vtimezone->add_entry($tzDaylight);
my $tzStandard=Data::ICal::Entry::TimeZone::Standard->new();
$tzStandard->add_properties(
tzoffsetfrom => "+0200",
tzoffsetto => "+0100",
dtstart => "19701025T030000",
tzname => "CET",
rrule => "FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU"
);
$vtimezone->add_entry($tzStandard);
$calendar->add_entry($vtimezone);
my $count=0;
foreach my $event (@eventList) {
# Build description
my @desc;
push(@desc, $event->{'subtitle'}) if ($event->{'subtitle'});
push(@desc, "Mit <b>".$event->{'trainer'}."</b>") if ($event->{'trainer'});
push(@desc, $event->{'emptyseats'}) if ($event->{'emptyseats'});
#push(@desc, "<a href='".$beschreibungsUrl."'>Beschreibung</a>");
#push(@desc, "<a href='".$event->{'url'}."'>Anmeldung</a>");
push(@desc, "<a href='".$event->{'url'}."'>Beschreibung und Anmeldung</a>");
my $description=join("<p/>",@desc);
# Create uid
my @tm=localtime();
my $uid=sprintf("%d%02d%02d%02d%02d%02d%s%02d\@geierb.de",
$tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2],
$tm[1], $tm[0], scalar(Time::HiRes::gettimeofday()), $count);
my $eventEntry=Data::ICal::Entry::Event->new();
$eventEntry->add_properties(
uid=>$uid,
summary => $event->{'title'},
description => $description,
dtstart => ($event->{'fullday'}) ? dt2icaldt_fullday($event->{'start'}) : dt2icaldt($event->{'start'}),
dtend => ($event->{'fullday'}) ? dt2icaldt_fullday($event->{'end'}) : dt2icaldt($event->{'end'}),
dtstamp=>$dstamp,
class=>"PUBLIC",
organizer=>'MAILTO:patricia@brightyoga.de',
location=>$event->{'location'},
url=>$event->{'url'},
);
$calendar->add_entry($eventEntry);
$count++;
}
die("Keine Einträge") if (($count==0) and not (($today->month==12 and $today->day>15) or ($today->month==1 and $today->day<15)));
print $calendar->as_string;