/** * * @param string $latLong * @param bool $forceReload * @return \models\entities\GeocodeCached Description * */ public function lookUp($latLong, $forceReload = false) { list($lat, $long) = explode(',', $latLong); $preciseLat = round(doubleval($lat), self::GEOCODE_PRECISION); $preciseLong = round(doubleval($long), self::GEOCODE_PRECISION); if (!$forceReload) { $lookUpWhere = (new \DbTableWhere())->where('longitude', $preciseLong)->where('latitude', $preciseLat)->setLimitAndOffset(1); $cachedList = \models\entities\GeocodeCached::manager()->getEntitiesWhere($lookUpWhere); if (count($cachedList)) { return $cachedList[0]; } } foreach ($this->serviceProviderList as $serviceProvier) { $lookUpResult = $serviceProvier->lookUp($preciseLong, $preciseLat); \SystemLogger::debug("Making call with: ", $serviceProvier->getClassBasic()); if ($lookUpResult && ($lookUpResult->getCity() || $lookUpResult->getPostalCode())) { //var_dump($lookUpResult);exit; return $this->cacheLookup($lookUpResult, $preciseLong, $preciseLat); } else { if ($lastError = $serviceProvier->getLastError(true)) { \SystemLogger::warn("Error: {$lastError->getMessage()} TYPE: {$lastError->getType()}"); } if ($lastError && !$lastError->isRateLimit()) { break; } } } return null; }
protected function loadMovieJSON($url) { \SystemLogger::debug("URL for Movie", $url); $json = file_get_contents($url, false, $this->context); \SystemLogger::debug("Response:", $json); return json_decode($json, true); }
/** * @param GeocodeCached $geocode Previously stored geoLocation information * @param string $currentDate date for showtimes to load, advisable to pass it in * @param int $dateOffset number of days from the current dates * @return TheatreData[] an array of schema TheatreData, see below for description * * Organized in the format => * * ``` * * TheatreData : [ * 'theatre' => [ * ...fields from theatre table, MUST contain name and address..., * ] * 'movies' => MovieData[] * ] * * MovieData : [ * 'movie' => [ * ...fields from movie table, MUST contain title..., * ] * 'showtimes' => ShowtimeData[] * ] * * ShowtimeData: [ * ...fields from showtime table, must conatin show_date, show_time & type... * ] * ``` */ public function loadShowtimes(GeocodeCached $geocode, $currentDate = null, $dateOffset = 0) { $params = ['country' => $geocode->country_iso, 'event_date' => \Utilities::dateFromOffset($currentDate ?: date('Y-m-d'), $dateOffset)]; \SystemLogger::debug('Begining API Request to Tripican'); $start = microtime(true); $allCinemaMovieShowtimes = TripicanDataLoader::GetCinemaMovieShowtimes($this->apiClient, $params); \SystemLogger::debug('Request to API completed in :', microtime(true) - $start); $theatres = []; foreach ($allCinemaMovieShowtimes as $cinemaMovieShowtimes) { $theatre = $cinemaMovieShowtimes['cinema']; $id = $theatre['id']; if (!isset($theatres[$id])) { $theatres[$id] = ['theatre' => ['name' => $theatre['centre_name'], 'address' => $this->_getAddress($theatre), 'longitude' => $theatre['place']['long'], 'latitude' => $theatre['place']['lat']], 'movies' => []]; } $theatreData =& $theatres[$id]; foreach ($cinemaMovieShowtimes['movie_showtimes'] as $movieShowtimes) { $movie = $movieShowtimes['movie']; $mId = $movie['id']; if (!isset($theatreData['movies'][$mId])) { $theatreData['movies'][$mId] = ['movie' => ['title' => $movie['title'], 'genre' => $movie['genre'][0], 'user_rating' => isset($movie['imdb_rating']) ? floatval($movie['imdb_rating'][0] / 10.0) : null, 'poster_url' => $movie['poster'], 'rated' => $movie['rated'][0], 'runtime' => $movie['duration']], 'showtimes' => []]; } $movieData =& $theatreData['movies'][$mId]; foreach ($movieShowtimes['showtimes'] as $showtime) { $movieData['showtimes'][] = ['show_date' => $showtime['event_date'], 'show_time' => $showtime['event_time'], 'type' => $this->_getMapType($showtime['type']), 'url' => $showtime['online_ticketing'] ? $showtime['url'] : null]; } } } array_walk($theatres, function (&$t) { $t['movies'] = array_values($t['movies']); }); if (!empty($theatres)) { $this->overrideDistanceCompute(); } return array_values($theatres); }
private function extractTheatreMovieShowtimes($pageData, $limit, &$totalPages) { $startTime = microtime(true); \SystemLogger::debug("Begining extraction of data from file, size = ", strlen($pageData)); if ($limit <= 0) { \SystemLogger::warn("Invalid limit was supplied: ", $limit); return array(); } \SystemLogger::debug('Attempting to load into Query Path'); /* @var $moviePage DOMQuery */ $moviePage = \QueryPath::withHTML($pageData, null, array('convert_to_encoding' => "UTF-8", 'convert_from_encoding' => "UTF-8")); \SystemLogger::debug('Loaded into QueryPath'); /* @var $theatersDom DOMQuery */ $theatersDom = $moviePage->find("div.theater"); //get total pages $paginationDom = $moviePage->find("#navbar td"); $totalPages = $paginationDom->length ? $paginationDom->length - 2 : 1; \SystemLogger::debug("Found", $theatersDom->length, "theatres"); $theatreCinemas = array(); $foundTheatres = 0; \SystemLogger::debug('Loading data from Theatres DOM list'); for ($i = 0; $i < $theatersDom->length && $foundTheatres < $limit; $i++) { $theatre = array(); $theatreDom = new DOMQuery($theatersDom->get($i)); $theatre['name'] = trim($theatreDom->find("h2.name")->first()->text()); if (!$theatre['name']) { \SystemLogger::warn("Found no theatre at dom level: ", $i); continue; } \SystemLogger::debug("processing theatre: ", $theatre['name']); $addressText = $theatreDom->find(".info")->first()->text(); //echo $addressText, "<br>"; $tmp = explode(" - ", trim($addressText)); array_pop($tmp); $theatre['address'] = join(' ', $tmp); $theatreCinemas[] = array('theatre' => $theatre, 'movies' => $this->extractMovieShowtimes($theatreDom)); $foundTheatres++; } \SystemLogger::info('Extraction done, completed in ', microtime(true) - $startTime, 'ms'); return $theatreCinemas; }
/** * * @param array $results * @param GeocodeCached $locationInfo * @return int number of cached results */ private function cacheResult($results, $locationInfo, $date) { \SystemLogger::debug('Extracted results are being cached.'); $startTime = microtime(true); $showtimesHuge = []; $computeDistance = !$this->shouldDeferDistanceInfo(); foreach ($results as $theatreMovieShowtime) { $theatre = Theatre::getOrCreate($theatreMovieShowtime['theatre'], $locationInfo, $computeDistance, $computeDistance); if ($theatre) { foreach ($theatreMovieShowtime['movies'] as $movieShowtimeData) { $movie = Movie::getOrCreate($movieShowtimeData['movie']); if ($movie) { $showtimesHuge = array_merge($showtimesHuge, $this->setShowtimesData($theatre, $movie, $movieShowtimeData['showtimes'])); } } } else { \SystemLogger::warn("Could not create theatre with data: ", $theatreMovieShowtime['theatre']); } } $result = !empty($showtimesHuge) ? Showtime::table()->insert($showtimesHuge, true, true) : 0; $locationInfo->setLastUpdated($date); \SystemLogger::debug('CACHING OF of results completed in: ', microtime(true) - $startTime, 'ms'); return $result; }
protected function callUrl($url, $logResponse = null) { $startTime = microtime(true); $contextOpt = array('http' => array('user_agent' => $this->userAgent, 'ignore_errors' => true, 'timeout' => 30, 'method' => 'GET', 'header' => ['Accept-Encoding: gzip, deflate'])); $streamContext = stream_context_create($contextOpt); \SystemLogger::debug(get_class($this), ":", __METHOD__, "URL: ", $url); $raw = file_get_contents($url, false, $streamContext); $result = $this->getResponseContent($raw, $http_response_header); \SystemLogger::debug("Response length: ", strlen($result)); if ($logResponse === null) { $logResponse = $this->isLoggingRequests(); } if ($logResponse) { $this->logRequest($url, $result, $http_response_header); } \SystemLogger::debug(get_class($this), ":", 'Completed request in : ', microtime(true) - $startTime, 'ms'); return $result; }
protected function extractShowtimes($pageData) { $startTime = microtime(true); \SystemLogger::debug('Extraction of page started, total length = ', strlen($pageData)); \SystemLogger::debug('Loading into QueryPath'); $imdbPage = QueryPath::withHTML($pageData); \SystemLogger::debug('Query Path done loading...'); \SystemLogger::debug(__CLASS__, "Extracting theatres..."); //$cinemasList = $imdbPage->find("#cinemas-at-list .list_item.odd, #cinemas-at-list .list_item.even"); $cinemasList = $imdbPage->find("#cinemas-at-list > .list_item"); \SystemLogger::debug(__CLASS__, "Found: ", $cinemasList->count(), "cinemas"); $theatreMovieShowtimes = array(); if ($cinemasList) { for ($i = 0; $i < ShowtimeService::THEATRE_LIMIT && $i < $cinemasList->count(); $i++) { \SystemLogger::debug('Processing theatre at position: ', $i); /* @var $cinemaDiv DOMQuery */ $cinemaDiv = new DOMQuery($cinemasList->get($i)); $theatreData = array(); $theatreTitle = $cinemaDiv->find('h3')->first(); \SystemLogger::info("{$i}. Theatre: ", $theatreTitle->text()); if (!$theatreTitle) { \SystemLogger::debug(__CLASS__, "No theatre found"); continue; } $theatreData['name'] = trim($theatreTitle->text()); $addressSpanTmp = $cinemaDiv->find('.address div'); $addressSpan = $addressSpanTmp ? preg_replace('/\\s+/', ' ', $addressSpanTmp->text()) : ""; $theatreData['address'] = trim(explode('|', $addressSpan)[0]); $movieDomList = $cinemaDiv->find('.list_item'); \SystemLogger::info(__CLASS__, "Number of Movies = ", $movieDomList->count()); if (!count($movieDomList)) { \SystemLogger::debug(__CLASS__, "No movies found"); continue; } $theatreMovieShowtimes[] = array('theatre' => $theatreData, 'movies' => $this->extractMovies($movieDomList)); \SystemLogger::debug('--Theatre extraction completed---'); } } \SystemLogger::debug('Showtimes extraction completed in :', microtime(true) - $startTime); //var_dump($theatreMovieShowtimes);exit; return $theatreMovieShowtimes; }