123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- #declare(strict_types=1);
-
- include SERVER_ROOT.'/sections/torrents/functions.php';
-
- if (!empty($_GET['order_way']) && $_GET['order_way'] === 'asc') {
- $OrderWay = 'asc';
- } else {
- $OrderWay = 'desc';
- }
-
- if (empty($_GET['order_by']) || !isset(TorrentSearch::$SortOrders[$_GET['order_by']])) {
- $OrderBy = 'time';
- } else {
- $OrderBy = $_GET['order_by'];
- }
-
- $GroupResults = !isset($_GET['group_results']) || (int) $_GET['group_results'] !== 0;
- $Page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
-
- $Search = new TorrentSearch($GroupResults, $OrderBy, $OrderWay, $Page, TORRENTS_PER_PAGE);
- $Results = $Search->query($_GET);
-
- $Groups = $Search->get_groups();
- $NumResults = $Search->record_count();
-
- if ($Results === false) {
- json_die('error', 'Search returned an error. Make sure all parameters are valid and of the expected types.');
- }
-
- if ($NumResults === 0) {
- json_die('success', [
- 'results' => []
- ]);
- }
-
- $Bookmarks = Bookmarks::all_bookmarks('torrent');
-
- $JsonGroups = [];
- foreach ($Results as $Key => $GroupID) {
- $GroupInfo = $Groups[$GroupID];
- if (empty($GroupInfo['Torrents'])) {
- continue;
- }
-
- $CategoryID = $GroupInfo['CategoryID'];
- $Artists = $GroupInfo['Artists'];
- $GroupCatalogueNumber = $GroupInfo['CatalogueNumber'];
- $GroupName = $GroupInfo['Name'];
-
- if ($GroupResults) {
- $Torrents = $GroupInfo['Torrents'];
- $GroupTime = $MaxSize = $TotalLeechers = $TotalSeeders = $TotalSnatched = 0;
-
- foreach ($Torrents as $T) {
- $GroupTime = max($GroupTime, strtotime($T['Time']));
- $MaxSize = max($MaxSize, $T['Size']);
- $TotalLeechers += $T['Leechers'];
- $TotalSeeders += $T['Seeders'];
- $TotalSnatched += $T['Snatched'];
- }
- } else {
- $TorrentID = $Key;
- $Torrents = [$TorrentID => $GroupInfo['Torrents'][$TorrentID]];
- }
-
- $TagList = explode(' ', str_replace('_', '.', $GroupInfo['TagList']));
- $JsonArtists = [];
- $DisplayName = '';
-
- if (!empty($Artists)) {
- $DisplayName = Artists::display_artists($Artists, false, false, false);
- foreach ($Artists as $Artist) {
- $JsonArtists[] = [
- 'id' => (int) $Artist['id'],
- 'name' => $Artist['name']
- ];
- }
- }
-
- $JsonTorrents = [];
- foreach ($Torrents as $TorrentID => $Data) {
- // All of the individual torrents in the group
-
- $JsonTorrents[] = [
- 'torrentId' => (int) $TorrentID,
- 'authors' => $JsonArtists,
- 'platform' => $Data['Media'],
- 'format' => $Data['Container'],
- 'license' => $Data['Codec'],
- 'scope' => $Data['Resolution'],
- 'annotated' => $Data['Censored'],
- 'archive' => $Data['Archive'],
- 'fileCount' => (int) $Data['FileCount'],
- 'time' => $Data['Time'],
- 'size' => (int) $Data['Size'],
- 'snatches' => (int) $Data['Snatched'],
- 'seeders' => (int) $Data['Seeders'],
- 'leechers' => (int) $Data['Leechers'],
- 'isFreeleech' => (int) $Data['FreeTorrent'] === 1,
- 'isNeutralLeech' => (int) $Data['FreeTorrent'] === 2,
- 'isPersonalFreeleech' => $Data['PersonalFL'],
- 'canUseToken' => Torrents::can_use_token($Data),
- 'hasSnatched' => $Data['IsSnatched']
- ];
- }
-
- $JsonGroups[] = [
- 'groupId' => (int) $GroupID,
- 'groupName' => $GroupName,
- 'author' => $DisplayName,
- 'picture' => $GroupInfo['WikiImage'],
- 'tags' => $TagList,
- 'bookmarked' => (in_array($GroupID, $Bookmarks)),
- 'groupYear' => (int) $GroupInfo['Year'],
- 'groupTime' => (int) $GroupTime,
- 'accession' => $GroupInfo['CatalogueNumber'],
- 'lab' => $GroupInfo['Studio'],
- 'location' => $GroupInfo['Series'],
- 'maxSize' => (int) $MaxSize,
- 'totalSnatched' => (int) $TotalSnatched,
- 'totalSeeders' => (int) $TotalSeeders,
- 'totalLeechers' => (int) $TotalLeechers,
- 'torrents' => $JsonTorrents
- ];
- }
-
- json_print('success', [
- 'currentPage' => intval($Page),
- 'pages' => ceil($NumResults / TORRENTS_PER_PAGE),
- 'results' => $JsonGroups
- ]);
|