From e1f952470c5868e1c194f2880fb3f8e836d6e40b Mon Sep 17 00:00:00 2001 From: xiaomlove <353856593@qq.com> Date: Fri, 19 Mar 2021 14:19:02 +0800 Subject: [PATCH] unify torrent rating rendering --- nexus/Imdb/Imdb.php | 24 ++++++++-------------- nexus/PTGen/PTGen.php | 47 ++++++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/nexus/Imdb/Imdb.php b/nexus/Imdb/Imdb.php index 652ecda7..d84be446 100644 --- a/nexus/Imdb/Imdb.php +++ b/nexus/Imdb/Imdb.php @@ -4,6 +4,7 @@ namespace Nexus\Imdb; use Imdb\Config; use Imdb\Title; +use Nexus\PTGen\PTGen; class Imdb { @@ -13,6 +14,8 @@ class Imdb private $pages = array('Title', 'Credits', 'ReleaseInfo', ); + private $ptGen; + public function __construct() { $config = new Config(); @@ -265,25 +268,14 @@ class Imdb if (!is_numeric($rating)) { $rating = $defaultRating; } - $site = 'imdb'; - $result = '
'; - $result .= sprintf( - '
%s%s
', - 'pic/imdb2.png', $site, $site, $rating - ); - $result .= '
'; - return $result; + return $this->getPtGen()->buildRatingSpan([PTGen::SITE_IMDB => $rating]); } - public function renderHotAndClassic() + public function getPtGen() { - global $showextinfo, $showmovies; - - $shouldDisplay = ($showextinfo['imdb'] == 'yes' || get_setting('main.enable_pt_gen_system')) && ($showmovies['hot'] == "yes" || $showmovies['classic'] == "yes"); - if (!$shouldDisplay) { - return ''; + if (empty($this->ptGen)) { + $this->ptGen = new PTGen(); } - - + return $this->ptGen; } } \ No newline at end of file diff --git a/nexus/PTGen/PTGen.php b/nexus/PTGen/PTGen.php index 454c9830..0efccd69 100644 --- a/nexus/PTGen/PTGen.php +++ b/nexus/PTGen/PTGen.php @@ -40,11 +40,7 @@ class PTGen public function __construct() { $setting = get_setting('main'); - if (empty($setting['pt_gen_api_point'])) { - do_log("empty PT-Gen api point", 'warning'); - } else { - $this->setApiPoint($setting['pt_gen_api_point']); - } + $this->setApiPoint($setting['pt_gen_api_point'] ?? ''); } public function getApiPoint(): string @@ -230,24 +226,51 @@ HTML; public function renderTorrentsPageAverageRating(array $ptGenData) { - $result = '
'; - $count = 1; + $siteIdAndRating = []; foreach (self::$validSites as $site => $info) { $rating = $ptGenData[$site]['data']["{$site}_rating_average"] ?? ''; if (empty($rating)) { continue; } + $siteIdAndRating[$site] = $rating; + } + return $this->buildRatingSpan($siteIdAndRating); + } + + public function buildRatingSpan(array $siteIdAndRating) + { + $result = '
'; + $count = 1; + $ratingIcons = []; + foreach (self::$validSites as $site => $info) { + if (!isset($siteIdAndRating[$site])) { + continue; + } + $rating = $siteIdAndRating[$site]; + if (empty($rating)) { + continue; + } if ($count > 2) { //only show the first two break; } - $result .= sprintf( - '
%s%s
', - $info['rating_average_img'], $site, $site, $rating - ); + $ratingIcons[] = $this->getRatingIcon($site, $rating); $count++; } - $result .= '
'; + if (empty($ratingIcons)) { + $ratingIcons[] = $this->getRatingIcon(self::SITE_IMDB, 'N/A'); + $ratingIcons[] = $this->getRatingIcon(self::SITE_DOUBAN, 'N/A'); + } + $result .= implode("", $ratingIcons) . '
'; + return $result; + } + + public function getRatingIcon($siteId, $rating) + { + $result = sprintf( + '
%s%s
', + self::$validSites[$siteId]['rating_average_img'], $siteId, $siteId, $rating + ); return $result; } } \ No newline at end of file