2020-04-03 23:59:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Feed;
|
|
|
|
|
|
|
|
use App\Magnet\MagnetGenerator;
|
|
|
|
use App\Magnetico\Entity\Torrent;
|
|
|
|
use App\Magnetico\Repository\TorrentRepository;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
|
|
|
use Suin\RSSWriter\{Channel, Feed, Item};
|
|
|
|
use Symfony\Component\Routing\{Generator\UrlGeneratorInterface, RouterInterface};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates RSS feed.
|
|
|
|
*
|
|
|
|
* @see https://www.bittorrent.org/beps/bep_0036.html
|
|
|
|
*/
|
|
|
|
class RssGenerator
|
|
|
|
{
|
|
|
|
private const PER_PAGE = 1000;
|
|
|
|
private const MIME_TYPE = 'application/x-bittorrent';
|
|
|
|
|
|
|
|
private TorrentRepository $repo;
|
|
|
|
private RouterInterface $router;
|
|
|
|
private MagnetGenerator $magnetGenerator;
|
|
|
|
|
|
|
|
public function __construct(TorrentRepository $repo, RouterInterface $router, MagnetGenerator $magnetGenerator)
|
|
|
|
{
|
|
|
|
$this->repo = $repo;
|
|
|
|
$this->router = $router;
|
|
|
|
$this->magnetGenerator = $magnetGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateLast(int $page): string
|
|
|
|
{
|
2020-04-04 00:22:37 +00:00
|
|
|
$qb = $this->createLastTorrentsQueryBuilder(self::PER_PAGE, $page * self::PER_PAGE);
|
2020-04-03 23:59:47 +00:00
|
|
|
|
2020-04-04 00:22:37 +00:00
|
|
|
$feed = $this->createFeedFromTorrents($qb->getQuery()->getResult());
|
2020-04-03 23:59:47 +00:00
|
|
|
|
|
|
|
return $feed->render();
|
|
|
|
}
|
|
|
|
|
2020-04-04 00:22:37 +00:00
|
|
|
/**
|
|
|
|
* @param Torrent[] $torrents
|
|
|
|
*
|
|
|
|
* @return Feed
|
|
|
|
*/
|
|
|
|
private function createFeedFromTorrents(array $torrents): Feed
|
2020-04-03 23:59:47 +00:00
|
|
|
{
|
|
|
|
$feed = new Feed();
|
|
|
|
$channel = $this->createChannel();
|
|
|
|
$feed->addChannel($channel);
|
|
|
|
|
|
|
|
foreach ($this->createItemsFromTorrents($torrents) as $item) {
|
|
|
|
$channel->addItem($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO feed pagination
|
2020-04-04 00:37:08 +00:00
|
|
|
// @see https://tools.ietf.org/html/rfc5005#section-3
|
2020-04-03 23:59:47 +00:00
|
|
|
|
|
|
|
return $feed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createChannel(): Channel
|
|
|
|
{
|
|
|
|
$time = time();
|
|
|
|
|
|
|
|
$channel = new Channel();
|
|
|
|
$channel
|
|
|
|
->title('Last')
|
|
|
|
->description('Last torrents')
|
|
|
|
->url($this->generateUrl('index'))
|
|
|
|
->feedUrl($this->generateUrl('api_v1_rss_last'))
|
|
|
|
->language('en-US')
|
|
|
|
->pubDate($time)
|
|
|
|
->lastBuildDate($time)
|
|
|
|
->ttl(15)
|
|
|
|
;
|
|
|
|
|
|
|
|
return $channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 00:22:37 +00:00
|
|
|
* @param Torrent[] $torrents
|
2020-04-03 23:59:47 +00:00
|
|
|
*
|
|
|
|
* @return Item[]
|
|
|
|
*/
|
2020-04-04 00:22:37 +00:00
|
|
|
private function createItemsFromTorrents(array $torrents): array
|
2020-04-03 23:59:47 +00:00
|
|
|
{
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($torrents as $torrent) {
|
|
|
|
$items[] = $this->createItemFromTorrent($torrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createItemFromTorrent(Torrent $torrent): Item
|
|
|
|
{
|
|
|
|
$item = new Item();
|
|
|
|
$item
|
|
|
|
->title($torrent->getName())
|
|
|
|
->description($torrent->getInfoHash())
|
2020-04-04 00:37:08 +00:00
|
|
|
->pubDate($torrent->getDiscoveredOn())
|
2020-04-03 23:59:47 +00:00
|
|
|
->url($this->generateUrl('torrents_show', ['id' => $torrent->getId()]))
|
|
|
|
->enclosure(
|
|
|
|
$this->magnetGenerator->generate($torrent->getInfoHash(), $torrent->getName()),
|
|
|
|
$torrent->getTotalSize(),
|
|
|
|
self::MIME_TYPE
|
|
|
|
)
|
|
|
|
->guid($torrent->getInfoHash())
|
|
|
|
;
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2020-04-04 00:22:37 +00:00
|
|
|
private function createLastTorrentsQueryBuilder(int $limit = self::PER_PAGE, int $offset = 0): QueryBuilder
|
2020-04-03 23:59:47 +00:00
|
|
|
{
|
|
|
|
$qb = $this->repo->createQueryBuilder('t');
|
|
|
|
$qb
|
|
|
|
->select('t')
|
2020-04-04 00:22:37 +00:00
|
|
|
->orderBy('t.id', 'DESC')
|
|
|
|
->setMaxResults($limit)
|
|
|
|
->setFirstResult($offset)
|
|
|
|
;
|
2020-04-03 23:59:47 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateUrl(string $route, array $parameters = []): string
|
|
|
|
{
|
|
|
|
return $this->router->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
|
|
|
|
}
|
|
|
|
}
|