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

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