Skip to content

Commit

Permalink
add command event:fire + fetch imdb when torrent created
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Feb 22, 2024
1 parent 5c6a2b1 commit 9efa5b2
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 24 deletions.
46 changes: 46 additions & 0 deletions app/Console/Commands/FireEvent.php
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions app/Console/Commands/TorrentFetchImdb.php
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions app/Events/TorrentCreated.php
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions app/Listeners/FetchTorrentImdb.php
Original file line number Diff line number Diff line change
@@ -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!");
}
}
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,9 @@ class EventServiceProvider extends ServiceProvider
SeedBoxRecordUpdated::class => [
RemoveSeedBoxRecordCache::class,
],
TorrentCreated::class => [
FetchTorrentImdb::class,
],
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/BonusRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logCom
->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();
Expand Down
31 changes: 30 additions & 1 deletion app/Repositories/TorrentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
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
{
Expand Down Expand Up @@ -862,4 +862,33 @@ public function loadPiecesHashCache($id = 0): array
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');
}
}

}
2 changes: 1 addition & 1 deletion include/constants.php
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
11 changes: 8 additions & 3 deletions include/globalfunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions public/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@
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))
Expand All @@ -337,7 +336,7 @@
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 ."&amp;type=1&amp;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=\"\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $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=\"\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $lang_details['text_someone_has_requested'].min(max(time()-$row['cache_stamp'],0),120) . $lang_details['text_please_be_patient'],1);
break;
}
case "1" :
Expand Down
34 changes: 18 additions & 16 deletions public/retriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@
$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;
Expand Down
4 changes: 4 additions & 0 deletions public/takeupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ function checkTorrentDict($dict, $key, $type = null)
'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];
Expand Down Expand Up @@ -446,6 +447,9 @@ function checkTorrentDict($dict, $key, $type = null)
$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)
{
Expand Down

0 comments on commit 9efa5b2

Please sign in to comment.