Files
eventcinema/inc/eventcinema.php
2017-05-03 14:11:18 +12:00

148 lines
4.0 KiB
PHP

<?php
class EventCinema extends PhpObject
{
//DB
const DB = 'db.json';
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 PROM_LINK = 'https://www.eventcinemas.co.nz/Promotions/MemberMovieOfTheWeek#cinemas=502';
public function __construct()
{
$this->loadMoviesFromDb();
$this->addMovieOfTheWeek();
}
private function loadMoviesFromDb()
{
if(!file_exists(self::DB)) $this->asMovies = array();
else
{
$sContent = file_get_contents(self::DB);
$this->asMovies = json_decode($sContent==''?'{}':$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 addMovieOfTheWeek()
{
$asNewMovie = $this->getMovieOfTheWeek();
$sTitle = $asNewMovie['title'];
//Merge with DB
if(!array_key_exists($sTitle, $this->asMovies))
{
$this->asMovies = array($sTitle=>$asNewMovie) + $this->asMovies;
}
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);
//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;
$aoDetails = $oHeader->item(5)->childNodes;
$sDates = mb_substr($aoDetails->item(1)->nodeValue, 6);
$sWorkingLink = $sLink;
break;
}
}
return array('title'=>$sTitle, 'dates'=>$sDates, 'link'=>$sWorkingLink, 'date'=>date('r'));
}
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)
{
$asItem = array();
$asItem['title'] = $asMovie['title'];
$asItem['link'] = $asMovie['link'];
$asItem['guid'] = $asMovie['title'];
$asItem['category'] = 'Movies';
$asItem['pub_date'] = $asMovie['date'];
$asItem['author'] = 'Event Cinema - Queen Street';
$asItem['description'] = $asMovie['dates'].' '.date('Y');
$oFeed->addItem($asItem);
}
return $oFeed->getFeed();
}
}