diff --git a/inc/eventcinema.php b/inc/eventcinema.php index b7dd116..b9fc88b 100644 --- a/inc/eventcinema.php +++ b/inc/eventcinema.php @@ -7,24 +7,30 @@ class EventCinema extends PhpObject private $asMovies; //Remote - const PROM_LINKS = array( 'https://www.eventcinemas.co.nz/Promotions/MemberMovieOfTheWeek#cinemas=502', - 'https://www.eventcinemas.co.nz/Promotions/MembersMovieOfTheWeek#cinemas=502'); + const EVENT_URL = 'https://www.eventcinemas.co.nz'; + const CINEMA_ID = 502; + const PROM_LINKS = array( self::EVENT_URL.'/Promotions/MemberMovieOfTheWeek#cinemas='.self::CINEMA_ID, + self::EVENT_URL.'/Promotions/MembersMovieOfTheWeek#cinemas='.self::CINEMA_ID); - const PROM_LINK = 'https://www.eventcinemas.co.nz/Promotions/MemberMovieOfTheWeek#cinemas=502'; + const LASTCHANCE_LINK = self::EVENT_URL.'/Promotions/LastChanceFilms#cinemas='.self::CINEMA_ID; + //https://www.eventcinemas.co.nz/Cinemas/GetSessions?cinemaIds=502&date=2017-07-04 public function __construct() { + parent::__construct(); $this->loadMoviesFromDb(); - $this->addMovieOfTheWeek(); + + //Movie of the week is overwriting as it is cheaper than last chance films + $this->addNewMovies(); } private function loadMoviesFromDb() { - if(!file_exists(self::DB)) $this->asMovies = array(); - else + $this->asMovies = array(); + if(file_exists(self::DB)) { $sContent = file_get_contents(self::DB); - $this->asMovies = json_decode($sContent==''?'{}':$sContent, true); + if($sContent != '') $this->asMovies = json_decode($sContent, true); } } @@ -69,51 +75,113 @@ class EventCinema extends PhpObject return $sContent; } - private function addMovieOfTheWeek() + private function addNewMovies() { - $asNewMovie = $this->getMovieOfTheWeek(); - $sTitle = $asNewMovie['title']; - - //Merge with DB - if(!array_key_exists($sTitle, $this->asMovies)) + $aoMovies = array_merge($this->getLastChanceFilms(), $this->getMovieOfTheWeek()); + foreach($aoMovies as $oMovie) { - $this->asMovies = array($sTitle=>$asNewMovie) + $this->asMovies; + $asMovie = $oMovie->getMovieInfo(); + $sTitle = $asMovie['title']; + + //Merge with DB + if(!array_key_exists($sTitle, $this->asMovies)) + { + $this->asMovies[$sTitle] = $asMovie; + } } - //else $this->asMovies[$sTitle] = $asNewMovie; $this->saveMovieToDb(); } private function getMovieOfTheWeek() { - $sTitle = ''; - $sDates = ''; - $sWorkingLink = ''; - //Tests all URLs foreach(self::PROM_LINKS as $sLink) { - //Get Event Cinema Page - $sContent = $this->getCurl($sLink); - - //Build DOM - $oDom = new DOMDocument(); - @$oDom->loadHTML($sContent); - $oXPath = new DOMXPath($oDom); + $aoMovies = $this->getMovieListFromPage($sLink, 'Movie of the Week'); + if(!empty($aoMovies)) break; + /* + $oMovie = new Movie(); + $oXPath = $this->getXPath($sLink); //Get header object $oMovieSection = $oXPath->query('//*[@class="header-title"]//*[@class="featured-name arrow-top small"]'); if($oMovieSection->length > 0) { $oHeader = $oMovieSection->item(0)->childNodes; - $sTitle = $oHeader->item(3)->nodeValue; + $oMovie->setTitle($oHeader->item(3)->nodeValue); $aoDetails = $oHeader->item(5)->childNodes; - $sDates = mb_substr($aoDetails->item(1)->nodeValue, 6); - $sWorkingLink = $sLink; + $oMovie->setDates(mb_substr($aoDetails->item(1)->nodeValue, 6)); + $oMovie->setLink($sLink); + $oMovie->setPromo('Movie of the Week'); break; } + */ } - return array('title'=>$sTitle, 'dates'=>$sDates, 'link'=>$sWorkingLink, 'date'=>date('r')); + + return $aoMovies; + } + + private function getLastChanceFilms() + { + return $this->getMovieListFromPage(self::LASTCHANCE_LINK, 'Last Chance'); + } + + private function getMovieListFromPage($sUrl, $sPromo='') + { + $aoMovies = array(); + $oTimeZone = new DateTimeZone('Pacific/Auckland'); + + $oXPath = $this->getXPath($sUrl); + $oMoviesNodeList = $oXPath->query('//*[@class="movie-data"]'); + if($oMoviesNodeList->length > 0) + { + $oMovieNode = $oMoviesNodeList->item(0); + $asMovies = json_decode($oMovieNode->getAttribute('data-movies'), true); + + //Specific start/end fields + $oFrom = new DateTime(substr($oMovieNode->getAttribute('data-fromdate'), 0, 10), $oTimeZone); + $oTo = new DateTime(substr($oMovieNode->getAttribute('data-todate'), 0, 10), $oTimeZone); + + //Avalaible dates (fallback) + $asDates = json_decode($oMovieNode->getAttribute('data-dates'), true); + foreach($asDates as $sDate) + { + $oDate = new DateTime($sDate, $oTimeZone); + if($oDate < $oFrom) $oFrom = $oDate; + if($oDate > $oTo) $oTo = $oDate; + } + + $sFrom = $oFrom->format("d/m"); + $sTo = $oTo->format("d/m"); + + foreach ($asMovies as $asMovie) + { + $oMovie = new Movie($asMovie['Id']); + + $oMovie->setTitle($asMovie['Name']); + $oMovie->setDates($sFrom.' - '.$sTo); + $oMovie->setLink(self::EVENT_URL.$asMovie['MovieUrl']); + $oMovie->setPromo($sPromo); + + $aoMovies[] = $oMovie; + } + } + + return $aoMovies; + } + + private function getXPath($sLink) + { + //Get Event Cinema Page + $sContent = $this->getCurl($sLink); + + //Build DOM + $oDom = new DOMDocument(); + @$oDom->loadHTML($sContent); + $oXPath = new DOMXPath($oDom); + + return $oXPath; } public function getRss() @@ -131,14 +199,16 @@ class EventCinema extends PhpObject foreach($this->asMovies as $asMovie) { + $sPromo = array_key_exists('promo', $asMovie)?$asMovie['promo']:'Movies'; + $asItem = array(); $asItem['title'] = $asMovie['title']; $asItem['link'] = $asMovie['link']; $asItem['guid'] = $asMovie['title']; - $asItem['category'] = 'Movies'; + $asItem['category'] = $sPromo; $asItem['pub_date'] = $asMovie['date']; $asItem['author'] = 'Event Cinema - Queen Street'; - $asItem['description'] = $asMovie['dates'].' '.date('Y'); + $asItem['description'] = $sPromo.' - '.$asMovie['dates']; $oFeed->addItem($asItem); } diff --git a/inc/movie.php b/inc/movie.php new file mode 100644 index 0000000..31cf6b6 --- /dev/null +++ b/inc/movie.php @@ -0,0 +1,55 @@ +setId($iId); + $this->setTitle(''); + $this->setDates(''); + $this->setLink(''); + $this->setPromo(''); + } + + public function setId($iId) + { + $this->iId = $iId; + } + + public function setTitle($sTitle) + { + $this->sTitle = $sTitle; + } + + public function setDates($sDates) + { + $this->sDates = $sDates; + } + + public function setLink($sLink) + { + $this->sLink = $sLink; + } + + public function setPromo($sPromo) + { + $this->sPromo = $sPromo; + } + + public function getMovieInfo() + { + return array( 'id'=>$this->iId, + 'title'=>$this->sTitle, + 'dates'=>$this->sDates, + 'link'=>$this->sLink, + 'promo'=>$this->sPromo, + 'date'=>date('r')); + } +} \ No newline at end of file diff --git a/index.php b/index.php index e8adc70..fafa9da 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,7 @@ require_once '../objects/class_management.php'; $oClassManagement = new ClassManagement('eventcinema'); $oClassManagement->incClass('rss'); +$oClassManagement->incClass('movie', true); $oEventCinema = new EventCinema();