<?php
/**
* SloveniaMovieShowtimes class
*
* Class for fetching movie showtimes for the cinemas in Slovenia.
* Information is retrieved from http://www.napovednik.com.
*
* @copyright 2009 Tomaž Muraus
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version Release: 1.0
* @link http://www.tomaz-muraus.info
*/
class SloveniaMovieShowtimes
{
const SHOWTIMES_URL = 'http://www.napovednik.com/kino/';
const MOVIE_URL = 'http://www.napovednik.com';
protected $_city;
protected $_date;
protected $_showtimes = array();
public function __construct($city = 'ljubljana', $date = 'today')
{
$this->_city = $this->_formatCityName($city);
$this->_date = $date;
}
/**
* Fetches the showtimes for the given city and date.
*
* @return array Showtimes array.
*/
public function fetchShowtimes()
{
if ($this->_checkForValidCity($this->_city) === FALSE)
{
return FALSE;
}
if ($this->_date === 'today')
{
$data = file_get_contents(self::SHOWTIMES_URL . $this->_city . '/');
}
else
{
// Calculate the date difference in days between the current and given date
$differenceInDays = $this->_calculateDateDifferenceInDays(strtotime($this->_date), mktime(0, 0, 0, date('m'), date('d'), date('Y')));
// Maximal date difference in days can be +/-25 days
if (abs($differenceInDays) > 25)
{
return FALSE;
}
else
{
$data = file_get_contents(self::SHOWTIMES_URL . $this->_city . '/d/' . $differenceInDays);
}
}
// Fetch cinema list for the specified city and date
preg_match_all('#<h2><a href=".*?" class="kino">(.*?):*</a>:*</h2>#i', $data, $cinemas);
// Fetch movie showtimes for each cinema
for ($i = 0; $i < count($cinemas[0]); $i++)
{
$start = strpos($data, $cinemas[0][$i]) + strlen($cinemas[0][$i]);
$end = ($i < count($cinemas[0]) - 1) ? strpos($data, $cinemas[0][$i + 1]) : strpos($data, '<div id=footer><small>');
$length = $end - $start;
$cinemaData = substr($data, $start, $length);
preg_match_all('#<h4><a href="(.*)">(.*?)</a></h4>#i', $cinemaData, $title);
preg_match_all('#</h4>\s(.*?)<br />#i', $cinemaData, $info);
preg_match_all('#<i>(.*?)</i><br>#i', $cinemaData, $times);
// Fill the showtimes array
$this->_showtimes[$i] = array
(
'id' => $i,
'cinema' => $cinemas[1][$i],
'movies' => array()
);
for ($j = 0; $j < count($title[0]); $j++)
{
$this->_showtimes[$i]['movies'][$j] = array
(
'id' => $j,
'title' => $title[2][$j],
'url' => self::MOVIE_URL . $title[1][$j],
'info' => $info[1][$j],
'times' => $times[1][$j]
);
}
}
return $this->_showtimes;
}
/**
* Checks if the given city is valid.
*
* @param string $city City name.
*
* @return boolean TRUE on success, FALSE otherwise.
*/
protected function _checkForValidCity($city)
{
$validCities = file_get_contents('http://www.napovednik.com/kino_sporedi');
preg_match('#<div id="*subSelect"*>(.*?)</div>#is', $validCities, $matches);
$validCities = preg_replace(array('#<h1>#', '#<a href=.*?>#', '#</h1>#', '#</a>#', '#\s\|\s#'), array('', '', '', '', '|'), $matches[1]);
$validCities = array_map(array($this, _formatCityName), explode('|', $validCities));
if (in_array($city, $validCities))
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Calculates the difference in days between the two dates.
*
* @param int $date1 First date as Unix timestamp.
* @param int $date2 Second date as Unix timestamp.
*
* @return int Difference in days.
*/
protected function _calculateDateDifferenceInDays($date1, $date2)
{
$differenceInDays = floor((($date1 - $date2) / (60 * 60 * 24)));
return $differenceInDays;
}
/**
* Formats the city name (replaces spaces with an underscore and makes a name lowercase).
*
* @param string $city City name.
*
* @return string Formated city name.
*/
protected function _formatCityName($city)
{
return strtolower(str_replace(' ', '_', $city));
}
}
/* End of file SloveniaMovieShowtimes.php */
/* Location: ./SloveniaMovieShowtimes.php */