implement torrent scrape queue

This commit is contained in:
ghost 2023-10-09 05:09:18 +03:00
parent d2f7fff24e
commit 1b722812e8
7 changed files with 194 additions and 24 deletions

View file

@ -72,8 +72,8 @@ class TorrentController extends AbstractController
'peers' => (int) $torrent->getPeers(),
'leechers' => (int) $torrent->getLeechers(),
],
'locales' => $torrentService->findLastTorrentLocales($torrent->getId()),
'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(),
'locales' => $torrentService->findLastTorrentLocalesByTorrentId($torrent->getId()),
'sensitive' => $torrentService->findLastTorrentSensitiveByTorrentId($torrent->getId())->isValue(),
'download' =>
[
'file' =>
@ -346,7 +346,7 @@ class TorrentController extends AbstractController
// Otherwise, get latest available
else
{
if ($torrentLocales = $torrentService->findLastTorrentLocales($torrent->getId()))
if ($torrentLocales = $torrentService->findLastTorrentLocalesByTorrentId($torrent->getId()))
{
$torrentLocalesCurrent['userId'] = $torrentLocales->getUserId();
@ -367,7 +367,7 @@ class TorrentController extends AbstractController
// Init edition history
$editions = [];
foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocalesEdition)
foreach ($torrentService->findTorrentLocalesByTorrentId($torrent->getId()) as $torrentLocalesEdition)
{
$editions[] =
[
@ -652,7 +652,7 @@ class TorrentController extends AbstractController
}
else
{
if ($torrentSensitive = $torrentService->findLastTorrentSensitive($request->get('torrentId')))
if ($torrentSensitive = $torrentService->findLastTorrentSensitiveByTorrentId($request->get('torrentId')))
{
$torrentSensitiveCurrent =
[
@ -675,7 +675,7 @@ class TorrentController extends AbstractController
// Init edition history
$editions = [];
foreach ($torrentService->findTorrentSensitive($torrent->getId()) as $torrentSensitiveEdition)
foreach ($torrentService->findTorrentSensitiveByTorrentId($torrent->getId()) as $torrentSensitiveEdition)
{
$editions[] =
[
@ -1088,4 +1088,26 @@ class TorrentController extends AbstractController
$file->getMagnetLink()
);
}
// Tools
#[Route(
'/crontab/scrape',
methods:
[
'GET'
]
)]
public function scrape(
Request $request,
TranslatorInterface $translator,
TorrentService $torrentService,
): Response
{
$torrentService->scrapeTorrentQueue(
explode('|', $this->getParameter('app.trackers'))
);
// Render response
return new Response(); // @TODO
}
}

View file

@ -20,6 +20,9 @@ class Torrent
#[ORM\Column]
private ?int $added = null;
#[ORM\Column(nullable: true)]
private ?int $scraped = null;
#[ORM\Column]
private ?bool $approved = null;
@ -71,6 +74,18 @@ class Torrent
return $this;
}
public function getScraped(): ?int
{
return $this->scraped;
}
public function setScraped(int $scraped): static
{
$this->scraped = $scraped;
return $this;
}
public function getKeywords(): ?string
{
return $this->keywords;

View file

@ -30,4 +30,14 @@ class TorrentRepository extends ServiceEntityRepository
->getOneOrNullResult()
;
}
public function getTorrentScrapeQueue(): ?Torrent
{
return $this->createQueryBuilder('t')
->orderBy('t.scraped', 'ASC') // same to ts.added
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}

View file

@ -36,6 +36,80 @@ class TorrentService
}
// Tools
public function scrapeTorrentQueue(
array $trackers = []
): void
{
// Init Scraper
$scraper = new \Yggverse\Scrapeer\Scraper();
if ($torrent = $this->getTorrentScrapeQueue())
{
// Get file
if (!$file = $this->readTorrentFileByTorrentId($torrent->getId()))
{
// @TODO
throw new \Exception(
$translator->trans('File not found')
);
}
// Get info hashes
$hashes = [];
if ($hash = $file->getInfoHashV1(false))
{
$hashes[] = $hash;
}
if ($hash = $file->getInfoHashV2(false))
{
$hashes[] = $hash;
}
// Get scrape
if ($hashes && $trackers)
{
// Update scrape info
if ($results = $scraper->scrape($hashes, $trackers, null, 1))
{
foreach ($results as $result)
{
if (isset($result['seeders']))
{
$torrent->setSeeders(
(int) $result['seeders']
);
}
if (isset($result['completed']))
{
$torrent->setPeers(
(int) $result['completed']
);
}
if (isset($result['leechers']))
{
$torrent->setLeechers(
(int) $result['leechers']
);
}
}
}
}
// Update time scraped
$torrent->setScraped(
time()
);
// Save results to DB
$this->entityManagerInterface->persist($torrent);
$this->entityManagerInterface->flush();
}
}
public function readTorrentFileByFilepath(
string $filepath
): ?\Rhilip\Bencode\TorrentFile
@ -188,6 +262,13 @@ class TorrentService
return $torrent;
}
public function getTorrentScrapeQueue(): ?Torrent
{
return $this->entityManagerInterface
->getRepository(Torrent::class)
->getTorrentScrapeQueue();
}
// Torrent locale
public function getTorrentLocales(int $id): ?TorrentLocales
{