Files
eventcinema/inc/eventcinema.php

219 lines
6.1 KiB
PHP

<?php
class EventCinema extends PhpObject
{
//DB
const DB = 'db.json';
private $asMovies;
//Remote
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 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();
//Movie of the week is overwriting as it is cheaper than last chance films
$this->addNewMovies();
}
private function loadMoviesFromDb()
{
$this->asMovies = array();
if(file_exists(self::DB))
{
$sContent = file_get_contents(self::DB);
if($sContent != '') $this->asMovies = json_decode($sContent, true);
}
}
private function saveMovieToDb()
{
$sContent= json_encode($this->asMovies);
file_put_contents(self::DB, $sContent);
}
private function getCurl($sUrl, $bHeader=false, $asPostData=array())
{
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HEADER, $bHeader);
if(!empty($asPostData))
{
curl_setopt($oCurl, CURLOPT_POST, count($asPostData));
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $asPostData);
}
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
//Fake browser
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//if($this->sReferer!='') curl_setopt($oCurl, CURLOPT_REFERER, $this->sReferer);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
//if($bHeader) curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
//Cookies
//$sCookies = $this->getCookies();
//if($sCookies!='') curl_setopt($oCurl, CURLOPT_COOKIE, $sCookies);
$fCookieJar = tempnam('/tmp','cookie');
curl_setopt($oCurl, CURLOPT_COOKIESESSION, true);
curl_setopt($oCurl, CURLOPT_COOKIEJAR, $fCookieJar);
curl_setopt($oCurl, CURLOPT_COOKIEFILE, $fCookieJar);
$sContent = curl_exec($oCurl);
curl_close($oCurl);
//$this->sReferer = $sUrl;
return $sContent;
}
private function addNewMovies()
{
$aoMovies = array_merge($this->getLastChanceFilms(), $this->getMovieOfTheWeek());
foreach($aoMovies as $oMovie)
{
$asMovie = $oMovie->getMovieInfo();
$sKey = $asMovie['promo'].'|'.$asMovie['title'];
//Merge with DB
if(!array_key_exists($sKey, $this->asMovies))
{
$this->asMovies[$sKey] = $asMovie;
}
}
$this->saveMovieToDb();
}
private function getMovieOfTheWeek()
{
//Tests all URLs
foreach(self::PROM_LINKS as $sLink)
{
$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;
$oMovie->setTitle($oHeader->item(3)->nodeValue);
$aoDetails = $oHeader->item(5)->childNodes;
$oMovie->setDates(mb_substr($aoDetails->item(1)->nodeValue, 6));
$oMovie->setLink($sLink);
$oMovie->setPromo('Movie of the Week');
break;
}
*/
}
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)
{
if(!in_array(self::CINEMA_ID, $asMovie['CinemaIds'])) continue;
$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()
{
$asDesc = array
(
'title'=>'Rarbg RSS Feed',
'link'=>'',
'copyright'=>'Powered by Franzz. RSS Feed Generator under GPLv3 License',
'description'=>'Rarbg RSS Feed',
'language'=>'en',
'webmaster_mail'=>'franzz@gmail.com'
);
$oFeed = new Feed($asDesc);
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'] = $sPromo;
$asItem['pub_date'] = $asMovie['date'];
$asItem['author'] = 'Event Cinema - Queen Street';
$asItem['description'] = $sPromo.' - '.$asMovie['dates'];
$oFeed->addItem($asItem);
}
return $oFeed->getFeed();
}
}