Merge pull request 'Fix duplication of ICS events and make TTT only show up on the last Sunday of each month' (#12) from khuxkm/tilderadio-site:master into master

Reviewed-on: tilderadio/site#12
This commit is contained in:
deepend 2020-09-27 02:15:44 +00:00
commit 5aa784495e
1 changed files with 42 additions and 26 deletions

View File

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