This repository was archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventsTodayAndTomorrow.php
More file actions
165 lines (145 loc) · 4.48 KB
/
eventsTodayAndTomorrow.php
File metadata and controls
165 lines (145 loc) · 4.48 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
<?php
// Get config
$configFile = $argv[1];
if (!$configFile || !file_exists($configFile)) {
die("You must provide a config file!\n");
}
// load config file, and check
$config = parse_ini_file($configFile);
foreach(array('site_url','slack_incoming_webhook_url') as $var) {
if (!isset($config[$var]) || !$config[$var]) {
die("Missing config variable: ".$var."\n");
}
}
$ignoreEvents = array();
if (isset($config['ignore_events'])) {
foreach(explode("," , $config['ignore_events']) as $ie) {
$ie = intval(trim($ie));
if ($ie) {
$ignoreEvents[] = $ie;
}
}
}
// Get JSON URL
$url = $config['site_url'];
if (substr($url, -1) != '/') {
$url .= '/';
}
$url .= 'api1';
if (isset($config['area_slug']) && $config['area_slug']) {
$url .= '/area/'.$config['area_slug'];
} else if (isset($config['curated_list_slug']) && $config['curated_list_slug']) {
$url .= '/curatedlist/'.$config['curated_list_slug'];
}
$url .= "/events.json";
// Get the Data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'OpenACalendar Slack');
$dataString = curl_exec($ch);
$response = curl_getinfo( $ch );
curl_close($ch);
if ($response['http_code'] != 200) {
die("Not a 200 response from ".$url."\n");
}
$data = json_decode($dataString);
// which events to include?
$datetime = new DateTime("",new DateTimeZone("UTC"));
$datetime->setTime(0,0,0);
$todayStarts = $datetime->getTimestamp();
$datetime->setTime(23,59,59);
$todayEnds = $datetime->getTimestamp();
$datetime->add(new \DateInterval("P1D"));
$tomorrowEnds = $datetime->getTimestamp();
$datetime->setTime(0,0,0);
$tomorrowStarts = $datetime->getTimestamp();
$dataToIncludeToday = array();
$dataToIncludeTomorrow = array();
foreach($data->data as $event) {
if (!$event->deleted && !in_array($event->slug, $ignoreEvents)) {
// TODAY?
$include = false;
// starts today?
if ($event->start->timestamp >= $todayStarts && $event->start->timestamp <= $todayEnds) {
$include = true;
}
// ends today
if ($event->end->timestamp >= $todayStarts && $event->end->timestamp <= $todayEnds) {
$include = true;
}
// starts before today and ends after today - a ongoing event
if ($event->start->timestamp <= $todayStarts && $event->end->timestamp >= $todayEnds) {
$include = true;
}
if ($include) {
$dataToIncludeToday[] = $event;
} else {
// TOMORROW?
$include = false;
// starts tomorrow?
if ($event->start->timestamp >= $tomorrowStarts && $event->start->timestamp <= $tomorrowEnds) {
$include = true;
}
// ends tomorrow
if ($event->end->timestamp >= $tomorrowStarts && $event->end->timestamp <= $tomorrowEnds) {
$include = true;
}
// starts before tomorrow and ends after tomorrow - a ongoing event
if ($event->start->timestamp <= $tomorrowStarts && $event->end->timestamp >= $tomorrowEnds) {
$include = true;
}
if ($include) {
$dataToIncludeTomorrow[] = $event;
}
}
}
}
// Anything?
if (!$dataToIncludeToday && !$dataToIncludeTomorrow) {
die("No Data In Include\n");
}
// Build message
$message = '';
if ($dataToIncludeToday) {
$message = "On today:\n";
foreach($dataToIncludeToday as $event) {
if ($event->cancelled) {
$message .= "<".$event->siteurl."|".$event->summaryDisplay." [CANCELLED]>\n";
} else {
$message .= "<".$event->siteurl."|".$event->summaryDisplay.">\n";
}
}
}
if ($dataToIncludeTomorrow) {
$message .= "On tomorrow:\n";
foreach($dataToIncludeTomorrow as $event) {
if ($event->cancelled) {
$message .= "<".$event->siteurl."|".$event->summaryDisplay." [CANCELLED]>\n";
} else {
$message .= "<".$event->siteurl."|".$event->summaryDisplay.">\n";
}
}
}
// Post to Slack!
$post = array('text'=>$message);
if (isset($config['slack_channel']) && $config['slack_channel']) {
$post['channel'] = $config['slack_channel'];
}
if (isset($config['slack_username']) && $config['slack_username']) {
$post['username'] = $config['slack_username'];
}
if (isset($config['slack_icon_url']) && $config['slack_icon_url']) {
$post['icon_url'] = $config['slack_icon_url'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['slack_incoming_webhook_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'OpenACalendar Slack');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => json_encode($post)));
$pastResponse = curl_exec($ch);
$postInfo = curl_getinfo( $ch );
curl_close($ch);
var_dump($postResponse);
var_dump($postInfo);