mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +08:00
add command event:fire + fetch imdb when torrent created
This commit is contained in:
46
app/Console/Commands/FireEvent.php
Normal file
46
app/Console/Commands/FireEvent.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Events\TorrentCreated;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class FireEvent extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'event:fire {--name=} {--id=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Fire a event, options: --name, --id';
|
||||
|
||||
protected array $eventMaps = [
|
||||
"torrent_created" => 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;
|
||||
}
|
||||
}
|
||||
40
app/Console/Commands/TorrentFetchImdb.php
Normal file
40
app/Console/Commands/TorrentFetchImdb.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Events\TorrentCreated;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class TorrentFetchImdb extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'torrent:fetch_imdb {torrent_id}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Fetch torrent imdb info';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$torrentId = $this->argument("torrent_id");
|
||||
$this->info("torrentId: $torrentId");
|
||||
if (!$torrentId) {
|
||||
$this->error("require argument torrent_id");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
TorrentCreated::dispatch($torrentId);
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
38
app/Events/TorrentCreated.php
Normal file
38
app/Events/TorrentCreated.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TorrentCreated
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $torrentId;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $torrentId)
|
||||
{
|
||||
$this->torrentId = $torrentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('channel-name');
|
||||
}
|
||||
}
|
||||
35
app/Listeners/FetchTorrentImdb.php
Normal file
35
app/Listeners/FetchTorrentImdb.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Models\Torrent;
|
||||
use App\Repositories\TorrentRepository;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
||||
class FetchTorrentImdb implements ShouldQueue
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
$torrentId = $event->torrentId;
|
||||
$torrentRep = new TorrentRepository();
|
||||
$torrentRep->fetchImdb($torrentId);
|
||||
do_log("fetchImdb for torrent: $torrentId done!");
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.9');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-02-22');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-02-23');
|
||||
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
||||
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
||||
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
||||
|
||||
@@ -1192,9 +1192,14 @@ function executeCommand($command, $format = 'string', $artisan = false, $excepti
|
||||
do_log("command: $command");
|
||||
$result = exec($command, $output, $result_code);
|
||||
$outputString = implode("\n", $output);
|
||||
do_log(sprintf('result_code: %s, result: %s, output: %s', $result_code, $result, $outputString));
|
||||
if ($exception && $result_code != 0) {
|
||||
throw new \RuntimeException($outputString);
|
||||
$log = sprintf('result_code: %s, result: %s, output: %s', $result_code, $result, $outputString);
|
||||
if ($result_code != 0) {
|
||||
do_log($log, "error");
|
||||
if ($exception) {
|
||||
throw new \RuntimeException($outputString);
|
||||
}
|
||||
} else {
|
||||
do_log($log);
|
||||
}
|
||||
return $format == 'string' ? $outputString : $output;
|
||||
}
|
||||
|
||||
@@ -327,7 +327,6 @@ JS;
|
||||
if ($imdb_id && $showextinfo['imdb'] == 'yes' && $CURUSER['showimdb'] != 'no')
|
||||
{
|
||||
$thenumbers = $imdb_id;
|
||||
|
||||
$Cache->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']."<a href=\"retriver.php?id=". $id ."&type=1&siteid=1\">".$lang_details['text_here_to_retrieve'] . $lang_details['text_imdb'],1);
|
||||
else
|
||||
tr($lang_details['text_imdb'] . $lang_details['row_info'] , "<img src=\"pic/progressbar.gif\" alt=\"\" /> " . $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'] , "<img src=\"pic/progressbar.gif\" alt=\"\" /> " . $lang_details['text_someone_has_requested'].min(max(time()-$row['cache_stamp'],0),120) . $lang_details['text_please_be_patient'],1);
|
||||
break;
|
||||
}
|
||||
case "1" :
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user