From d123e8a8490f0a48d98f948fa7eaee5dc3cd0be7 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 16 May 2025 02:43:45 +0700 Subject: [PATCH] recover ptGen --- .gitignore | 1 + app/Console/Commands/Test.php | 4 +- .../MigrateTorrentsTableTextColumn.php | 2 +- app/Listeners/FetchTorrentPTGen.php | 45 +++++++++++++++++++ app/Logging/NexusFormatter.php | 2 +- app/Models/Setting.php | 5 +++ app/Models/TorrentExtra.php | 6 ++- app/Providers/EventServiceProvider.php | 2 + app/Repositories/ClaimRepository.php | 2 +- ...1_08_133552_create_torrent_extra_table.php | 1 + ...811_add_pt_gen_to_torrent_extras_table.php | 30 +++++++++++++ include/constants.php | 2 +- include/functions.php | 5 ++- nexus/PTGen/PTGen.php | 28 +++++++----- nexus/Torrent/Torrent.php | 19 ++++---- public/edit.php | 2 +- public/retriver.php | 2 +- public/takeedit.php | 20 ++++----- public/takeupload.php | 3 +- 19 files changed, 140 insertions(+), 41 deletions(-) create mode 100644 app/Listeners/FetchTorrentPTGen.php create mode 100644 database/migrations/2025_05_15_155811_add_pt_gen_to_torrent_extras_table.php diff --git a/.gitignore b/.gitignore index 5987a483..97485a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ yarn-error.log /imdb/cache /imdb/images /resources/geoip +/resources/upload .DS_Store /plugins auth.json diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index c4d9fc57..666f740c 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -7,11 +7,13 @@ use App\Models\ExamUser; use App\Models\Language; use App\Models\PersonalAccessToken; use App\Models\Torrent; +use App\Models\TorrentExtra; use App\Models\User; use App\Repositories\ExamRepository; use App\Repositories\SeedBoxRepository; use App\Repositories\UploadRepository; use Illuminate\Console\Command; +use Nexus\PTGen\PTGen; use NexusPlugin\Menu\Filament\MenuItemResource\Pages\ManageMenuItems; use NexusPlugin\Menu\MenuRepository; use NexusPlugin\Menu\Models\MenuItem; @@ -56,7 +58,7 @@ class Test extends Command */ public function handle() { - Language::updateTransStatus(); + } } diff --git a/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php b/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php index d11a31a5..50f7f9f7 100644 --- a/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php +++ b/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php @@ -29,7 +29,7 @@ class MigrateTorrentsTableTextColumn extends Command public function handle() { if (Schema::hasTable("torrent_extras") && Schema::hasColumn("torrents", "descr")) { - NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, created_at) select id, descr, technical_info, nfo, now() from torrents on duplicate key update torrent_id = values(torrent_id)"); + NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, pt_gen, created_at) select id, descr, technical_info, nfo, pt_gen, now() from torrents on duplicate key update torrent_id = values(torrent_id)"); } $columns = ["ori_descr", "descr", "nfo", "technical_info", "pt_gen"]; $sql = "alter table torrents "; diff --git a/app/Listeners/FetchTorrentPTGen.php b/app/Listeners/FetchTorrentPTGen.php new file mode 100644 index 00000000..fa4fc87d --- /dev/null +++ b/app/Listeners/FetchTorrentPTGen.php @@ -0,0 +1,45 @@ +model; + if (!$torrent instanceof Torrent) { + do_log("$logPrefix, not Torrent: " . Torrent::class); + return; + } + $torrentId = $torrent->id ?? 0; + if (empty($torrentId)) { + do_log("$logPrefix, no torrent_id"); + return; + } + $torrentTool = new PTGen(); + $torrentTool->updateTorrentPtGen($torrentId); + do_log("FetchTorrentPTGen for torrent: $torrentId done!"); + } +} diff --git a/app/Logging/NexusFormatter.php b/app/Logging/NexusFormatter.php index bb6a1a20..081a8307 100644 --- a/app/Logging/NexusFormatter.php +++ b/app/Logging/NexusFormatter.php @@ -19,7 +19,7 @@ class NexusFormatter $id = nexus()->getRequestId(); } $format = "[%datetime%] [" . $id . "] %channel%.%level_name%: %message% %context% %extra%\n"; - return tap(new LineFormatter($format, 'Y-m-d H:i:s', true, true), function ($formatter) { + return tap(new LineFormatter($format, "Y-m-d\TH:i:s.vP", true, true), function ($formatter) { $formatter->includeStacktraces(); }); } diff --git a/app/Models/Setting.php b/app/Models/Setting.php index dfc87d77..079095c4 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -120,6 +120,11 @@ class Setting extends NexusModel return self::get("main.defaultlang"); } + public static function getIsPTGenEnabled(): bool + { + return self::get("main.enable_pt_gen_system") == "yes"; + } + public static function getIsUseChallengeResponseAuthentication(): bool { return self::get("security.use_challenge_response_authentication") == "yes"; diff --git a/app/Models/TorrentExtra.php b/app/Models/TorrentExtra.php index 4bf9d80f..c6551953 100644 --- a/app/Models/TorrentExtra.php +++ b/app/Models/TorrentExtra.php @@ -9,7 +9,11 @@ class TorrentExtra extends NexusModel { public $timestamps = true; - protected $fillable = ['torrent_id', 'descr', 'ori_descr', 'media_info', 'nfo']; + protected $fillable = ['torrent_id', 'descr', 'ori_descr', 'media_info', 'nfo', 'pt_gen']; + + protected $casts = [ + 'pt_gen' => 'array', + ]; public function torrent() { diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 5d649103..2431ffd0 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -10,6 +10,7 @@ use App\Events\UserDeleted; use App\Events\UserDisabled; use App\Listeners\DeductUserBonusWhenTorrentDeleted; use App\Listeners\FetchTorrentImdb; +use App\Listeners\FetchTorrentPTGen; use App\Listeners\RemoveOauthTokens; use App\Listeners\RemoveSeedBoxRecordCache; use App\Listeners\SendEmailNotificationWhenTorrentCreated; @@ -41,6 +42,7 @@ class EventServiceProvider extends ServiceProvider ], TorrentCreated::class => [ FetchTorrentImdb::class, + FetchTorrentPTGen::class, SyncTorrentToElasticsearch::class, SyncTorrentToMeilisearch::class, SendEmailNotificationWhenTorrentCreated::class, diff --git a/app/Repositories/ClaimRepository.php b/app/Repositories/ClaimRepository.php index aa626ad5..953b821b 100644 --- a/app/Repositories/ClaimRepository.php +++ b/app/Repositories/ClaimRepository.php @@ -128,7 +128,7 @@ class ClaimRepository extends BaseRepository { $startOfThisMonth = Carbon::now()->startOfMonth(); $query = Claim::query() - ->selectRaw("uid, count(*) as count)") + ->selectRaw("uid, count(*) as count") ->where("created_at", "<", $startOfThisMonth) ->where(function (Builder $query) use ($startOfThisMonth) { $query->where('last_settle_at', '<', $startOfThisMonth)->orWhereNull('last_settle_at'); diff --git a/database/migrations/2025_01_08_133552_create_torrent_extra_table.php b/database/migrations/2025_01_08_133552_create_torrent_extra_table.php index dfb95b01..9c0b9c95 100644 --- a/database/migrations/2025_01_08_133552_create_torrent_extra_table.php +++ b/database/migrations/2025_01_08_133552_create_torrent_extra_table.php @@ -17,6 +17,7 @@ return new class extends Migration $table->mediumText('descr'); $table->text('media_info')->nullable(); $table->binary('nfo')->nullable(); + $table->mediumText('pt_gen')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2025_05_15_155811_add_pt_gen_to_torrent_extras_table.php b/database/migrations/2025_05_15_155811_add_pt_gen_to_torrent_extras_table.php new file mode 100644 index 00000000..828a84ee --- /dev/null +++ b/database/migrations/2025_05_15_155811_add_pt_gen_to_torrent_extras_table.php @@ -0,0 +1,30 @@ +mediumText('pt_gen')->nullable()->after('nfo'); + } + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('torrent_extras', function (Blueprint $table) { + // + }); + } +}; diff --git a/include/constants.php b/include/constants.php index eaa2aba6..318c3ee3 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ listLeechingSeedingStatus($CURUSER['id'], $torrentIdArr); + $ptGenInfo = TorrentExtra::query()->whereIn('torrent_id', $torrentIdArr)->pluck('pt_gen', 'torrent_id')->toArray(); $tagRep = new \App\Repositories\TagRepository(); $torrentTagCollection = \App\Models\TorrentTag::query()->whereIn('torrent_id', $torrentIdArr)->get(); $torrentTagResult = $torrentTagCollection->groupBy('torrent_id'); @@ -3664,7 +3665,7 @@ foreach ($rows as $row) print(""); if ($enableImdb || $enablePtGen) { - echo $torrent->renderTorrentsPageAverageRating($row); + echo $torrent->renderTorrentsPageAverageRating($row, $ptGenInfo[$row['id']] ?? []); } $act = ""; if ($CURUSER["dlicon"] != 'no' && $CURUSER["downloadpos"] != "no") diff --git a/nexus/PTGen/PTGen.php b/nexus/PTGen/PTGen.php index 8869e47b..537baffc 100644 --- a/nexus/PTGen/PTGen.php +++ b/nexus/PTGen/PTGen.php @@ -9,10 +9,12 @@ namespace Nexus\PTGen; use App\Models\Torrent; +use App\Models\TorrentExtra; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Nexus\Database\NexusDB; use Nexus\Imdb\Imdb; class PTGen @@ -163,12 +165,11 @@ HTML; private function request(string $url, bool $withoutCache = false): array { - global $Cache; $begin = microtime(true); $logPrefix = "url: $url"; $cacheKey = $this->getApiPointResultCacheKey($url); if (!$withoutCache) { - $cache = $Cache->get_value($cacheKey); + $cache = NexusDB::cache_get($cacheKey); if ($cache) { do_log("$logPrefix, from cache"); return $cache; @@ -196,7 +197,7 @@ HTML; throw new PTGenException($msg); } if ($this->isRawPTGen($bodyArr) || $this->isIyuu($bodyArr)) { - $Cache->cache_value($cacheKey, $bodyArr, 24 * 3600); + NexusDB::cache_put($cacheKey, $bodyArr, 24 * 3600); do_log("$logPrefix, success get from api point, use time: " . (microtime(true) - $begin)); $bodyArr['__updated_at'] = now()->toDateTimeString(); return $bodyArr; @@ -209,8 +210,7 @@ HTML; public function deleteApiPointResultCache($url) { - global $Cache; - $Cache->delete_value($this->getApiPointResultCacheKey($url)); + NexusDB::cache_del($this->getApiPointResultCacheKey($url)); } private function getApiPointResultCacheKey($url) @@ -413,11 +413,17 @@ HTML; return $results; } - public function updateTorrentPtGen(array $torrentInfo): bool|array + public function updateTorrentPtGen(int $id): bool|array { $now = Carbon::now(); - $log = "torrent: " . $torrentInfo['id']; - $arr = json_decode($torrentInfo['pt_gen'], true); + $log = "updateTorrentPtGen, torrent: " . $id; + $torrent = Torrent::query()->find($id); + if (empty($torrent)) { + do_log("$log, Torrent not found"); + return false; + } + $extra = $torrent->extra; + $arr = $extra->pt_gen; if (is_array($arr)) { if (!empty($arr['__updated_at'])) { $log .= ", updated_at: " . $arr['__updated_at']; @@ -431,7 +437,7 @@ HTML; } $link = $this->getLink($arr); } else { - $link = $torrentInfo['pt_gen']; + $link = $arr; } if (empty($link)) { do_log("$log, no link..."); @@ -449,13 +455,13 @@ HTML; do_log("$log, site: $site can not be updated: " . $exception->getMessage(), 'error'); } } - $siteIdAndRating = $this->listRatings($ptGenInfo, $torrentInfo['url'], $torrentInfo['descr']); + $siteIdAndRating = $this->listRatings($ptGenInfo, $torrent->url, $extra->descr); foreach ($siteIdAndRating as $key => $value) { $ptGenInfo[$key]['data']["__rating"] = $value; } $ptGenInfo['__link'] = $link; $ptGenInfo['__updated_at'] = $now->toDateTimeString(); - Torrent::query()->where('id', $torrentInfo['id'])->update(['pt_gen' => $ptGenInfo]); + TorrentExtra::query()->where('torrent_id', $id)->update(['pt_gen' => $ptGenInfo]); do_log("$log, success update"); return $ptGenInfo; } diff --git a/nexus/Torrent/Torrent.php b/nexus/Torrent/Torrent.php index 3f21b8a7..dcc61c57 100644 --- a/nexus/Torrent/Torrent.php +++ b/nexus/Torrent/Torrent.php @@ -3,6 +3,7 @@ namespace Nexus\Torrent; use App\Models\Setting; +use App\Models\TorrentExtra; use Nexus\Database\NexusDB; use Nexus\Imdb\Imdb; use Nexus\PTGen\PTGen; @@ -55,6 +56,12 @@ class Torrent return $snatchedList; } + public function listPTGenInfo(array $torrentIdArr) + { + $list = TorrentExtra::query()->whereIn('torrent_id', $torrentIdArr)->get(['torrent_id', 'pt_gen']); + } + + public function renderProgressBar($activeStatus, $progress): string { $color = '#aaa'; @@ -71,21 +78,15 @@ class Torrent return $result; } - public function renderTorrentsPageAverageRating(array $torrentInfo): string + public function renderTorrentsPageAverageRating(array $torrentInfo, array|string $ptGenInfo): string { static $ptGen; if (is_null($ptGen)) { $ptGen = new PTGen(); } -// $ptGenInfo = $torrentInfo['pt_gen']; -// if (!is_array($torrentInfo['pt_gen']) && is_string($torrentInfo['pt_gen'])) { -// $ptGenInfo = json_decode($ptGenInfo, true); -// } - $log = "torrent: " . $torrentInfo['id']; -// $siteIdAndRating = $ptGen->listRatings($ptGenInfo ?? [], $torrentInfo['url']); - $siteIdAndRating = $ptGen->listRatings([], $torrentInfo['url']); - $log .= "siteIdAndRating: " . json_encode($siteIdAndRating); + $siteIdAndRating = $ptGen->listRatings(is_array($ptGenInfo) && count($ptGenInfo) ? $ptGenInfo : [], $torrentInfo['url']); + $log .= ", siteIdAndRating: " . json_encode($siteIdAndRating); do_log($log); return $ptGen->buildRatingSpan($siteIdAndRating); } diff --git a/public/edit.php b/public/edit.php index a23af657..c293c8f1 100644 --- a/public/edit.php +++ b/public/edit.php @@ -8,7 +8,7 @@ $id = intval($_GET['id'] ?? 0); if (!$id) die(); -$res = sql_query("SELECT torrents.*, categories.mode as cat_mode, torrent_extras.media_info as technical_info, torrent_extras.descr FROM torrents LEFT JOIN categories ON category = categories.id left join torrent_extras on torrents.id = torrent_extras.torrent_id WHERE torrents.id = $id"); +$res = sql_query("SELECT torrents.*, categories.mode as cat_mode, torrent_extras.media_info as technical_info, torrent_extras.descr, torrent_extras.pt_gen FROM torrents LEFT JOIN categories ON category = categories.id left join torrent_extras on torrents.id = torrent_extras.torrent_id WHERE torrents.id = $id"); $row = mysql_fetch_assoc($res); if (!$row) die(); diff --git a/public/retriver.php b/public/retriver.php index 82669543..9f14730a 100644 --- a/public/retriver.php +++ b/public/retriver.php @@ -51,7 +51,7 @@ switch ($siteid) { $ptGen = new \Nexus\PTGen\PTGen(); try { - $ptGen->updateTorrentPtGen($row, $siteid); + $ptGen->updateTorrentPtGen($id); } catch (\Exception $e) { $log = $e->getMessage() . ", trace: " . $e->getTraceAsString(); do_log($log, 'error'); diff --git a/public/takeedit.php b/public/takeedit.php index c63c8bf3..8f1fdd41 100644 --- a/public/takeedit.php +++ b/public/takeedit.php @@ -47,19 +47,19 @@ $url = parse_imdb_id($_POST['url'] ?? ''); * add PT-Gen * @since 1.6 * - * @deprecated - * @since 1.9 */ -//if (!empty($_POST['pt_gen'])) { -// $postPtGen = $_POST['pt_gen']; -// $existsPtGenInfo = json_decode($row['pt_gen'], true) ?? []; -// $ptGen = new \Nexus\PTGen\PTGen(); -// if ($postPtGen != $ptGen->getLink($existsPtGenInfo)) { +if (!empty($_POST['pt_gen'])) { + $postPtGen = $_POST['pt_gen']; + $existsPtGenInfo = json_decode($row['pt_gen'], true) ?? []; + $ptGen = new \Nexus\PTGen\PTGen(); + if ($postPtGen != $ptGen->getLink($existsPtGenInfo)) { // $updateset[] = "pt_gen = " . sqlesc($postPtGen); -// } -//} else { + $extraUpdate["pt_gen"] = $postPtGen; + } +} else { // $updateset[] = "pt_gen = ''"; -//} + $extraUpdate["pt_gen"] = ""; +} //$updateset[] = "technical_info = " . sqlesc($_POST['technical_info'] ?? ''); $extraUpdate["media_info"] = $_POST['technical_info'] ?? ''; diff --git a/public/takeupload.php b/public/takeupload.php index 54998f99..bb77228a 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -353,13 +353,14 @@ $insert = [ 'cache_stamp' => time(), ]; /** - * migrate to extra table and remove pt_gen field + * migrate to extra table * @since 1.9 */ $extra = [ 'descr' => $descr, 'media_info' => $_POST['technical_info'] ?? '', 'nfo' => $nfo, + 'pt_gen' => $_POST['pt_gen'] ?? '', ]; if (isset($_POST['hr'][$catmod]) && isset(\App\Models\Torrent::$hrStatus[$_POST['hr'][$catmod]]) && user_can('torrent_hr')) { $insert['hr'] = $_POST['hr'][$catmod];