BioTorrents.de’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 6.3KB

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