ICal2Plings

From Plings Info

Jump to: navigation, search
This script has been obsoleted, and has been replaced by PliCal.

Ical2Plings is a simple and rather hackish script to try to convert an iCal file to a Plings XML file.

This was written as a one off script to parse one particular iCal file. For longer term use, it would make sense to rewrite using a proper iCal library. This would also allow for more complex recursion than weekly.

Please Note iCal and Plings XML formats are not 100% compatible, so really further data is required to get a good set of data to Plings.

Source Code

<?php
# Copyright (c) 2009 Ben Webb <bjwebb67@googlemail.com>
# Released as free software under the MIT license.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# 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 OR COPYRIGHT HOLDERS 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

function desc_format($string) {
    return stripslashes(str_replace("\\n", "\n", $string));
}
 
$lines = file("libraries.ics");
$maxtime = mktime(0, 0, 0, 8, 31, 2009);
 
$i = 0;
$desc = false;
$oldline = "";
$line = "";
$v = 0;
$recur = false;
foreach ($lines as $num => $currline) {
    if ($line == "") {
        $line = $currline;
        continue;
    }
    $currbits = explode(":",$currline);
    if ($currbits[0] == $currline) {
        $line .= trim($currline);
        continue;
    }
 
    $bits = explode(":",$line);
    if (trim($line) == "BEGIN:VEVENT") {
        $i++;
        $desc = false;
        echo "<span style=\"color: green; font-weight: bold;\">NEW</span>";
    }
 
    if (strpos($line, "FREQ=WEEKLY") !== FALSE) {
        $recur = true;
    }
    if (trim($line) == "END:VEVENT" && $recur) {
        $oldi = $i;
        strtotime($info[$oldi]["Starts"]);
        for ($a=strtotime($info[$oldi]["Starts"]); $a<$maxtime; $a += 7*24*60*60) {
            $i++;
            $info[$i] = $info[$oldi];
            $info[$i]["Starts"] = date("c",$a);
            $info[$i]["Ends"] = date("c",$a);
            $info[$i]["LinkWithActivitySourceID"] = $info[$oldi]["ActivitySourceID"];
            $info[$i]["ActivitySourceID"] = $i;
        }
    }
 
    if (preg_match("/^DTSTART/", $bits[0]))
        $info[$i]["Starts"] = date("c",strtotime($bits[1]));
    elseif (preg_match("/^DTEND/", $bits[0]))
        $info[$i]["Ends"] = date("c",strtotime($bits[1]));
    elseif (preg_match("/^UID/", $bits[0]))
        $info[$i]["ActivitySourceID"] = $bits[1];
    elseif (preg_match("/^SUMMARY/", $bits[0]))
        $info[$i]["Name"] = $bits[1];
    elseif (preg_match("/^DESCRIPTION/", $bits[0])) {
        $info[$i]["Description"] = desc_format($bits[1]);
    }
    elseif (preg_match("/^LOCATION/", $bits[0])) {
        $addr = stripslashes($bits[1]);
        $addr = str_replace("St.", "St", $addr);
        $addrbit = preg_split("/[,\.]/",$addr);
        $tmp = trim($addrbit[2]);
        if ($venuearr[$tmp] == "") { 
            echo $tmp;
            if (($pos = strpos($tmp," ")) !== FALSE) {
                $addrbit[2] = substr($tmp,0,$pos);
                $addrbit[3] = substr($tmp,$pos+1,1000);
                //BIG DIRTY HACK!!
                if (strpos($addrbit[3], "LE2 ODS") !== FALSE)
                    $addrbit[3] = "LE2 0DS";
            }
            $venuearr[$tmp] = $v;
            $venues[$v] = $addrbit;
            $info[$i]["Venue"] = $v;
            $v++;
        }
        else {
            $info[$i]["Venue"] = $venuearr[$tmp];
        }
    }
    echo $line."<br/>";
    // TODO *PROPER* RECUR
 
    $line = rtrim($currline);
}
 
/*
$info[$i]["ContactName"] = "General Enquiries";
$info[$i]["ContactNumber"] = "n/a";
*/
 
print_r($info);
print_r($venues);
 
$doc = new DOMDocument();
$doc->formatOutput = true;
 
$tl = $doc->createElement("Activities");
$doc->appendChild($tl);
 
$i = 0;
$j = 0;
foreach ($info as $i => $act) {
    $activity[$i][$j] = $doc->createElement("Activity");
    $tl->appendChild($activity[$i][$j]);
    if ($act["Description"] == "") $act["Description"] = "No description available.";
    $act["ContactName"] = "General Enquiries";
    $act["ContactNumber"] = "n/a";
    foreach ($act as $field => $text) {
        if ($text != "" && $field != "Venue") {
            $tmp = $doc->createElement($field);
            $activity[$i][$j]->appendChild($tmp);
            $tmp->appendChild($doc->createTextNode(trim($text, "\x7f..\xff\x0..\x1f\t\r\n\0 ")));
        }
    }
    $vnode = $doc->createElement("Venue");
    $activity[$i][$j]->appendChild($vnode);
    $tmp = $doc->createElement("ProviderVenueID");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($act["Venue"]));
 
    $tmp = $doc->createElement("Name");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($venues[$act["Venue"]][0]));
    $tmp = $doc->createElement("BuildingNameNo");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($venues[$act["Venue"]][0]));
    $tmp = $doc->createElement("Street");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($venues[$act["Venue"]][1]));
    $tmp = $doc->createElement("Town");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($venues[$act["Venue"]][2]));
    $tmp = $doc->createElement("Postcode");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode($venues[$act["Venue"]][3]));
    $tmp = $doc->createElement("ContactPhone");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode("n/a"));
    $tmp = $doc->createElement("ContactForename");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode("General"));
    $tmp = $doc->createElement("ContactSurname");
    $vnode->appendChild($tmp);
    $tmp->appendChild($doc->createTextNode("Enquiries"));
 
}
 
$doc->save("output.xml");
echo "<a href=\"output.xml\">View XML</a>";
 
?>
Personal tools