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

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