2011-09-16 08:00:13 +00:00
< ?
2013-04-19 08:00:55 +00:00
if ( isset ( $_GET [ 'details' ])) {
if ( in_array ( $_GET [ 'details' ], array ( 'day' , 'week' , 'overall' , 'snatched' , 'data' , 'seeded' ))) {
2011-09-16 08:00:13 +00:00
$Details = $_GET [ 'details' ];
} else {
print json_encode ( array ( 'status' => 'failure' ));
die ();
}
} else {
$Details = 'all' ;
}
// defaults to 10 (duh)
$Limit = isset ( $_GET [ 'limit' ]) ? intval ( $_GET [ 'limit' ]) : 10 ;
$Limit = in_array ( $Limit , array ( 10 , 100 , 250 )) ? $Limit : 10 ;
$WhereSum = ( empty ( $Where )) ? '' : md5 ( $Where );
$BaseQuery = " SELECT
t . ID ,
g . ID ,
g . Name ,
g . CategoryID ,
2013-04-30 18:18:07 +00:00
g . wikiImage ,
2011-09-16 08:00:13 +00:00
g . TagList ,
t . Format ,
t . Encoding ,
t . Media ,
t . Scene ,
t . HasLog ,
t . HasCue ,
t . LogScore ,
t . RemasterYear ,
g . Year ,
t . RemasterTitle ,
t . Snatched ,
t . Seeders ,
t . Leechers ,
2013-03-18 08:00:24 +00:00
(( t . Size * t . Snatched ) + ( t . Size * 0.5 * t . Leechers )) AS Data ,
2013-03-25 08:00:21 +00:00
g . ReleaseType ,
t . Size
2011-09-16 08:00:13 +00:00
FROM torrents AS t
LEFT JOIN torrents_group AS g ON g . ID = t . GroupID " ;
$OuterResults = array ();
2013-04-19 08:00:55 +00:00
if ( $Details == 'all' || $Details == 'day' ) {
2011-09-16 08:00:13 +00:00
if ( ! $TopTorrentsActiveLastDay = $Cache -> get_value ( 'top10tor_day_' . $Limit . $WhereSum )) {
2013-04-24 08:00:23 +00:00
$DayAgo = time_minus ( 86400 );
$Query = $BaseQuery . ' WHERE t.Seeders>0 AND ' ;
if ( ! empty ( $Where )) { $Query .= $Where . ' AND ' ; }
$Query .= "
t . Time > '$DayAgo'
ORDER BY ( t . Seeders + t . Leechers ) DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
2013-04-30 18:18:07 +00:00
$TopTorrentsActiveLastDay = $DB -> to_array (); // TODO: MYSQLI_NUM to avoid duplicate data in the cache (does that break something with generate_torrent_json?)
2013-04-24 08:00:23 +00:00
$Cache -> cache_value ( 'top10tor_day_' . $Limit . $WhereSum , $TopTorrentsActiveLastDay , 3600 * 2 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Most Active Torrents Uploaded in the Past Day' , 'day' , $TopTorrentsActiveLastDay , $Limit );
}
2013-04-19 08:00:55 +00:00
if ( $Details == 'all' || $Details == 'week' ) {
2011-09-16 08:00:13 +00:00
if ( ! $TopTorrentsActiveLastWeek = $Cache -> get_value ( 'top10tor_week_' . $Limit . $WhereSum )) {
2013-04-24 08:00:23 +00:00
$WeekAgo = time_minus ( 604800 );
$Query = $BaseQuery . ' WHERE ' ;
if ( ! empty ( $Where )) { $Query .= $Where . ' AND ' ; }
$Query .= "
t . Time > '$WeekAgo'
ORDER BY ( t . Seeders + t . Leechers ) DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
$TopTorrentsActiveLastWeek = $DB -> to_array ();
$Cache -> cache_value ( 'top10tor_week_' . $Limit . $WhereSum , $TopTorrentsActiveLastWeek , 3600 * 6 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Most Active Torrents Uploaded in the Past Week' , 'week' , $TopTorrentsActiveLastWeek , $Limit );
}
2013-04-19 08:00:55 +00:00
if ( $Details == 'all' || $Details == 'overall' ) {
2013-10-30 08:01:19 +00:00
if ( ! $TopTorrentsActiveAllTime = $Cache -> get_value ( " top10tor_overall_ $Limit $WhereSum " )) {
2013-04-24 08:00:23 +00:00
// IMPORTANT NOTE - we use WHERE t.Seeders>500 in order to speed up this query. You should remove it!
$Query = $BaseQuery ;
2013-10-30 08:01:19 +00:00
if ( ! empty ( $Where )) {
$Query .= " WHERE $Where " ;
} elseif ( $Details == 'all' ) {
$Query .= " WHERE t.Seeders > 500 " ;
}
2013-04-24 08:00:23 +00:00
$Query .= "
ORDER BY ( t . Seeders + t . Leechers ) DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
$TopTorrentsActiveAllTime = $DB -> to_array ();
2013-10-30 08:01:19 +00:00
$Cache -> cache_value ( " top10tor_overall_ $Limit $WhereSum " , $TopTorrentsActiveAllTime , 3600 * 6 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Most Active Torrents of All Time' , 'overall' , $TopTorrentsActiveAllTime , $Limit );
}
2013-04-19 08:00:55 +00:00
if (( $Details == 'all' || $Details == 'snatched' ) && empty ( $Where )) {
2013-10-30 08:01:19 +00:00
if ( ! $TopTorrentsSnatched = $Cache -> get_value ( " top10tor_snatched_ $Limit $WhereSum " )) {
2013-04-24 08:00:23 +00:00
$Query = $BaseQuery ;
$Query .= "
ORDER BY t . Snatched DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
$TopTorrentsSnatched = $DB -> to_array ();
2013-10-30 08:01:19 +00:00
$Cache -> cache_value ( " top10tor_snatched_ $Limit $WhereSum " , $TopTorrentsSnatched , 3600 * 6 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Most Snatched Torrents' , 'snatched' , $TopTorrentsSnatched , $Limit );
}
2013-04-19 08:00:55 +00:00
if (( $Details == 'all' || $Details == 'data' ) && empty ( $Where )) {
2013-10-30 08:01:19 +00:00
if ( ! $TopTorrentsTransferred = $Cache -> get_value ( " top10tor_data_ $Limit $WhereSum " )) {
2013-04-24 08:00:23 +00:00
// IMPORTANT NOTE - we use WHERE t.Snatched>100 in order to speed up this query. You should remove it!
$Query = $BaseQuery ;
2013-10-30 08:01:19 +00:00
if ( $Details == 'all' ) {
$Query .= " WHERE t.Snatched > 100 " ;
}
2013-04-24 08:00:23 +00:00
$Query .= "
ORDER BY Data DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
$TopTorrentsTransferred = $DB -> to_array ();
2013-10-30 08:01:19 +00:00
$Cache -> cache_value ( " top10tor_data_ $Limit $WhereSum " , $TopTorrentsTransferred , 3600 * 6 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Most Data Transferred Torrents' , 'data' , $TopTorrentsTransferred , $Limit );
}
2013-04-19 08:00:55 +00:00
if (( $Details == 'all' || $Details == 'seeded' ) && empty ( $Where )) {
2013-10-30 08:01:19 +00:00
if ( ! $TopTorrentsSeeded = $Cache -> get_value ( " top10tor_seeded_ $Limit $WhereSum " )) {
2013-04-24 08:00:23 +00:00
$Query = $BaseQuery . "
ORDER BY t . Seeders DESC
LIMIT $Limit ; " ;
$DB -> query ( $Query );
$TopTorrentsSeeded = $DB -> to_array ();
2013-10-30 08:01:19 +00:00
$Cache -> cache_value ( " top10tor_seeded_ $Limit $WhereSum " , $TopTorrentsSeeded , 3600 * 6 );
2011-09-16 08:00:13 +00:00
}
$OuterResults [] = generate_torrent_json ( 'Best Seeded Torrents' , 'seeded' , $TopTorrentsSeeded , $Limit );
}
print
json_encode (
array (
'status' => 'success' ,
2013-04-19 08:00:55 +00:00
'response' => $OuterResults
)
);
2011-09-16 08:00:13 +00:00
function generate_torrent_json ( $Caption , $Tag , $Details , $Limit ) {
2013-10-30 08:01:19 +00:00
global $LoggedUser , $Categories ;
2011-09-16 08:00:13 +00:00
$results = array ();
foreach ( $Details as $Detail ) {
2013-10-30 08:01:19 +00:00
list ( $TorrentID , $GroupID , $GroupName , $GroupCategoryID , $WikiImage , $TorrentTags ,
$Format , $Encoding , $Media , $Scene , $HasLog , $HasCue , $LogScore , $Year , $GroupYear ,
$RemasterTitle , $Snatched , $Seeders , $Leechers , $Data , $ReleaseType , $Size ) = $Detail ;
2011-09-16 08:00:13 +00:00
2012-10-11 08:00:15 +00:00
$Artist = Artists :: display_artists ( Artists :: get_artist ( $GroupID ), false , true );
2013-10-30 08:01:19 +00:00
$TruncArtist = substr ( $Artist , 0 , strlen ( $Artist ) - 3 );
2011-09-16 08:00:13 +00:00
2013-04-19 08:00:55 +00:00
$TagList = array ();
2011-09-16 08:00:13 +00:00
2013-04-19 08:00:55 +00:00
if ( $TorrentTags != '' ) {
2013-10-30 08:01:19 +00:00
$TorrentTags = explode ( ' ' , $TorrentTags );
2011-09-16 08:00:13 +00:00
foreach ( $TorrentTags as $TagKey => $TagName ) {
2013-10-30 08:01:19 +00:00
$TagName = str_replace ( '_' , '.' , $TagName );
2013-04-19 08:00:55 +00:00
$TagList [] = $TagName ;
2011-09-16 08:00:13 +00:00
}
}
// Append to the existing array.
$results [] = array (
2013-10-30 08:01:19 +00:00
'torrentId' => ( int ) $TorrentID ,
'groupId' => ( int ) $GroupID ,
2011-09-16 08:00:13 +00:00
'artist' => $TruncArtist ,
2011-10-27 08:00:15 +00:00
'groupName' => $GroupName ,
2013-10-30 08:01:19 +00:00
'groupCategory' => ( int ) $GroupCategory ,
'groupYear' => ( int ) $GroupYear ,
2011-10-27 08:00:15 +00:00
'remasterTitle' => $RemasterTitle ,
2011-09-16 08:00:13 +00:00
'format' => $Format ,
'encoding' => $Encoding ,
2011-12-31 08:00:21 +00:00
'hasLog' => $HasLog == 1 ,
'hasCue' => $HasCue == 1 ,
2011-09-16 08:00:13 +00:00
'media' => $Media ,
2011-12-31 08:00:21 +00:00
'scene' => $Scene == 1 ,
2013-10-30 08:01:19 +00:00
'year' => ( int ) $Year ,
2011-09-16 08:00:13 +00:00
'tags' => $TagList ,
2013-10-30 08:01:19 +00:00
'snatched' => ( int ) $Snatched ,
'seeders' => ( int ) $Seeders ,
'leechers' => ( int ) $Leechers ,
'data' => ( int ) $Data ,
'size' => ( int ) $Size ,
2011-09-16 08:00:13 +00:00
);
}
return array (
'caption' => $Caption ,
'tag' => $Tag ,
2013-10-30 08:01:19 +00:00
'limit' => ( int ) $Limit ,
2011-09-16 08:00:13 +00:00
'results' => $results
);
}
?>