public

Wise Timetable to iCalendar Converter

  • Download Gist
WiseTimetableToiCalendarConverter.php
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
<?php
/*
* Wise Timetable to iCalendar Converter
* @Author Marko Zabreznik
* @copyright Marko Zabreznik
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Usage: ?c=[id][,id]...
* id is number of the course
*
*/
 
define('LOCALE', 'Slovenian');
define('URL', 'http://www.feri.uni-mb.si/urniki/');
 
/* */
// do a post request with APC caching
function do_cached_post_request($url, $data, $optional_headers = null){
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null)
$params['http']['header'] = $optional_headers;
// maybe get from APC
$hash = md5(serialize($params));
if (function_exists('apc_fetch') && $response = apc_fetch($hash))
return $response;
$fp = @fopen($url, 'rb', false, stream_context_create($params));
if (!$fp)
throw new Exception("Problem with $url, $php_errormsg");
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
// store to APC
if (function_exists('apc_add'))
apc_add($hash, $response, 360 );
return $response;
}
/* */
 
setlocale(LC_ALL, LOCALE);
 
// get courses from url
$courses='';
foreach(explode(',',$_GET['c']) as $course)
if(is_numeric($course)) $courses[]=$course;
if(empty($courses))
die('not enought data');
 
// get courses from Wise Timetable server
$data = do_cached_post_request(URL,'show_week=0&courses_values='.implode('%2C',$courses));
 
// get data from HTML
preg_match_all('/span class="caption">(.*?)<\/span/is', $data, $titles);
preg_match_all('/\/thead>(.*?)<\/table/is', $data, $tables);
 
// VCalendar Header
echo <<<EOT
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hxcal//NONSGML Version 1//EN
 
EOT;
 
// parse courses
$it = 0;
foreach($tables[1] as $table) {
$table = explode('class="data"',$table);
$title = ucfirst(strtolower(trim($titles[1][$it])));
// parse events
foreach($table as $trow){
preg_match_all('/td class="wholeCell">(.*?)<\/td/is', $trow, $cells);
$cells = $cells[1];
$time = explode(' - ',$cells[2]);
$dtstart = date('Ymd\THis',strtotime($cells[1].' '.$time[0]));
$dtend = date('Ymd\THis',strtotime($cells[1].' '.$time[1]));
$cells[6]=ucwords(strtolower($cells[6]));
$skupine = explode(',',$cells[5]);
 
echo <<<EOT
BEGIN:VEVENT
DTSTART;TZID=Europe/Ljubljana:$dtstart
DTEND;TZID=Europe/Ljubljana:$dtend
LOCATION:{$cells[3]}
CLASS:{$cells[4]}
CONTACT:{$cells[6]}
DESCRIPTION:{$cells[4]}: {$cells[6]} za {$cells[5]}
SUMMARY:$title
 
EOT;
 
foreach ($skupine as $skupina)
echo 'ATTENDEE:'.trim($skupina).PHP_EOL;
echo 'END:VEVENT'.PHP_EOL;
}
$it++;
}
 
echo "END:VCALENDAR".PHP_EOL;
 
// end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.