-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.php
259 lines (213 loc) · 9.67 KB
/
cron.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
<?php
// Ryerson Events Twitter Bot
// Built by Ryder Damen, 2017
// This bot reads events from the ConnectRU Twitter Feed, and tweets event information the hour before the event begins
// This code is built to be easily adapted for other schools using CollegiateLink software (Brock University, etc)
// File Setup
include 'includes/api_keys.php';
include 'includes/config.php';
date_default_timezone_set($timeZoneSet);
$today = date('Y-m-d');
// File Authorization
$access_key = htmlspecialchars($_GET['key']);
if ($file_access_key !== $access_key) { // $file_access_key provided by
echo "Unauthorized";
die;
}
// Core Code
$eventsArray = getRSSFeed($feedURL);
$results = searchEvents($eventsArray);
echo $results[0] . " events searched, " . $results[1] . " tweets posted.";
// Notify someone if the option is set, and if a tweet is posted
if ($results[1] !== 0 and $notifyEmail !== "") {
$tweetNotificationMessage .= "<br><br><table>";
$i = 1;
foreach ($results[3] as $tweet) {
$tweetNotificationMessage .= "<tr><td>{$i}. </td><td>{$tweet}</td></tr>";
$i++;
}
$tweetNotificationMessage .= "</table>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($notifyEmail, "$results[1] {$schoolName} twitter bot event(s) posted", $tweetNotificationMessage, $headers);
}
// Build the daily schedule
echo '<br>' . buildDailySchedule($results[2], $today);
// Tweet the daily summary
echo '<br>' . tweetDailySummary();
// Functions ----------------------------------------------------------------------------------------------------
function tweetDailySummary() {
// This function tweets a daily summary with a link to the daily summary page
include('includes/config.php');
$currentHour = date('G');
if ( $currentHour == $dailySummaryHour and $enableDailySummaryTweet == true) { // Tweet the daily summary
$json_events = json_decode(file_get_contents('today/events.json'));
$count = $json_events->meta->dailyEventCount;
if ($count == 0) {
$daily_message = $dailySummaryNoEventsMessage;
if ( $dailySummaryNoEventsMessage == "" ) {
return "The daily tweet was not posted, since there are no events, and no message is configured.";
}
}
elseif ($count == 1) {
$daily_message = $dailySummaryOneEventMessage;
}
else {
$daily_message = preg_replace("/###/", $count, $dailySummaryMultipleEventsMessage);
}
if ($enableDailySummaryPage) {
$daily_message .= " " . $dailySummaryUrl;
}
// Connecting to twitter
require_once 'includes/twitteroauth.php';
include 'includes/api_keys.php';
// Initializing twitter connection
define("CONSUMER_KEY", $twitter_consumer_key);
define("CONSUMER_SECRET", $twitter_consumer_secret);
define("OAUTH_TOKEN", $twitter_oauth_token);
define("OAUTH_SECRET", $twitter_oauth_secret);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
// Sending data to twitter
$tweet = $daily_message;
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => $tweet));
return "The daily tweet was posted";
}
}
function buildDailySchedule($todays_events, $today) {
// This function builds a daily schedule at a predetermined time, where students can view events in a simple layout
include('includes/config.php');
if (!$enableDailySummaryPage) {
return 'Daily schedule is not enabled.';
}
$event_count = count($todays_events);
$right_now = strtotime("now");
$meta = array(
'lastUpdated' => "{$right_now}",
'dailyEventCount' => "{$event_count}",
);
$json_print = array(
'meta' => $meta,
'events' => $todays_events
);
if ( !is_readable('./today/events.json') or !is_writeable('./today/events.json') ) {
return 'There is a file permissions error. Please check your server settings';
}
// Write the data to JSON
$json_file = fopen("./today/events.json", "w");
fwrite($json_file, json_encode($json_print, true));
fclose($json_file);
return 'The daily schedule was built';
}
function getRSSFeed($feedURL) {
// This function retrieves the RSS feed from the appropriate website, and returns the decoded array
try {
return simplexml_load_string(file_get_contents($feedURL));
}
catch (Exception $e) {
echo "The RSS Feed is down";
die; // Kill the file
}
}
function searchEvents($eventsArray) {
// This functions searches the feed for events starting this particular hour
$now = strtotime( date('Y-m-d H:i:s') ); // Returns the current date, for processing
$later = strtotime( date('Y-m-d H:i:s', strtotime('+ 65 minutes') ) ); // Returns the date 1 hour 5 mins later
$i_searched = 0; // Setting integer count for events searched
$i_posted = 0; // Setting integer count for events posted
$todays_events = array();
$today = date('Y-m-d');
$tweets = array();
foreach ($eventsArray->channel->item as $event) {
// Setting basic variables
$event_url = $event->link;
$event_organization = $event->author;
$event_name = $event->title;
$event_raw_description = $event->description;
// Do not include cancelled events
if (strpos($event_name, '(Cancelled)') !== false) { continue; }
// Instead of assigning it an individual variable, the start / end dates have been hidden in the description
// In addition to that nonsense, the date also seems to randomly move around within the description tag as it pleases
// This bit of code sorts that nonsense out and finds the date
$event_start_time = findEventStartDate($event_raw_description); // Retrieves the start time from the DOM tree, returned as ISO
$event_start_unix = strtotime($event_start_time);
$event_start_ymd = date('Y-m-d', $event_start_unix); // Get the event start time as a day
if ($today == $event_start_ymd) { // If the event is starting today, at it to the today array
// Find the event location and end date
$event_location = findEventLocation($event_raw_description);
$event_end_time = findEventEndDate($event_raw_description); // Retrieves the start time from the DOM tree, returned as ISO
// Add the event to the today array
$todays_events[] = [
'event_url' => "{$event_url}",
'event_organization' => "{$event_organization}",
'event_name' => "{$event_name}",
'event_location' => "{$event_location}",
'event_start' => "{$event_start_time}",
'event_end' => "{$event_end_time}"
];
}
if ($now <= $event_start_unix and $event_start_unix <= $later) {
// We've got a currently happening event!
$event_location = findEventLocation($event_raw_description); // Using grep to grab the location
$tweets[] = tweetEvent($event_name, $event_url, $event_location, $event_organization, $event_start_time);
$i_posted++;
}
$i_searched++;
} // end of foreach
return array($i_searched, $i_posted, $todays_events, $tweets);
}
function tweetEvent($event_name, $event_url, $event_location, $event_organization, $event_start_time) {
// Limits string lengths, and posts to twitter
require_once 'includes/twitteroauth.php';
include 'includes/api_keys.php';
// Initializing twitter connection
define("CONSUMER_KEY", $twitter_consumer_key);
define("CONSUMER_SECRET", $twitter_consumer_secret);
define("OAUTH_TOKEN", $twitter_oauth_token);
define("OAUTH_SECRET", $twitter_oauth_secret);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
// Limiting the string lengths for twitter with elvis operators (similar to if else statements)
$event_name = strlen($event_name) > 60 ? substr($event_name,0,57)."..." : $event_name;
$event_organization = strlen($event_organization) > 60 ? substr($event_organization,0,57)."..." : $event_organization;
$event_location = strlen($event_location) > 60 ? substr($event_location,0,57)."..." : $event_location;
// Optimizing date for twitter
$event_start_time = date('g:i A' ,strtotime($event_start_time));
// Sending data to twitter
$tweet = "{$event_start_time}: {$event_name} hosted by {$event_organization}. 📍{$event_location}. {$event_url}";
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => $tweet));
return $tweet;
}
function findEventStartDate($description) {
// Finding the start date
preg_match("/dtstart\" title=\"(.*?)\"/", $description, $dtstart);
if (empty($dtstart)){ // If the date is hidden somewhere else in the DOM tree
// Get the date
preg_match("/span class=\"dtstart\"><span class=\"value\" title=\"(.*?)\"/", $description, $dtstart_date);
// Get the time
preg_match("/class=\"value\" title=\"(.*?)\"/", $description, $dtstart_time);
return $dtstart_date[1] . "T" . $dtstart_time[1]; // This RSS feed layout is the stuff of nightmares
};
// Return in ISO format for multi-use processing
return $dtstart[1];
}
function findEventEndDate($description) {
// Finding the end date
preg_match("/dtend\" title=\"(.*?)\"/", $description, $dtend);
if (empty($dtend)){ // If the date is hidden somewhere else in the DOM tree
// Get the date
preg_match("/span class=\"dtend\" title=\"(.*?)\"/", $description, $dtend_date);
// Get the time
preg_match("/dtend\" title=\"(.*?)\"/", $description, $dtend_time);
return $dtend_date[1] . "T" . $dtend_time[1]; // This RSS feed layout is the stuff of nightmares
};
// Return in ISO format for multi-use processing
return $dtend[1];
}
function findEventLocation($description) {
// Finds the event location hidden within the description tag
$description = htmlspecialchars_decode($description);
preg_match("/location\">(.*?)</", $description, $location);
return $location[1];
}
?>