Browse Source

update json api #26

main
ghost 1 year ago
parent
commit
e1e3bfb2ce
  1. 114
      src/Controller/TorrentController.php

114
src/Controller/TorrentController.php

@ -707,21 +707,65 @@ class TorrentController extends AbstractController
); );
// Init request // Init request
$query = $request->get('query') ? explode(' ', urldecode($request->get('query'))) : []; $query = $request->get('query') ?
$page = $request->get('page') ? (int) $request->get('page') : 1; explode(' ', urldecode($request->get('query'))) : [];
$locales = $request->get('locales') ? explode('|', $request->get('locales')) : explode('|', $this->getParameter('app.locales')); $page = $request->get('page') ?
$categories = $request->get('categories') ? explode('|', $request->get('categories')) : explode('|', $this->getParameter('app.categories')); (int) $request->get('page') : 1;
$sensitive = $request->get('sensitive') ? (bool) $request->get('sensitive') : null;
$yggdrasil = $request->get('yggdrasil') ? (bool) $request->get('yggdrasil') : false; $filter = $request->get('filter') ?
true : false;
if ($request->get('locales'))
{
$locales = explode('|', $request->get('locales'));
}
else
{
$locales = $user->getLocales();
}
if ($request->get('categories'))
{
$categories = explode('|', $request->get('categories'));
}
else
{
$categories = $user->getCategories();
}
switch ($request->get('sensitive'))
{
case 'true':
$sensitive = true;
break;
case 'false':
$sensitive = false;
break;
default:
$sensitive = $user->isSensitive() ? false : null;
}
switch ($request->get('yggdrasil'))
{
case 'true':
$yggdrasil = true;
break;
case 'false':
$yggdrasil = false;
break;
default:
$yggdrasil = $user->isYggdrasil();
}
// Init trackers // Init trackers
$trackers = explode('|', $this->getParameter('app.trackers')); $trackers = explode('|', $this->getParameter('app.trackers'));
// Get total torrents // Get total torrents
$total = $torrentService->findTorrentsTotal( $total = $torrentService->findTorrentsTotal(
$user->getId(), $filter ? 0 : $user->getId(),
$query, $query,
$locales, $locales,
$categories, $categories,
@ -733,7 +777,7 @@ class TorrentController extends AbstractController
// Create torrents list // Create torrents list
$torrents = []; $torrents = [];
foreach ($torrentService->findTorrents( foreach ($torrentService->findTorrents(
$user->getId(), $filter ? 0 : $user->getId(),
$query, $query,
$locales, $locales,
$categories, $categories,
@ -753,28 +797,15 @@ class TorrentController extends AbstractController
// Apply yggdrasil filters // Apply yggdrasil filters
$file = $this->filterYggdrasil($file, $yggdrasil); $file = $this->filterYggdrasil($file, $yggdrasil);
// Generate url
$url = [];
foreach ($locales as $locale)
{
$url[$locale] = $this->generateUrl(
'torrent_info',
[
'_locale' => $locale,
'torrentId' => $torrent->getId(),
],
false
);
}
$torrents[] = $torrents[] =
[ [
'torrent' => 'torrent' =>
[ [
'id' => $torrent->getId(), 'id' => $torrent->getId(),
'added' => $torrent->getAdded(), 'added' => $torrent->getAdded(),
'locales' => $torrent->getLocales(), 'locales' => $torrent->getLocales(),
'sensitive' => $torrent->isSensitive(), 'categories' => $torrent->getCategories(),
'sensitive' => $torrent->isSensitive(),
'file' => 'file' =>
[ [
'name' => $file->getName(), 'name' => $file->getName(),
@ -796,7 +827,7 @@ class TorrentController extends AbstractController
], ],
false false
), ),
'urn' => $file->getMagnetLink() //'urn' => $file->getMagnetLink()
], ],
'scrape' => 'scrape' =>
[ [
@ -804,23 +835,18 @@ class TorrentController extends AbstractController
'peers' => (int) $torrent->getPeers(), 'peers' => (int) $torrent->getPeers(),
'leechers' => (int) $torrent->getLeechers(), 'leechers' => (int) $torrent->getLeechers(),
], ],
'url' => $url 'url' => $this->generateUrl(
'torrent_info',
[
'_locale' => $user->getLocale(),
'torrentId' => $torrent->getId(),
],
false
)
], ],
]; ];
} }
$url = [];
foreach ($locales as $locale)
{
$url[$locale] = $this->generateUrl(
'torrent_recent',
[
'_locale' => $locale
],
false
);
}
return $this->json( return $this->json(
[ [
'version' => time(), 'version' => time(),
@ -828,7 +854,13 @@ class TorrentController extends AbstractController
[ [
'name' => $this->getParameter('app.name'), 'name' => $this->getParameter('app.name'),
'version' => $this->getParameter('app.version'), 'version' => $this->getParameter('app.version'),
'url' => $url 'url' => $this->generateUrl(
'torrent_recent',
[
'_locale' => $user->getLocale()
],
false
)
], ],
'torrents' => $torrents 'torrents' => $torrents
] ]

Loading…
Cancel
Save