2021-03-16 21:12:27 +08:00
< ? php
namespace Nexus\Torrent ;
2022-04-14 00:52:28 +08:00
use App\Models\Setting ;
2021-06-08 10:42:39 +08:00
use Nexus\Database\NexusDB ;
2022-04-14 00:52:28 +08:00
use Nexus\Imdb\Imdb ;
use Nexus\PTGen\PTGen ;
2021-03-16 21:12:27 +08:00
class Torrent
{
/**
2021-03-17 01:21:35 +08:00
* get torrent seeding or leeching status , download progress of someone
2021-03-16 21:12:27 +08:00
*
* @ param int $uid
* @ param array $torrentIdArr
* @ return array
* @ throws \Nexus\Database\DatabaseException
*/
public function listLeechingSeedingStatus ( int $uid , array $torrentIdArr )
{
$torrentIdStr = implode ( ',' , $torrentIdArr );
//seeding or leeching, from peers
$whereStr = sprintf ( " userid = %s and torrent in (%s) " , sqlesc ( $uid ), $torrentIdStr );
2021-06-08 10:42:39 +08:00
$peerList = NexusDB :: getAll ( 'peers' , $whereStr , 'torrent, to_go' );
2021-03-16 21:12:27 +08:00
$peerList = array_column ( $peerList , 'to_go' , 'torrent' );
//download progress, from snatched
$sql = sprintf (
2021-03-17 01:21:35 +08:00
" select snatched.to_go, snatched.torrentid, torrents.size from snatched inner join torrents on snatched.torrentid = torrents.id where snatched.userid = %s and snatched.torrentid in (%s) " ,
2021-03-16 21:12:27 +08:00
sqlesc ( $uid ), $torrentIdStr
);
$snatchedList = [];
$res = sql_query ( $sql );
while ( $row = mysql_fetch_assoc ( $res )) {
$id = $row [ 'torrentid' ];
2021-03-17 01:21:35 +08:00
$activeStatus = 'inactivity' ;
2021-03-16 21:12:27 +08:00
if ( isset ( $peerList [ $id ])) {
2021-03-19 16:15:34 +08:00
if ( $peerList [ $id ] == 0 ) {
2021-03-16 21:12:27 +08:00
$activeStatus = 'seeding' ;
} else {
$activeStatus = 'leeching' ;
}
}
$realDownloaded = $row [ 'size' ] - $row [ 'to_go' ];
$progress = sprintf ( '%.4f' , $realDownloaded / $row [ 'size' ]);
$snatchedList [ $id ] = [
'finished' => $row [ 'to_go' ] == 0 ? 'yes' : 'no' ,
'progress' => $progress ,
'active_status' => $activeStatus ,
];
}
return $snatchedList ;
}
2021-03-17 01:21:35 +08:00
2022-04-14 00:52:28 +08:00
public function renderProgressBar ( $activeStatus , $progress ) : string
2021-03-17 01:21:35 +08:00
{
$color = '#aaa' ;
if ( $activeStatus == 'seeding' ) {
$color = 'green' ;
} elseif ( $activeStatus == 'leeching' ) {
2021-03-17 18:46:40 +08:00
$color = 'blue' ;
2021-03-17 01:21:35 +08:00
}
$progress = ( $progress * 100 ) . '%' ;
$result = sprintf (
'<div style="padding: 1px;margin-top: 2px;border: 1px solid #838383" title="%s"><div style="width: %s;background-color: %s;height: 2px"></div></div>' ,
2021-03-17 18:46:40 +08:00
$activeStatus . " $progress " , $progress , $color
2021-03-17 01:21:35 +08:00
);
return $result ;
}
2022-04-14 00:52:28 +08:00
public function renderTorrentsPageAverageRating ( array $torrentInfo ) : string
{
static $ptGen ;
if ( is_null ( $ptGen )) {
$ptGen = new PTGen ();
}
2022-06-08 18:37:27 +08:00
$ptGenInfo = $torrentInfo [ 'pt_gen' ];
2023-02-06 14:25:05 +08:00
if ( ! is_array ( $torrentInfo [ 'pt_gen' ]) && is_string ( $torrentInfo [ 'pt_gen' ])) {
2022-06-08 18:37:27 +08:00
$ptGenInfo = json_decode ( $ptGenInfo , true );
}
2022-04-14 00:52:28 +08:00
$log = " torrent: " . $torrentInfo [ 'id' ];
2022-04-14 14:05:51 +08:00
$siteIdAndRating = $ptGen -> listRatings ( $ptGenInfo ? ? [], $torrentInfo [ 'url' ]);
2022-04-14 00:52:28 +08:00
$log .= " siteIdAndRating: " . json_encode ( $siteIdAndRating );
do_log ( $log );
return $ptGen -> buildRatingSpan ( $siteIdAndRating );
}
2021-06-08 10:42:39 +08:00
}