RDF to iCal converter (PHP script)

…which can easily be adapted to, say, RSS or shit.

I wrote this with a particular RDF feed in mind, so you might have to tweak it, if you want to use it.

Also, see the TODOs.

<?php

// GPLv3.
// Parsing part copied over from PHP manual.
// Also used RFC2445: http://www.ietf.org/rfc/rfc2445.txt
// Distributed under fuck you license.

class RDFItem {
	var $link;		//
	var $title;		//
	var $description;	//
	var $date;		//
//	var $dc:date;		// hell knows what this is

    function RDFItem ($link)
    {
        foreach ( $link as $k => $v )
            $this->$k = $link[$k];
    }
}

function readRDF( $filename )
{
    // read the RDF
    $data = implode("", file($filename));
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);

    // loop through the file structures
    foreach ($tags as $key=>$val) {
        if ($key == 'item') {
            $itemranges = $val;

            // each contiguous pair of array entries are the
            // lower and upper range for each RDF feed item
            // (what?..)

            for ($i=0; $i < count($itemranges); $i+=2) {
                $offset = $itemranges[$i] + 1;
                $len = $itemranges[$i + 1] - $offset;
                $tdb[] = parseItem( array_slice($values, $offset, $len) );
            }
        } else {
            continue;
        }
    }

    return $tdb;
}

function parseItem($subElements)
{
	for ( $i = 0; $i \n/", "", $desc, 1 );

	// Make sure all lines except the 1st have a space up front.
	$desc = str_replace( "\n", "", $desc );
	$desc = str_replace( "<br />", "\\n\n ", $desc );
	$desc = str_replace( "<br/>", "\\n\n ", $desc );

	// Manage links.
	$desc = preg_replace( "/<a href=\"(.*?)\">/s", " [$1] ", $desc );
	$desc = str_replace( "</a>", "", $desc );

	// Manage all other HTML tags.
//	$desc = preg_replace( "/<(?:.*?)>/s", "", $desc );

	// TODO: check that lines are folded at 75 octet.

	return $desc;
}


$db = readRDF( 'hardcore.rdf' );

/*
echo "<pre>";
print_r($db);
echo "</pre>";
*/




// Now for the dirty punk solution.
// Create the calendar.
$cal  = "BEGIN:VCALENDAR\nVERSION:2.0\n";
$cal .= "X-WR-CALNAME:Vilnius Hardcore Events | [TODO: date]\n";
$cal .= "PRODID:-//YOURNAME//TOOLNAME//EN\n";

foreach ( $db as $info )
{
	$info->date = fix_date($info->date);
	$info->description = fix_description($info->description);

	$cal .= "BEGIN:VEVENT\n";
	$cal .= "DTSTART;VALUE=DATE-TIME:".$info->date."\n";
	$cal .= "DTEND;VALUE=DATE-TIME:".$info->date."\n";
	$cal .= "SUMMARY:".$info->title."\n";
	$cal .= "DESCRIPTION:".$info->description."\n";
	$cal .= "URL:http://url.com/TODO\n";
	$cal .= "UID:".$info->link."\n";
	$cal .= "END:VEVENT\n";
}

$cal .= "END:VCALENDAR\n";

//echo $cal;
echo file_put_contents ( "hardcore.ics", $cal );

?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: