summaryrefslogtreecommitdiff
path: root/scraper.php
diff options
context:
space:
mode:
authorJonas Kümmerlin <jonas@kuemmerlin.eu>2024-01-27 13:05:41 +0100
committerJonas Kümmerlin <jonas@kuemmerlin.eu>2024-01-27 13:05:41 +0100
commit907f39db4ab00de43016646660d9d1ca9c73d1f9 (patch)
tree49642a7e9ab374c3eab2ae91b12a24fdf4a21f62 /scraper.php
initial commitHEADmaster
Diffstat (limited to 'scraper.php')
-rw-r--r--scraper.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/scraper.php b/scraper.php
new file mode 100644
index 0000000..3c1faec
--- /dev/null
+++ b/scraper.php
@@ -0,0 +1,60 @@
+<?php
+
+/* DATA RETRIEVAL
+ *
+ * Retrieves events from communi and puts them into the database
+ */
+
+function communievents_scrape_events() {
+ global $wpdb;
+
+ $tablename = $wpdb->prefix . 'communievents_events';
+ $sync_dt = new DateTimeImmutable('now', new DateTimeZone('utc'));
+ $sync_ts = $sync_dt->getTimestamp();
+
+ $api = CommuniApi::defaultInstance();
+ $events = $api->events($api->userGroupIds());
+
+ // falls es gar keine events gibt ist etwas faul!
+ // lieber hier abbrechen und keine alten Events löschen
+ if (empty($events))
+ return;
+
+ foreach ($events as $e) {
+ if ($e->allday()) {
+ // wir speichern Ganztags-Veranstaltungen als Veranstaltung von 00:00:01 bis 23:59:59
+ // damit die Queries mit < und > die Veranstaltung den ganzen Tag lang ausspucken
+ $start_str = $e->start()->format('Y-m-d 00:00:01');
+ $end_str = $e->end()->format('Y-m-d 23:59:59');
+ } else {
+ $start_str = $e->start()->format('Y-m-d H:i:s');
+ $end_str = $e->end()->format('Y-m-d H:i:s');
+ }
+
+ $wpdb->replace($tablename,
+ array(
+ 'id' => $e->id(),
+ 'group' => $e->groupId(),
+ 'title' => $e->title(),
+ 'location' => $e->location(),
+ 'description' => $e->description(),
+ 'start' => $start_str,
+ 'end' => $end_str,
+ 'allday' => $e->allday(),
+ 'official' => $e->official(),
+ 'url' => $e->webappUrl(),
+ 'lastsync' => $sync_ts
+ ));
+ if ($wpdb->last_error !== '')
+ $wpdb->print_error();
+ }
+
+ // remove all old events
+ $wpdb->query($wpdb->prepare("DELETE FROM $tablename WHERE lastsync < %d", $sync_ts));
+ if ($wpdb->last_error !== '')
+ $wpdb->print_error();
+}
+
+function communievents_setup_cron_hook() {
+ add_action('communievents_cron_hook', 'communievents_scrape_events');
+}