diff --git a/nexus/Imdb/Imdb.php b/nexus/Imdb/Imdb.php index ac40c9b4..dd389e72 100644 --- a/nexus/Imdb/Imdb.php +++ b/nexus/Imdb/Imdb.php @@ -11,7 +11,7 @@ class Imdb private $movie; - private $pages = array('Title', 'Credits', 'Amazon', 'Goofs', 'Plot', 'Comments', 'Quotes', 'Taglines', 'Plotoutline', 'Trivia', 'Directed'); + private $pages = array('Title', 'Credits', 'ReleaseInfo', ); public function __construct() { @@ -47,16 +47,16 @@ class Imdb return true; } - public function getCachedAt(int $id, string $page) + public function getCachedAt(int $id) { $id = parse_imdb_id($id); - $log = "id: $id, page: $page"; - $cacheFile = $this->getCacheFilePath($id, $page); + $log = "id: $id"; + $cacheFile = $this->getCacheFilePath($id); if (!file_exists($cacheFile)) { $log .= ", file: $cacheFile not exits"; } $result = filemtime($cacheFile); - $log .= ", cache at: $result"; + $log .= ", file: $cacheFile cache at: $result"; do_log($log); return $result; } @@ -64,15 +64,13 @@ class Imdb /** * @date 2021/1/18 * @param int $id - * @param string $page Title, Credits, etc... * @return int state (0-not complete, 1-cache complete) */ - public function getCacheStatus(int $id, string $page) + public function getCacheStatus(int $id) { - return 1; $id = parse_imdb_id($id); - $log = "id: $id, page: $page"; - $cacheFile = $this->getCacheFilePath($id, $page); + $log = "id: $id"; + $cacheFile = $this->getCacheFilePath($id); if (!file_exists($cacheFile)) { $log .= ", file: $cacheFile not exits"; do_log($log); @@ -88,9 +86,10 @@ class Imdb public function purgeSingle($id) { - foreach ($this->pages as $page) { - $file = $this->getCacheFilePath($id, $page); + $mainCacheFile = $this->getCacheFilePath($id); + foreach (glob("$mainCacheFile*") as $file) { if (file_exists($file)) { + do_log("unlink: $file"); unlink($file); } } @@ -105,9 +104,26 @@ class Imdb return $this->movie; } - private function getCacheFilePath($id, $page) + private function getCacheFilePath($id, $suffix = '') { - return sprintf('%s%s.%s', $this->config->cachedir, $id, $page); + $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 getPate() is protected, so... + $movie->title(); + $movie->photo_localurl(); + $movie->releaseInfo(); + return true; + } public function renderDetailsPageDescription($torrentId, $imdbId) diff --git a/nexus/PTGen/PTGen.php b/nexus/PTGen/PTGen.php index 79ad9bd2..32cc9b05 100644 --- a/nexus/PTGen/PTGen.php +++ b/nexus/PTGen/PTGen.php @@ -77,12 +77,16 @@ class PTGen { global $lang_details; $ptGenFormatted = $ptGenArr['format']; - $prefix = sprintf("[img]%s[/img]\n", $ptGenArr['poster']); - $ptGenFormatted = mb_substr($ptGenFormatted, mb_strlen($prefix, 'utf-8') + 1); + $poster = ''; + if (!empty($ptGenArr['poster'])) { + $poster = $ptGenArr['poster']; + $prefix = sprintf("[img]%s[/img]\n", $poster); + $ptGenFormatted = mb_substr($ptGenFormatted, mb_strlen($prefix, 'utf-8') + 1); + } $ptGenFormatted = format_comment($ptGenFormatted); $ptGenFormatted .= sprintf( '%s%s%s%s', - $lang_details['text_information_updated_at'], date('Y-m-d H:i:s', intval($ptGenArr['generate_at'] / 1000)), $lang_details['text_might_be_outdated'], + $lang_details['text_information_updated_at'], !empty($ptGenArr['generate_at']) ? date('Y-m-d H:i:s', intval($ptGenArr['generate_at'] / 1000)) : '', $lang_details['text_might_be_outdated'], $torrentId, $site, $lang_details['text_here_to_update'] ); $titleShowOrHide = $lang_details['title_show_or_hide'] ?? ''; @@ -97,7 +101,7 @@ class PTGen
- poster + poster
@@ -113,6 +117,7 @@ HTML; private function request(string $url, bool $withoutCache = false): array { global $Cache; + $begin = microtime(true); $logPrefix = "url: $url"; $cacheKey = $this->getApiPointResultCacheKey($url); if (!$withoutCache) { @@ -123,7 +128,7 @@ HTML; } } $http = new Client(); - $response = $http->get($url, ['timeout' => 10]); + $response = $http->get($url, ['timeout' => 5]); $statusCode = $response->getStatusCode(); if ($statusCode != 200) { $msg = "api point response http status code: $statusCode"; @@ -148,7 +153,7 @@ HTML; throw new PTGenException($msg); } $Cache->cache_value($cacheKey, $bodyArr, 24 * 3600); - do_log("$logPrefix, success get from api point"); + do_log("$logPrefix, success get from api point, use time: " . (microtime(true) - $begin)); return $bodyArr; } @@ -195,7 +200,16 @@ HTML; ]; $html .= $this->buildDetailsPageTableRow($torrentId, $data, $site); } else { - $ptGenArr = $this->generate($torrentPtGenArr[$site]['link']); + try { + $ptGenArr = $this->generate($torrentPtGenArr[$site]['link']); + } catch (\Exception $e) { + $log = $e->getMessage() . ", trace: " . $e->getTraceAsString(); + do_log($log,'error'); + $ptGenArr = [ + 'format' => $e->getMessage() + ]; + } + $jsonArr[$site] = [ 'link' => $link, 'data' => $ptGenArr, diff --git a/public/details.php b/public/details.php index ba87d303..b3a4fc48 100644 --- a/public/details.php +++ b/public/details.php @@ -139,7 +139,7 @@ else { { $thenumbers = $imdb_id; if (!$moviename = $Cache->get_value('imdb_id_'.$thenumbers.'_movie_name')){ - switch ($imdb->getCacheStatus($imdb_id, 'Title')){ + switch ($imdb->getCacheStatus($imdb_id)){ case "1":{ $moviename = $movie->title (); break; $Cache->cache_value('imdb_id_'.$thenumbers.'_movie_name', $moviename, 1296000); @@ -172,7 +172,7 @@ else { $Cache->new_page('imdb_id_'.$thenumbers.'_large', 1296000, true); if (!$Cache->get_page()){ - switch ($imdb->getCacheStatus($imdb_id, 'Title')) + switch ($imdb->getCacheStatus($imdb_id)) { case "0" : //cache is not ready, try to { @@ -191,7 +191,7 @@ else { $smallth = "\"no"; $autodata = $imdb->renderDetailsPageDescription($row['id'], $imdb_id); - $cache_time = $imdb->getCachedAt($imdb_id, 'Title'); + $cache_time = $imdb->getCachedAt($imdb_id); $Cache->add_whole_row(); print(""); print("\"Show/Hide\" ".$lang_details['text_imdb'] . $lang_details['row_info'] ."
". $smallth."
"); diff --git a/public/retriver.php b/public/retriver.php index f36a9da2..3f0195ac 100644 --- a/public/retriver.php +++ b/public/retriver.php @@ -27,15 +27,20 @@ switch ($siteid) { $thenumbers = $imdb_id; $imdb = new \Nexus\Imdb\Imdb(); - $movie = $imdb->getMovie($imdb_id); - $movieid = $thenumbers; - $target = array('Title', 'Credits', 'Plot'); - ($type == 2 ? $imdb->purgeSingle($imdb_id) : ""); set_cachetimestamp($id,"cache_stamp"); - $Cache->delete_value('imdb_id_'.$thenumbers.'_movie_name'); - $Cache->delete_value('imdb_id_'.$thenumbers.'_large', true); - $Cache->delete_value('imdb_id_'.$thenumbers.'_median', true); - $Cache->delete_value('imdb_id_'.$thenumbers.'_minor', true); + + $imdb->purgeSingle($imdb_id); + + try { + $imdb->updateCache($imdb_id); + $Cache->delete_value('imdb_id_'.$thenumbers.'_movie_name'); + $Cache->delete_value('imdb_id_'.$thenumbers.'_large', true); + $Cache->delete_value('imdb_id_'.$thenumbers.'_median', true); + $Cache->delete_value('imdb_id_'.$thenumbers.'_minor', true); + } catch (\Exception $e) { + $log = $e->getMessage() . ", trace: " . $e->getTraceAsString(); + do_log($log, 'error'); + } header("Location: " . get_protocol_prefix() . "$BASEURL/details.php?id=".htmlspecialchars($id)); } break; @@ -47,9 +52,14 @@ switch ($siteid) $ptGenInfo = json_decode($row['pt_gen'], true); $link = $ptGenInfo[$siteid]['link']; $ptGen = new \Nexus\PTGen\PTGen(); - $result = $ptGen->generate($link, true); - $ptGenInfo[$siteid]['data'] = $result; - sql_query(sprintf("update torrents set pt_gen = %s where id = %s", sqlesc(json_encode($ptGenInfo)), $id)) or sqlerr(__FILE__, __LINE__); + try { + $result = $ptGen->generate($link, true); + $ptGenInfo[$siteid]['data'] = $result; + sql_query(sprintf("update torrents set pt_gen = %s where id = %s", sqlesc(json_encode($ptGenInfo)), $id)) or sqlerr(__FILE__, __LINE__); + } catch (\Exception $e) { + $log = $e->getMessage() . ", trace: " . $e->getTraceAsString(); + do_log($log, 'error'); + } header("Location: " . get_protocol_prefix() . "$BASEURL/details.php?id=".htmlspecialchars($id)); break; }