Fix duplication of ICS events and make TTT only show up on the last Sunday of each month #12

Merged
deepend merged 2 commits from :master into master 2020-09-27 02:15:47 +00:00
1 changed files with 42 additions and 26 deletions

View File

@ -1,6 +1,6 @@
<?php
$from = gmdate("Y-m-d\T00:00:00\Z", strtotime(date("w") ? "last sunday" : "sunday"));
$to = gmdate("Y-m-d\T00:00:00\Z", strtotime("2 days ago",strtotime(date("w") ? "sunday" : "next sunday")));
$from = gmdate("Y-m-d\T00:00:00\Z",strtotime("today"));
$to = gmdate("Y-m-d\T00:00:00\Z",strtotime("today + 8 days"));
include 'schedule.php';
function ics_formatdate($date) {
@ -24,31 +24,47 @@ echo "VERSION:2.0".ICS_EOL;
echo "PRODID:tilderadio schedule".ICS_EOL;
echo "DTSTAMP:".ics_formatdate("now").ICS_EOL;
// A list of event IDs that we have on the calendar, to avoid duplication
$event_ids = array();
foreach ($schedule as $event) {
// The VEVENT structure's pretty easy to generate, especially since we're already in UTC.
echo "BEGIN:VEVENT".ICS_EOL;
// First, we need a creation date.
// Just go with now.
echo "DTSTAMP:".ics_formatdate("now").ICS_EOL;
// Next, the event start and end.
echo "DTEND:".ics_formatdate($event["end"]).ICS_EOL;
echo "DTSTART:".ics_formatdate($event["start"]).ICS_EOL;
// Next, the recurrence rule.
// We assume the format is weekly. (No DJs have requested any other frequency yet.)
echo "RRULE:FREQ=WEEKLY".ICS_EOL;
// Next, the event title, or "summary" as the spec calls it.
echo "SUMMARY:DJ ".$event["title"].ICS_EOL;
// Finally, a unique ID for this event.
// To make absolutely certain we don't repeat the same event ID, I decided to use a SHA256 hash of the event structure.
echo "UID:";
echo hash("sha256",json_encode($event));
// to avoid the validator complaining about lines longer than 75 characters, split after the hash
echo ICS_EOL." ";
// Now finish the address UID
echo "@tilderadio.org".ICS_EOL;
// Finally, close the VEVENT structure.
echo "END:VEVENT".ICS_EOL;
// Next event?
if (!in_array($event["id"],$event_ids)){
array_push($event_ids,$event["id"]);
// The VEVENT structure's pretty easy to generate, especially since we're already in UTC.
echo "BEGIN:VEVENT".ICS_EOL;
// First, we need a creation date.
// Just go with now.
echo "DTSTAMP:".ics_formatdate("now").ICS_EOL;
// Next, the event start and end.
echo "DTEND:".ics_formatdate($event["end"]).ICS_EOL;
echo "DTSTART:".ics_formatdate($event["start"]).ICS_EOL;
// Next, the recurrence rule.
if ($event["title"]!="tomasino") {
// We assume the format is weekly. (tomasino is the only DJ to have requested any other frequency.)
echo "RRULE:FREQ=WEEKLY".ICS_EOL;
} else {
// TTT only comes on the last Sunday of the month, 23:30:00 UTC to 01:00:00 UTC
if (gmdate("His",strtotime($event["start"]))=="233000" && gmdate("His",strtotime($event["end"]))=="010000") {
echo "RRULE:FREQ=MONTHLY;WKST=SU;BYDAY=SU;BYSETPOS=-1".ICS_EOL;
} else {
// his other shows are weekly though
echo "RRULE:FREQ=WEEKLY".ICS_EOL;
}
}
// Next, the event title, or "summary" as the spec calls it.
echo "SUMMARY:DJ ".$event["title"].ICS_EOL;
// Finally, a unique ID for this event.
// To make absolutely certain we don't repeat the same event ID, I decided to use a SHA256 hash of the event structure.
echo "UID:";
echo hash("sha256",json_encode($event));
// to avoid the validator complaining about lines longer than 75 characters, split after the hash
echo ICS_EOL." ";
// Now finish the address UID
echo "@tilderadio.org".ICS_EOL;
// Finally, close the VEVENT structure.
echo "END:VEVENT".ICS_EOL;
// Next event?
}
}
// Finally, close out the VCALENDAR structure.