Trying new ical style, could this work?

This commit is contained in:
Ismo Vuorinen
2010-09-28 09:14:51 +03:00
parent 11427ec552
commit 6f8e84a981

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* Basso Radio Showtimes * Basso Radio Showtimes
* *
@@ -15,97 +16,89 @@
* @see http://en.wikipedia.org/wiki/ICalendar * @see http://en.wikipedia.org/wiki/ICalendar
* @todo Documentation * @todo Documentation
* @todo More testing * @todo More testing
**/ * */
class BassoFeed class BassoFeed {
{
/** /**
* Show you listen to * Show you listen to
* @var string * @var string
**/ * */
var $show; var $show;
/** /**
* List of showtimes * List of showtimes
* @var array * @var array
*/ */
var $showtimes; var $showtimes;
/** /**
* Show title, desc, etc. * Show title, desc, etc.
* @var array * @var array
*/ */
var $showinfo; var $showinfo;
/** /**
* File (and possibly folder) you use as cache * File (and possibly folder) you use as cache
* @var string Default: "./cache/[show].txt" * @var string Default: "./cache/[show].txt"
*/ */
var $cachefile; var $cachefile;
/** /**
* How long the programpage should be cached in seconds * How long the programpage should be cached in seconds
* @var int Default: 900 * @var int Default: 900
*/ */
var $cachetime; var $cachetime;
/** /**
* Is the data coming from cache or not * Is the data coming from cache or not
* @var bool * @var bool
*/ */
var $from_cache = 0; var $from_cache = 0;
function __construct($show) function __construct($show) {
{ $this->show = $show;
$this->show = $show; $this->cachefile = "./cache/{$this->show}.txt";
$this->cachefile = "./cache/{$this->show}.txt"; $this->cachetime = 900;
$this->cachetime = 900;
$this->generate(); $this->generate();
} }
/** /**
* generate * generate
* *
* The action sequence of the script. * The action sequence of the script.
* Used to run the process: * Used to run the process:
* * cache verification * * cache verification
* * scraping * * scraping
* * gets * * gets
* *
* @return void * @return void
* @author Ismo Vuorinen * @author Ismo Vuorinen
**/ * */
function generate() function generate() {
{
// Load the data // Load the data
$data = $this->cache(); $data = $this->cache();
// Process the data // Process the data
$this->showinfo = $this->get_showinfo($data); $this->showinfo = $this->get_showinfo($data);
$this->showtimes = $this->get_showtimes($data); $this->showtimes = $this->get_showtimes($data);
// Echo the iCal // Echo the iCal
$this->get_ical(); $this->get_ical();
} }
/** /**
* cache * cache
* Fetches the page to a cachefile and returns it * Fetches the page to a cachefile and returns it
* *
* @return mixed * @return mixed
* @author Ismo Vuorinen * @author Ismo Vuorinen
**/ * */
function cache() function cache() {
{
$filemtime = 0; $filemtime = 0;
if( is_readable($this->cachefile) ) { if (is_readable($this->cachefile)) {
$filemtime = filemtime($this->cachefile); $filemtime = filemtime($this->cachefile);
} }
if( if (
!$filemtime || (time() - $filemtime >= $this->cachetime) !$filemtime || (time() - $filemtime >= $this->cachetime)
) { ) {
$fetch = file_get_html( $fetch = file_get_html(
"http://www.basso.fi/radio/".$this->show "http://www.basso.fi/radio/" . $this->show
); );
file_put_contents($this->cachefile, $fetch); file_put_contents($this->cachefile, $fetch);
$this->from_cache = false; $this->from_cache = false;
@@ -115,71 +108,67 @@ class BassoFeed
return file_get_html($this->cachefile); return file_get_html($this->cachefile);
} }
} }
/** /**
* get_showtimes * get_showtimes
* Process loaded showpage and find our showtimes * Process loaded showpage and find our showtimes
* @param mixed $fetch * @param mixed $fetch
* @uses simple_html_dom::find|simple_html_dom::innertext * @uses simple_html_dom::find|simple_html_dom::innertext
*/ */
function get_showtimes($fetch) function get_showtimes($fetch) {
{
// Find our sidebar columns and get the insides // Find our sidebar columns and get the insides
foreach($fetch->find('div.column_entry') as $m) { foreach ($fetch->find('div.column_entry') as $m) {
$div = $m->innertext; $div = $m->innertext;
$items[] = $div; $items[] = $div;
} }
// List of finnish daynames for elimination from the strings // List of finnish daynames for elimination from the strings
$finnish_dates = array( $finnish_dates = array(
"Maanantai", "Tiistai", "Keskiviikko", "Maanantai", "Tiistai", "Keskiviikko",
"Torstai", "Perjantai", "Lauantai", "Sunnuntai" "Torstai", "Perjantai", "Lauantai", "Sunnuntai"
); );
// Take the found broadcast times, strip tags and explode it // Take the found broadcast times, strip tags and explode it
$items = $items[1]; $items = $items[1];
$items = str_replace("<br />", "|", $items); $items = str_replace("<br />", "|", $items);
$items = str_replace("Tulevia lähetysaikoja", "", $items); $items = str_replace("Tulevia lähetysaikoja", "", $items);
$items = explode("|", strip_tags($items)); $items = explode("|", strip_tags($items));
// Take the processed showtimes and mangle to right format // Take the processed showtimes and mangle to right format
foreach ($items as $n => $item) foreach ($items as $n => $item) {
{
$item = trim($item); $item = trim($item);
if( !empty($item) && strlen($item) > 2 ) if (!empty($item) && strlen($item) > 2) {
{
// Remove finnish daynames // Remove finnish daynames
$item = str_replace($finnish_dates, "", $item); $item = str_replace($finnish_dates, "", $item);
// Split into 2 vars; start and end times // Split into 2 vars; start and end times
$dates = explode("-", trim($item)); $dates = explode("-", trim($item));
$dates2 = explode(" ", $dates[0]); $dates2 = explode(" ", $dates[0]);
$dates_from = $dates[0]; $dates_from = $dates[0];
$dates_to = $dates2[0]." ".$dates[1]; $dates_to = $dates2[0] . " " . $dates[1];
$date = $dates2[0]; $date = $dates2[0];
list($day, $month, $year) = explode(".", $date); list($day, $month, $year) = explode(".", $date);
$date = "20{$year}-$month-$day"; // We are on the 21st cent. $date = "20{$year}-$month-$day"; // We are on the 21st cent.
// Unix timestamps // Unix timestamps
$time_f = strtotime($date." ".$dates2[1]); $time_f = strtotime($date . " " . $dates2[1]);
$time_t = strtotime($date." ".$dates[1]); $time_t = strtotime($date . " " . $dates[1]);
// We take -2 as timezone info 'coz the times are in +2 // We take -2 as timezone info 'coz the times are in +2
$date_f = $this->unixToiCal( $time_f, -2 ); $date_f = $this->unixToiCal($time_f, -2);
$date_t = $this->unixToiCal( $time_t, -2 ); $date_t = $this->unixToiCal($time_t, -2);
$stuff[$n]["time_f"] = $time_f; $stuff[$n]["time_f"] = $time_f;
$stuff[$n]["time_t"] = $time_t; $stuff[$n]["time_t"] = $time_t;
$stuff[$n]["date_f"] = $date_f; $stuff[$n]["date_f"] = $date_f;
$stuff[$n]["date_t"] = $date_t; $stuff[$n]["date_t"] = $date_t;
} }
} }
return $stuff; return $stuff;
} }
/** /**
* get_showinfo * get_showinfo
* Parses the show info from fetched data * Parses the show info from fetched data
@@ -187,44 +176,42 @@ class BassoFeed
* @return array * @return array
* @todo Document me * @todo Document me
* @author Ismo Vuorinen * @author Ismo Vuorinen
**/ * */
function get_showinfo($fetch) function get_showinfo($fetch) {
{ foreach ($fetch->find('div#main_column_1') as $m) {
foreach($fetch->find('div#main_column_1') as $m) {
$div = $m->innertext; $div = $m->innertext;
$items[] = $div; $items[] = $div;
} }
$title = $fetch->find("h1", 0); $title = $fetch->find("h1", 0);
$title = $title->plaintext; $title = $title->plaintext;
$cleaned = $items[0]; $cleaned = $items[0];
$cleaned = str_replace("&nbsp;", " ", $cleaned); $cleaned = str_replace("&nbsp;", " ", $cleaned);
$cleaned = strip_tags($cleaned, "<div><h1>"); $cleaned = strip_tags($cleaned, "<div><h1>");
$clean_array = explode("\n", $cleaned); $clean_array = explode("\n", $cleaned);
foreach ($clean_array as $clean) { foreach ($clean_array as $clean) {
$clean = trim( strip_tags($clean, "<h1>") ); $clean = trim(strip_tags($clean, "<h1>"));
if( if (
strlen( $clean ) > 10 && strlen($clean) > 10 &&
!preg_match('/\<h1\>/i', $clean) ) !preg_match('/\<h1\>/i', $clean)) {
{
$c[] = $clean; $c[] = $clean;
} }
} }
$cleaned = $c; $cleaned = $c;
$desc = $cleaned[2]; $desc = $cleaned[2];
$data = array( $data = array(
"title" => $title, "title" => $title,
"desc" => $desc, "desc" => $desc,
"url" => "http://www.basso.fi/radio/" . $this->show "url" => "http://www.basso.fi/radio/" . $this->show
); );
return $data; return $data;
} }
@@ -235,40 +222,52 @@ class BassoFeed
* @return str * @return str
* @todo Document me * @todo Document me
* @author Ismo Vuorinen * @author Ismo Vuorinen
**/ * */
function get_ical() function get_ical() {
{ $cal = "BEGIN:VCALENDAR\n"
$cal = "BEGIN:VCALENDAR\n" . "VERSION:2.0\n"
."VERSION:2.0\n" . "PRODID:BASSOFEED\n"
."PRODID:-//basso/feed//NONSGML v1.0//EN\n"; . "CALSCALE:GREGORIAN\n"
. "X-WR-TIMEZONE:Etc/GMT\n"
foreach( $this->showtimes as $i ) . "METHOD:PUBLISH\n"
{ . "BEGIN:VTIMEZONE\n"
. "TZID:GMT\n"
. "BEGIN:STANDARD\n"
. "DTSTART:20100101T010000\n"
. "TZOFFSETTO:+0000\n"
. "TZOFFSETFROM:+0000\n"
. "END:STANDARD\n"
. "END:VTIMEZONE\n";
foreach ($this->showtimes as $i) {
$cal .= "BEGIN:VEVENT\n" $cal .= "BEGIN:VEVENT\n"
."UID:".md5($this->show . $i["date_f"])."@basso.fi\n" . "SUMMARY:{$this->showinfo["title"]}\n"
."DTSTAMP:{$i["date_f"]}\n" . "DESCRIPTION:{$this->showinfo["desc"]}\n"
."DTSTART:{$i["date_f"]}\n" . "LOCATION:Basso Radio 102.8 FM\n"
."DTEND:{$i["date_t"]}\n" . "UID:{$this->show}-{$i["date_f"]}/basso.fi\n"
."SUMMARY:{$this->showinfo["title"]}\n" . "URL:http://basso.fi/radio/{$this->show}\n"
."DESCRIPTION:{$this->showinfo["desc"]}\n" . "DTSTART;VALUE=DATE-TIME;TZID=GMT:{$i["date_f"]}\n"
."END:VEVENT\n"; . "DTEND;VALUE=DATE-TIME;TZID=GMT:{$i["date_t"]}\n"
. "DTSTAMP:{$i["date_f"]}\n"
. "END:VEVENT\n";
} }
$cal .= "END:VCALENDAR\n"; $cal .= "END:VCALENDAR\n";
return $cal; return str_replace("\n", "\r\n", $cal);
} }
/** /**
* unixToiCal * unixToiCal
* Unix timestamp to iCal spec format * Unix timestamp to iCal spec format
* @author chubby at chicks dot com * @author chubby at chicks dot com
* @see http://fi.php.net/manual/en/function.date.php#83429 * @see http://fi.php.net/manual/en/function.date.php#83429
* @return str * @return str
**/ * */
function unixToiCal($uStamp = 0, $tzone = 0.0) { function unixToiCal($uStamp = 0, $tzone = 0.0) {
$uStampUTC = $uStamp + ($tzone * 3600); $uStampUTC = $uStamp + ($tzone * 3600);
$stamp = date("Ymd\THis\Z", $uStampUTC); $stamp = date("Ymd\THis\Z", $uStampUTC);
return $stamp; return $stamp;
} }