checkDir($cacheDir, 'imdb_cache_dir'); $this->checkDir($photoDir, 'imdb_photo_dir'); $config->cachedir = $cacheDir; $config->photodir = $photoDir; $config->photoroot = $photoRoot; $config->language = get_setting('main.imdb_language', 'en-US'); $config->cache_expire = 0; $config->force_agent = 'Googlebot/2.1 (+http://www.google.com/bot.html)'; $this->config = $config; } public static function listSupportLanguages(): array { $file = sprintf('%s/resources/lang/%s/imdb.php', ROOT_PATH, get_langfolder_cookie(true)); $data = include $file; return $data['languages']; } public function setDebug($debug) { $this->config->debug = $debug; } private function checkDir($dir, $langKeyPrefix) { if (!is_dir($dir)) { $mkdirResult = mkdir($dir, 0777, true); if ($mkdirResult !== true) { $msg = nexus_trans("torrent.{$langKeyPrefix}_can_not_create"); do_log("$msg, dir: $dir"); throw new ImdbException($msg); } } if (!is_writable($dir)) { $msg = nexus_trans("torrent.{$langKeyPrefix}_is_not_writeable"); do_log("$msg, dir: $dir"); throw new ImdbException($msg); } return true; } public function getCachedAt(int $id) { $id = parse_imdb_id($id); $log = "id: $id"; $cacheFile = $this->getCacheFilePath($id); if (!file_exists($cacheFile)) { $log .= ", file: $cacheFile not exits"; } $result = filemtime($cacheFile); $log .= ", file: $cacheFile cache at: $result"; do_log($log); return $result; } /** * @date 2021/1/18 * @param int $id * @return int state (0-not complete, 1-cache complete) */ public function getCacheStatus(int $id) { $id = parse_imdb_id($id); $log = "id: $id"; $cacheFile = $this->getCacheFilePath($id); if (!file_exists($cacheFile)) { $log .= ", file: $cacheFile not exits"; do_log($log); return 0; } if (!fopen($cacheFile, 'r')) { $log .= ", file: $cacheFile can not open"; do_log($log); return 0; } return 1; } public function purgeSingle($id) { $mainCacheFile = $this->getCacheFilePath($id); if (!is_file($mainCacheFile)) { do_log("mainCacheFile: $mainCacheFile not exists, return"); return true; } foreach (glob("$mainCacheFile*") as $file) { if (file_exists($file)) { do_log("unlink: $file"); unlink($file); } } return true; } public function getMovie($id) { if (!isset($this->movies[$id])) { $this->movies[$id] = new Title($id, $this->config); } return $this->movies[$id]; } private function getCacheFilePath($id, $suffix = '') { $id = parse_imdb_id($id); $result = sprintf('%stitle.tt%s', $this->config->cachedir, $id); if ($suffix) { $result .= ".$suffix"; } return $result; } public function updateCache($id) { $id = parse_imdb_id($id); $movie = $this->getMovie($id); //because getPage() is protected, so... $movie->title(); $movie->photo_localurl(); $movie->releaseInfo(); return true; } public function renderDetailsPageDescription($torrentId, $imdbId) { global $lang_details; $movie = $this->getMovie($imdbId); $thenumbers = $imdbId; $country = $movie->country (); $director = $movie->director(); $creator = $movie->creator(); // For TV series $write = $movie->writing(); $produce = $movie->producer(); $cast = $movie->cast(); // $plot = $movie->plot (); $plot_outline = $movie->plotoutline(); $compose = $movie->composer(); $gen = $movie->genres(); //$comment = $movie->comment(); // $similiar_movies = $movie->similiar_movies(); $autodata = 'https://www.imdb.com/title/tt'.$thenumbers."
------------------------------------------------------------------------------------------------------------------------------------
\n"; $autodata .= "".$lang_details['text_information']."
\n"; $autodata .= "------------------------------------------------------------------------------------------------------------------------------------

\n"; $autodata .= "". $lang_details['text_title']."" . "".$movie->title ()."
\n"; $autodata .= "".$lang_details['text_also_known_as'].""; $temp = ""; foreach ($movie->alsoknow() as $ak) { $temp .= $ak["title"].($ak["country"] != "" ? " (".$ak["country"].")" : "") . ($ak["comment"] != "" ? " (" . $ak["comment"] . ")" : "") . ", "; } $autodata .= rtrim(trim($temp), ","); $runtimes = str_replace(" min",$lang_details['text_mins'], $movie->runtime() ?? ''); $autodata .= "
\n".$lang_details['text_year']."" . "".$movie->year ()."
\n"; $autodata .= "".$lang_details['text_runtime']."".$runtimes."
\n"; $autodata .= "".$lang_details['text_votes']."" . "".$movie->votes ()."
\n"; $autodata .= "".$lang_details['text_rating']."" . "".$movie->rating ()."
\n"; $autodata .= "".$lang_details['text_language']."" . "".$movie->language ()."
\n"; $autodata .= "".$lang_details['text_country'].""; $temp = ""; for ($i = 0; $i < count ($country); $i++) { $temp .="$country[$i], "; } $autodata .= rtrim(trim($temp), ","); $autodata .= "
\n".$lang_details['text_all_genres'].""; $temp = ""; for ($i = 0; $i < count($gen); $i++) { $temp .= "$gen[$i], "; } $autodata .= rtrim(trim($temp), ","); $autodata .= "
\n".$lang_details['text_tagline']."" . "".$movie->tagline ()."
\n"; if ($director){ $autodata .= "".$lang_details['text_director'].""; $temp = ""; for ($i = 0; $i < count ($director); $i++) { $temp .= "" . $director[$i]["name"] . ", "; } $autodata .= rtrim(trim($temp), ","); } elseif ($creator) $autodata .= "".$lang_details['text_creator']."".implode(", ", array_column($creator, 'name')); $autodata .= "
\n".$lang_details['text_written_by'].""; $temp = ""; for ($i = 0; $i < count ($write); $i++) { $temp .= "" . "".$write[$i]["name"]."" . ", "; } $autodata .= rtrim(trim($temp), ","); $autodata .= "
\n".$lang_details['text_produced_by'].""; $temp = ""; for ($i = 0; $i < count ($produce); $i++) { $temp .= "" . "".$produce[$i]["name"]."" . ", "; } $autodata .= rtrim(trim($temp), ","); $autodata .= "
\n".$lang_details['text_music'].""; $temp = ""; for ($i = 0; $i < count($compose); $i++) { $temp .= "" . "".$compose[$i]["name"]."" . ", "; } $autodata .= rtrim(trim($temp), ","); $autodata .= "

\n\n------------------------------------------------------------------------------------------------------------------------------------
\n"; $autodata .= "".$lang_details['text_plot_outline']."
\n"; $autodata .= "------------------------------------------------------------------------------------------------------------------------------------
"; // if(count($plot) == 0) // { // $autodata .= "
\n".$plot_outline; // } // else // { // for ($i = 0; $i < count ($plot); $i++) // { // $autodata .= "
\n. "; // $autodata .= $plot[$i]; // } // } if (!empty($plot_outline)) { $autodata .= "
\n".$plot_outline; } $autodata .= "

\n\n------------------------------------------------------------------------------------------------------------------------------------
\n"; $autodata .= "".$lang_details['text_cast']."
\n"; $autodata .= "------------------------------------------------------------------------------------------------------------------------------------

\n"; for ($i = 0; $i < count ($cast); $i++) { if ($i > 9) { break; } $autodata .= ". " . "" . $cast[$i]["name"] . " " .$lang_details['text_as']."" . "".$cast[$i]["role"]."" . "
\n"; } return $autodata; } public function renderTorrentsPageAverageRating($imdbId): string { return $this->getPtGen()->buildRatingSpan([PTGen::SITE_IMDB => $this->getRating($imdbId)]); } public function getRating($imdbId): float|string { $imdbId = parse_imdb_id($imdbId); $defaultRating = $rating = 'N/A'; if ($imdbId && $this->getCacheStatus($imdbId) == 1) { $movie = $this->getMovie($imdbId); $rating = $movie->rating(); } if (!is_numeric($rating)) { $rating = $defaultRating; } return $rating; } public function getPtGen() { if (empty($this->ptGen)) { $this->ptGen = new PTGen(); } return $this->ptGen; } public function getMovieCover($imdbId): string { static $enabled; if (is_null($enabled)) { $enabled = Setting::getIsImdbEnabled(); } if (!$enabled) { return ''; } return NexusDB::remember(self::getMovieCoverCacheKey($imdbId), 864000, function() use ($imdbId) { if ($this->getCacheStatus($imdbId) != 1) { return ''; } try { return $this->getMovie($imdbId)->photo(false); } catch (\Exception $exception) { do_log($exception->getMessage() . $exception->getTraceAsString(), 'error'); return ''; } }); } public static function getMovieCoverCacheKey($imdbId): string { return "imdb:cover:$imdbId"; } }