diff --git a/README.md b/README.md index 78eab21..f915497 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,11 @@ Custom settings could be provided in the `/.env.local` file by overwriting defau #### Localization -* Join community translations by [Crowdin](https://crowdin.com/project/yggtracker) +Join community translations by [Crowdin](https://crowdin.com/project/yggtracker) + +#### API + +[Wiki reference](https://github.com/YGGverse/YGGtracker/wiki/API) #### Contribution @@ -95,7 +99,7 @@ git checkout -b my-pr-branch-name #### Support * [Issues](https://github.com/YGGverse/YGGtracker/issues) -* [Wiki](https://github.com/YGGverse/YGGtracker/wiki) +* [Documentation](https://github.com/YGGverse/YGGtracker/wiki) * [HowTo Yggdrasil](https://ygg.work.gd/yggdrasil:bittorrent:yggtracker) #### Blog diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 3d4ac71..1393397 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -608,6 +608,118 @@ class TorrentController extends AbstractController ); } + // #25 + // https://github.com/YGGverse/YGGtracker/issues/25 + #[Route( + '/api/torrents', + methods: + [ + 'GET' + ] + )] + public function jsonRecent( + Request $request, + UserService $userService, + TorrentService $torrentService, + ActivityService $activityService + ): Response + { + // Init request + $query = $request->get('query') ? explode(' ', urldecode($request->get('query'))) : []; + $page = $request->get('page') ? (int) $request->get('page') : 1; + + $locales = $request->get('locales') ? explode('|', $request->get('locales')) : explode('|', $this->getParameter('app.locales')); + $sensitive = $request->get('sensitive') ? (bool) $request->get('sensitive') : null; + + // Get total torrents + $total = $torrentService->findTorrentsTotal( + $query, + $locales, + $sensitive, + true, // approved only + ); + + // Create torrents list + $torrents = []; + foreach ($torrentService->findTorrents( + $query, + $locales, + $sensitive, + true, // approved only + $this->getParameter('app.pagination'), + ($page - 1) * $this->getParameter('app.pagination') + ) as $torrent) + { + // Read file + if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId())) + { + throw $this->createNotFoundException(); // @TODO exception + } + + // Generate url + $url = []; + foreach ($locales as $locale) + { + $url[$locale] = $this->generateUrl( + 'torrent_info', + [ + '_locale' => $locale, + 'torrentId' => $torrent->getId(), + ], + false + ); + } + + $torrents[] = + [ + 'torrent' => + [ + 'id' => $torrent->getId(), + 'added' => $torrent->getAdded(), + 'locales' => $torrent->getLocales(), + 'sensitive' => $torrent->isSensitive(), + 'file' => + [ + 'name' => $file->getName(), + 'size' => $file->getSize(), + ], + 'scrape' => + [ + 'seeders' => (int) $torrent->getSeeders(), + 'peers' => (int) $torrent->getPeers(), + 'leechers' => (int) $torrent->getLeechers(), + ], + 'url' => $url + ], + ]; + } + + $url = []; + foreach ($locales as $locale) + { + $url[$locale] = $this->generateUrl( + 'torrent_recent', + [ + '_locale' => $locale + ], + false + ); + } + + return $this->json( + [ + 'version' => time(), + 'tracker' => + [ + 'name' => $this->getParameter('app.name'), + 'version' => $this->getParameter('app.version'), + 'url' => $url + ], + 'torrents' => $torrents + ] + ); + } + // Forms #[Route( '/{_locale}/submit',