Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

torrents.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?
  2. if (isset($_GET['details'])) {
  3. if (in_array($_GET['details'], array('day','week','overall','snatched','data','seeded'))) {
  4. $Details = $_GET['details'];
  5. } else {
  6. print json_encode(array('status' => 'failure'));
  7. die();
  8. }
  9. } else {
  10. $Details = 'all';
  11. }
  12. // defaults to 10 (duh)
  13. $Limit = isset($_GET['limit']) ? intval($_GET['limit']) : 10;
  14. $Limit = in_array($Limit, array(10, 100, 250)) ? $Limit : 10;
  15. $WhereSum = (empty($Where)) ? '' : md5($Where);
  16. $BaseQuery = '
  17. SELECT
  18. t.ID,
  19. g.ID,
  20. g.Name,
  21. g.CategoryID,
  22. g.WikiImage,
  23. g.TagList,
  24. t.Media,
  25. g.Year,
  26. t.Snatched,
  27. t.Seeders,
  28. t.Leechers,
  29. ((t.Size * t.Snatched) + (t.Size * 0.5 * t.Leechers)) AS Data,
  30. t.Size
  31. FROM torrents AS t
  32. LEFT JOIN torrents_group AS g ON g.ID = t.GroupID';
  33. $OuterResults = [];
  34. if ($Details == 'all' || $Details == 'day') {
  35. if (!$TopTorrentsActiveLastDay = $Cache->get_value('top10tor_day_'.$Limit.$WhereSum)) {
  36. $DayAgo = time_minus(86400);
  37. $Query = $BaseQuery.' WHERE t.Seeders>0 AND ';
  38. if (!empty($Where)) { $Query .= $Where.' AND '; }
  39. $Query .= "
  40. t.Time>'$DayAgo'
  41. ORDER BY (t.Seeders + t.Leechers) DESC
  42. LIMIT $Limit;";
  43. $DB->query($Query);
  44. $TopTorrentsActiveLastDay = $DB->to_array(false, MYSQLI_NUM);
  45. $Cache->cache_value('top10tor_day_'.$Limit.$WhereSum, $TopTorrentsActiveLastDay, 3600 * 2);
  46. }
  47. $OuterResults[] = generate_torrent_json('Most Active Torrents Uploaded in the Past Day', 'day', $TopTorrentsActiveLastDay, $Limit);
  48. }
  49. if ($Details == 'all' || $Details == 'week') {
  50. if (!$TopTorrentsActiveLastWeek = $Cache->get_value('top10tor_week_'.$Limit.$WhereSum)) {
  51. $WeekAgo = time_minus(604800);
  52. $Query = $BaseQuery.' WHERE ';
  53. if (!empty($Where)) { $Query .= $Where.' AND '; }
  54. $Query .= "
  55. t.Time>'$WeekAgo'
  56. ORDER BY (t.Seeders + t.Leechers) DESC
  57. LIMIT $Limit;";
  58. $DB->query($Query);
  59. $TopTorrentsActiveLastWeek = $DB->to_array(false, MYSQLI_NUM);
  60. $Cache->cache_value('top10tor_week_'.$Limit.$WhereSum,$TopTorrentsActiveLastWeek,3600*6);
  61. }
  62. $OuterResults[] = generate_torrent_json('Most Active Torrents Uploaded in the Past Week', 'week', $TopTorrentsActiveLastWeek, $Limit);
  63. }
  64. if ($Details == 'all' || $Details == 'overall') {
  65. if (!$TopTorrentsActiveAllTime = $Cache->get_value("top10tor_overall_$Limit$WhereSum")) {
  66. $Query = $BaseQuery;
  67. if (!empty($Where)) {
  68. $Query .= " WHERE $Where";
  69. }
  70. $Query .= "
  71. ORDER BY (t.Seeders + t.Leechers) DESC
  72. LIMIT $Limit;";
  73. $DB->query($Query);
  74. $TopTorrentsActiveAllTime = $DB->to_array(false, MYSQLI_NUM);
  75. $Cache->cache_value("top10tor_overall_$Limit$WhereSum", $TopTorrentsActiveAllTime, 3600 * 6);
  76. }
  77. $OuterResults[] = generate_torrent_json('Most Active Torrents of All Time', 'overall', $TopTorrentsActiveAllTime, $Limit);
  78. }
  79. if (($Details == 'all' || $Details == 'snatched') && empty($Where)) {
  80. if (!$TopTorrentsSnatched = $Cache->get_value("top10tor_snatched_$Limit$WhereSum")) {
  81. $Query = $BaseQuery;
  82. $Query .= "
  83. ORDER BY t.Snatched DESC
  84. LIMIT $Limit;";
  85. $DB->query($Query);
  86. $TopTorrentsSnatched = $DB->to_array(false, MYSQLI_NUM);
  87. $Cache->cache_value("top10tor_snatched_$Limit$WhereSum", $TopTorrentsSnatched, 3600 * 6);
  88. }
  89. $OuterResults[] = generate_torrent_json('Most Snatched Torrents', 'snatched', $TopTorrentsSnatched, $Limit);
  90. }
  91. if (($Details == 'all' || $Details == 'data') && empty($Where)) {
  92. if (!$TopTorrentsTransferred = $Cache->get_value("top10tor_data_$Limit$WhereSum")) {
  93. $Query = $BaseQuery;
  94. $Query .= "
  95. ORDER BY Data DESC
  96. LIMIT $Limit;";
  97. $DB->query($Query);
  98. $TopTorrentsTransferred = $DB->to_array(false, MYSQLI_NUM);
  99. $Cache->cache_value("top10tor_data_$Limit$WhereSum", $TopTorrentsTransferred, 3600 * 6);
  100. }
  101. $OuterResults[] = generate_torrent_json('Most Data Transferred Torrents', 'data', $TopTorrentsTransferred, $Limit);
  102. }
  103. if (($Details == 'all' || $Details == 'seeded') && empty($Where)) {
  104. if (!$TopTorrentsSeeded = $Cache->get_value("top10tor_seeded_$Limit$WhereSum")) {
  105. $Query = $BaseQuery."
  106. ORDER BY t.Seeders DESC
  107. LIMIT $Limit;";
  108. $DB->query($Query);
  109. $TopTorrentsSeeded = $DB->to_array(false, MYSQLI_NUM);
  110. $Cache->cache_value("top10tor_seeded_$Limit$WhereSum", $TopTorrentsSeeded, 3600 * 6);
  111. }
  112. $OuterResults[] = generate_torrent_json('Best Seeded Torrents', 'seeded', $TopTorrentsSeeded, $Limit);
  113. }
  114. json_print("success", $OuterResults);
  115. function generate_torrent_json($Caption, $Tag, $Details, $Limit) {
  116. global $LoggedUser, $Categories;
  117. $results = [];
  118. foreach ($Details as $Detail) {
  119. list($TorrentID, $GroupID, $GroupName, $GroupCategoryID, $WikiImage, $TorrentTags,
  120. $Media, $GroupYear,
  121. $Snatched, $Seeders, $Leechers, $Data, $Size) = $Detail;
  122. $Artist = Artists::display_artists(Artists::get_artist($GroupID), false, false);
  123. $TagList = [];
  124. if ($TorrentTags != '') {
  125. $TorrentTags = explode(' ', $TorrentTags);
  126. foreach ($TorrentTags as $TagKey => $TagName) {
  127. $TagName = str_replace('_', '.', $TagName);
  128. $TagList[] = $TagName;
  129. }
  130. }
  131. // Append to the existing array.
  132. $results[] = array(
  133. 'torrentId' => (int)$TorrentID,
  134. 'groupId' => (int)$GroupID,
  135. 'artist' => $Artist,
  136. 'groupName' => $GroupName,
  137. 'groupCategory' => (int)$GroupCategoryID,
  138. 'groupYear' => (int)$GroupYear,
  139. 'media' => $Media,
  140. 'tags' => $TagList,
  141. 'snatched' => (int)$Snatched,
  142. 'seeders' => (int)$Seeders,
  143. 'leechers' => (int)$Leechers,
  144. 'data' => (int)$Data,
  145. 'size' => (int)$Size,
  146. 'wikiImage' => $WikiImage,
  147. );
  148. }
  149. return array(
  150. 'caption' => $Caption,
  151. 'tag' => $Tag,
  152. 'limit' => (int)$Limit,
  153. 'results' => $results
  154. );
  155. }
  156. ?>