Skip to content

Commit

Permalink
update json api #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost committed Nov 4, 2023
1 parent 31aad63 commit e1e3bfb
Showing 1 changed file with 73 additions and 41 deletions.
114 changes: 73 additions & 41 deletions src/Controller/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,21 +707,65 @@ public function jsonRecent(
);

// Init request
$query = $request->get('query') ? explode(' ', urldecode($request->get('query'))) : [];
$page = $request->get('page') ? (int) $request->get('page') : 1;
$query = $request->get('query') ?
explode(' ', urldecode($request->get('query'))) : [];

$locales = $request->get('locales') ? explode('|', $request->get('locales')) : explode('|', $this->getParameter('app.locales'));
$categories = $request->get('categories') ? explode('|', $request->get('categories')) : explode('|', $this->getParameter('app.categories'));
$sensitive = $request->get('sensitive') ? (bool) $request->get('sensitive') : null;
$page = $request->get('page') ?
(int) $request->get('page') : 1;

$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
$trackers = explode('|', $this->getParameter('app.trackers'));

// Get total torrents
$total = $torrentService->findTorrentsTotal(
$user->getId(),
$filter ? 0 : $user->getId(),
$query,
$locales,
$categories,
Expand All @@ -733,7 +777,7 @@ public function jsonRecent(
// Create torrents list
$torrents = [];
foreach ($torrentService->findTorrents(
$user->getId(),
$filter ? 0 : $user->getId(),
$query,
$locales,
$categories,
Expand All @@ -753,28 +797,15 @@ public function jsonRecent(
// Apply yggdrasil filters
$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[] =
[
'torrent' =>
[
'id' => $torrent->getId(),
'added' => $torrent->getAdded(),
'locales' => $torrent->getLocales(),
'sensitive' => $torrent->isSensitive(),
'id' => $torrent->getId(),
'added' => $torrent->getAdded(),
'locales' => $torrent->getLocales(),
'categories' => $torrent->getCategories(),
'sensitive' => $torrent->isSensitive(),
'file' =>
[
'name' => $file->getName(),
Expand All @@ -796,39 +827,40 @@ public function jsonRecent(
],
false
),
'urn' => $file->getMagnetLink()
//'urn' => $file->getMagnetLink()
],
'scrape' =>
[
'seeders' => (int) $torrent->getSeeders(),
'peers' => (int) $torrent->getPeers(),
'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(
[
'version' => time(),
'tracker' =>
[
'name' => $this->getParameter('app.name'),
'version' => $this->getParameter('app.version'),
'url' => $url
'url' => $this->generateUrl(
'torrent_recent',
[
'_locale' => $user->getLocale()
],
false
)
],
'torrents' => $torrents
]
Expand Down

0 comments on commit e1e3bfb

Please sign in to comment.