From 9efa5b2ae569f2bf81564b7268dd25b19ebd9008 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 23 Feb 2024 02:38:42 +0800 Subject: [PATCH] add command event:fire + fetch imdb when torrent created --- app/Console/Commands/FireEvent.php | 46 +++++++++++++++++++++++ app/Console/Commands/TorrentFetchImdb.php | 40 ++++++++++++++++++++ app/Events/TorrentCreated.php | 38 +++++++++++++++++++ app/Listeners/FetchTorrentImdb.php | 35 +++++++++++++++++ app/Providers/EventServiceProvider.php | 5 +++ app/Repositories/BonusRepository.php | 2 +- app/Repositories/TorrentRepository.php | 31 ++++++++++++++- include/constants.php | 2 +- include/globalfunctions.php | 11 ++++-- public/details.php | 3 +- public/retriver.php | 34 +++++++++-------- public/takeupload.php | 4 ++ 12 files changed, 227 insertions(+), 24 deletions(-) create mode 100644 app/Console/Commands/FireEvent.php create mode 100644 app/Console/Commands/TorrentFetchImdb.php create mode 100644 app/Events/TorrentCreated.php create mode 100644 app/Listeners/FetchTorrentImdb.php diff --git a/app/Console/Commands/FireEvent.php b/app/Console/Commands/FireEvent.php new file mode 100644 index 00000000..96c1c864 --- /dev/null +++ b/app/Console/Commands/FireEvent.php @@ -0,0 +1,46 @@ + TorrentCreated::class + ]; + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { + $name = $this->option('name'); + $id = $this->option('id'); + $log = "FireEvent, name: $name, id: $id"; + if (isset($this->eventMaps[$name])) { + $result = call_user_func([$this->eventMaps[$name], "dispatch"], $id); + $this->info("$log, success call dispatch, result: " . var_export($result, true)); + } else { + $this->error("$log, no event match this name"); + } + return Command::SUCCESS; + } +} diff --git a/app/Console/Commands/TorrentFetchImdb.php b/app/Console/Commands/TorrentFetchImdb.php new file mode 100644 index 00000000..2c202260 --- /dev/null +++ b/app/Console/Commands/TorrentFetchImdb.php @@ -0,0 +1,40 @@ +argument("torrent_id"); + $this->info("torrentId: $torrentId"); + if (!$torrentId) { + $this->error("require argument torrent_id"); + return Command::FAILURE; + } + TorrentCreated::dispatch($torrentId); + return Command::SUCCESS; + } +} diff --git a/app/Events/TorrentCreated.php b/app/Events/TorrentCreated.php new file mode 100644 index 00000000..5ba47795 --- /dev/null +++ b/app/Events/TorrentCreated.php @@ -0,0 +1,38 @@ +torrentId = $torrentId; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Listeners/FetchTorrentImdb.php b/app/Listeners/FetchTorrentImdb.php new file mode 100644 index 00000000..ad8b211a --- /dev/null +++ b/app/Listeners/FetchTorrentImdb.php @@ -0,0 +1,35 @@ +torrentId; + $torrentRep = new TorrentRepository(); + $torrentRep->fetchImdb($torrentId); + do_log("fetchImdb for torrent: $torrentId done!"); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 0428d63e..996ba5a9 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,7 +3,9 @@ namespace App\Providers; use App\Events\SeedBoxRecordUpdated; +use App\Events\TorrentCreated; use App\Events\TorrentUpdated; +use App\Listeners\FetchTorrentImdb; use App\Listeners\RemoveSeedBoxRecordCache; use App\Listeners\SyncTorrentToEs; use Illuminate\Auth\Events\Registered; @@ -28,6 +30,9 @@ class EventServiceProvider extends ServiceProvider SeedBoxRecordUpdated::class => [ RemoveSeedBoxRecordCache::class, ], + TorrentCreated::class => [ + FetchTorrentImdb::class, + ], ]; /** diff --git a/app/Repositories/BonusRepository.php b/app/Repositories/BonusRepository.php index fe570a5b..4f907346 100644 --- a/app/Repositories/BonusRepository.php +++ b/app/Repositories/BonusRepository.php @@ -350,7 +350,7 @@ class BonusRepository extends BaseRepository ->where('seedbonus', $oldUserBonus) ->update($userUpdates); if ($affectedRows != 1) { - do_log("update user seedbonus affected rows != 1, query: " . last_query(), 'error'); + do_log("update user seedbonus affected rows: ".$affectedRows." != 1, query: " . last_query(), 'error'); throw new \RuntimeException("Update user seedbonus fail."); } $nowStr = now()->toDateTimeString(); diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index f82d8cab..31d88e54 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -34,8 +34,8 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; use Nexus\Database\NexusDB; +use Nexus\Imdb\Imdb; use Rhilip\Bencode\Bencode; -use function Sodium\compare; class TorrentRepository extends BaseRepository { @@ -862,4 +862,33 @@ HTML; return compact('total', 'success'); } + public function fetchImdb(int $torrentId): void + { + $torrent = Torrent::query()->findOrFail($torrentId, ["id", "url", "cache_stamp"]); + $imdb_id = parse_imdb_id($torrent->url); + $log = sprintf("fetchImdb torrentId: %s", $torrentId); + if (!$imdb_id) { + do_log("$log, no imdb_id"); + return; + } + $thenumbers = $imdb_id; + $imdb = new Imdb(); + $torrent->cache_stamp = time(); + $torrent->save(); + + $imdb->purgeSingle($imdb_id); + + try { + $imdb->updateCache($imdb_id); + NexusDB::cache_del('imdb_id_'.$thenumbers.'_movie_name'); + NexusDB::cache_del('imdb_id_'.$thenumbers.'_large', true); + NexusDB::cache_del('imdb_id_'.$thenumbers.'_median', true); + NexusDB::cache_del('imdb_id_'.$thenumbers.'_minor', true); + do_log("$log, done"); + } catch (\Exception $e) { + $log .= ", error: " . $e->getMessage() . ", trace: " . $e->getTraceAsString(); + do_log($log, 'error'); + } + } + } diff --git a/include/constants.php b/include/constants.php index d7f50f95..a783f8de 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ new_page('imdb_id_'.$thenumbers.'_large', 3600*24, true); if (!$Cache->get_page()){ switch ($imdb->getCacheStatus($imdb_id)) @@ -337,7 +336,7 @@ JS; if($row['cache_stamp']==0 || ($row['cache_stamp'] != 0 && (time()-$row['cache_stamp']) > 120)) //not exist or timed out tr($lang_details['text_imdb'] . $lang_details['row_info'] , $lang_details['text_imdb'] . $lang_details['text_not_ready']."".$lang_details['text_here_to_retrieve'] . $lang_details['text_imdb'],1); else - tr($lang_details['text_imdb'] . $lang_details['row_info'] , "\"\"       " . $lang_details['text_someone_has_requested'] . $lang_details['text_imdb'] . " ".min(max(time()-$row['cache_stamp'],0),120) . $lang_details['text_please_be_patient'],1); + tr($lang_details['text_imdb'] . $lang_details['row_info'] , "\"\"       " . $lang_details['text_someone_has_requested'].min(max(time()-$row['cache_stamp'],0),120) . $lang_details['text_please_be_patient'],1); break; } case "1" : diff --git a/public/retriver.php b/public/retriver.php index 4d5360c0..82669543 100644 --- a/public/retriver.php +++ b/public/retriver.php @@ -23,22 +23,24 @@ switch ($siteid) $imdb_id = parse_imdb_id($row["url"]); if ($imdb_id) { - $thenumbers = $imdb_id; - $imdb = new \Nexus\Imdb\Imdb(); - set_cachetimestamp($id,"cache_stamp"); - - $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'); - } +// $thenumbers = $imdb_id; +// $imdb = new \Nexus\Imdb\Imdb(); +// set_cachetimestamp($id,"cache_stamp"); +// +// $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'); +// } + $torrentRep = new \App\Repositories\TorrentRepository(); + $torrentRep->fetchImdb($id); nexus_redirect(getSchemeAndHttpHost() . "/details.php?id=$id"); } break; diff --git a/public/takeupload.php b/public/takeupload.php index 886e2986..bcdfd47f 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -349,6 +349,7 @@ $insert = [ 'technical_info' => $_POST['technical_info'] ?? '', 'cover' => $cover, 'pieces_hash' => sha1($info['pieces']), + 'cache_stamp' => time(), ]; if (isset($_POST['hr'][$catmod]) && isset(\App\Models\Torrent::$hrStatus[$_POST['hr'][$catmod]]) && user_can('torrent_hr')) { $insert['hr'] = $_POST['hr'][$catmod]; @@ -446,6 +447,9 @@ $searchRep->addTorrent($id); $meiliSearch = new \App\Repositories\MeiliSearchRepository(); $meiliSearch->doImportFromDatabase($id); +//trigger event +executeCommand("event:fire --name=torrent_created --id=$id", "string", true, false); + //===notify people who voted on offer thanks CoLdFuSiOn :) if ($is_offer) {