Skip to content

Commit

Permalink
Add refreshing to http api app-build
Browse files Browse the repository at this point in the history
  • Loading branch information
pkly committed Jun 9, 2024
1 parent d110551 commit 1a12cac
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 54 deletions.
61 changes: 7 additions & 54 deletions src/Command/RssSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@

namespace App\Command;

use App\Entity\Rss\Result;
use App\Repository\Rss\ResultRepository;
use App\Repository\Rss\SearchRepository;
use App\Traits\EntityManagerTrait;
use App\Service\RssRefreshService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsCommand(
name: 'app:cron-search',
description: 'Run possible cron searches'
)]
class RssSearchCommand extends Command
{
use EntityManagerTrait;

public function __construct(
private readonly HttpClientInterface $client,
private readonly SearchRepository $searchRepository,
private readonly ResultRepository $resultRepository,
private readonly RssRefreshService $refreshService,
string $name = null
) {
parent::__construct($name);
Expand All @@ -35,51 +27,8 @@ protected function execute(
OutputInterface $output
): int {
$io = new SymfonyStyle($input, $output);
$count = 0;

foreach ($this->searchRepository->findBy(['active' => true]) as $search) {
$response = $this->client->request(
'GET',
sprintf($search->getRssGroup()->getUrl(), urlencode($search->getQuery()))
);

if (200 !== $response->getStatusCode()) {
$io->error(
sprintf(
'An issue occurred while trying to search for %s in %s (id: %s)',
$search->getQuery(),
$search->getRssGroup()->getName(),
$search->getId()
)
);

continue;
}

$xml = json_decode(json_encode(simplexml_load_string($response->getContent())), true);
$items = array_reverse($xml['channel']['item'] ?? []);

if (isset($items['link'])) {
$items = [$items];
}

foreach ($items as $item) {
if (null !== $this->resultRepository->findOneBy(['guid' => $item['guid']])) {
continue;
}

$found = (new Result())
->setTitle($item['title'] ?? 'Unknown title?')
->setGuid($item['guid'])
->setUrl($item['link'])
->setData($item)
->setSearch($search);

$this->em->persist($found);
$this->em->flush();
$count++;
}
}
[$errors, $count] = $this->refreshService->refresh();

if ($count > 0) {
$io->success(
Expand All @@ -92,6 +41,10 @@ protected function execute(
$io->warning('No new results found');
}

foreach ($errors as $error) {
$io->error($error);
}

return Command::SUCCESS;
}
}
10 changes: 10 additions & 0 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Repository\Rss\ResultRepository;
use App\Service\MascotService;
use App\Service\QBitTorrentService;
use App\Service\RssRefreshService;
use App\Service\SplashTitleService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -19,6 +20,15 @@
#[Route('/api', name: 'api.')]
class ApiController extends AbstractController
{
#[Route('/refresh-rss', name: 'refresh_rss', methods: ['GET'])]
public function refreshRss(
RssRefreshService $refreshService
): Response {
$refreshService->refresh();

return new Response();
}

#[Route('/mascot-groups', name: 'mascot_groups', methods: ['GET'])]
public function getMascotGroups(
MascotService $service
Expand Down
77 changes: 77 additions & 0 deletions src/Service/RssRefreshService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Service;

use App\Entity\Rss\Result;
use App\Repository\Rss\ResultRepository;
use App\Repository\Rss\SearchRepository;
use App\Traits\EntityManagerTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class RssRefreshService
{
use EntityManagerTrait;

public function __construct(
private readonly HttpClientInterface $client,
private readonly SearchRepository $searchRepository,
private readonly ResultRepository $resultRepository,
) {
}

/**
* @return array{0: list<string>, 1: int}
*/
public function refresh(): array
{
$errors = [];
$count = 0;

foreach ($this->searchRepository->findBy(['active' => true]) as $search) {
$response = $this->client->request(
'GET',
sprintf($search->getRssGroup()->getUrl(), urlencode($search->getQuery()))
);

if (200 !== $response->getStatusCode()) {
$errors[] = sprintf(
'An issue occurred while trying to search for %s in %s (id: %s)',
$search->getQuery(),
$search->getRssGroup()->getName(),
$search->getId()
);

continue;
}

$xml = json_decode(json_encode(simplexml_load_string($response->getContent())), true);
$items = array_reverse($xml['channel']['item'] ?? []);

if (isset($items['link'])) {
$items = [$items];
}

foreach ($items as $item) {
if (null !== $this->resultRepository->findOneBy(['guid' => $item['guid']])) {
continue;
}

$found = (new Result())
->setTitle($item['title'] ?? 'Unknown title?')
->setGuid($item['guid'])
->setUrl($item['link'])
->setData($item)
->setSearch($search);

$this->em->persist($found);
$this->em->flush();
$count++;
}
}

return [
$errors,
$count,
];
}
}

0 comments on commit 1a12cac

Please sign in to comment.