Snippets
Created by
GreenBlack
last modified
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 | <?php
// This script grabs the events from zwift.com/events and makes them available in ical form so you can use them in your calendar
// Disclaimer: This is script created for myself to use in my calendar, and is NOT supported by or related to Zwift in any way
class zwift_ics{
private $url = "http://zwift.com/json/events";
private $cachefile = "cache.txt";
private $cachetime = "600";
private $output = FALSE;
function run(){
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=zwift_events.ics');
// Try to get the data from cache
$this->output = $this->get_cache();
// No data, query zwift
if($this->output === FALSE){
$data = $this->get_data_from_zwift();
if($data !== FALSE){
$this->create_output($data);
}else{
throw new Exception("Sorry, I wasn't able to grab the latest data from Zwift");
}
$this->set_cache($this->output);
}
// Print everything and exit
exit($this->output);
}
function create_output($data){
$data = json_decode($data);
if(!is_array($data)){
throw new Exception("Retrieved data incorrect");
}
$this->output = $this->header();
foreach($data as $id => $event){
$this->output .= $this->event($event);
}
$this->output .= $this->footer();
return $this->output;
}
function get_cache(){
if(!file_exists($this->cachefile)){
file_put_contents($this->cachefile, "");
}
$cache = file_get_contents($this->cachefile);
// If the file is empty
if(strlen($cache) < 100){
return FALSE;
}
// if it is modified longer than given time ago
if((filemtime($this->cachefile) + $this->cachetime) < time()){
return FALSE;
}
return $cache;
}
function set_cache($data){
file_put_contents($this->cachefile, $data);
}
function get_data_from_zwift(){
return file_get_contents($this->url);
}
function dateToCal($timestamp) {
return date('Ymd\THis\Z', $timestamp);
}
function escapeString($string) {
$string = str_replace("\n", "\\n", $string);
return preg_replace('/([\,;])/','\\\$1', $string);
}
function header(){
return "BEGIN:VCALENDAR\n" .
"VERSION:2.0\n" .
"METHOD:PUBLISH\n" .
"PRODID:-//onlinephpfunctions.com//zwift 1.0//EN\n";
}
function event($event){
return "BEGIN:VEVENT" .
"\nDESCRIPTION:" . $this->escapeString($event->description) .
"\nDTEND:" . $this->dateToCal($event->start + $event->durationInSeconds) .
"\nDTSTAMP:" . $this->dateToCal($event->start) .
"\nDTSTART:" . $this->dateToCal($event->start) .
"\nSEQUENCE:0" .
"\nLOCATION:" . $this->escapeString($event->route) .
"\nSUMMARY:" . $this->escapeString($event->name) . // Go to Zwift.com/events for full details" // . $this->escapeString($event->description) . "
"\nUID:" . uniqid() .
"\nEND:VEVENT\n";
}
function footer(){
return "\nEND:VCALENDAR\n";
}
}
$zwift_events = new zwift_ics();
$zwift_events->run();
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.