64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Article;
|
|
|
|
use App\Models\Article;
|
|
use App\Services\Http\HttpFetcher;
|
|
use App\Services\Factories\ArticleParserFactory;
|
|
use App\Services\Factories\HomepageParserFactory;
|
|
use Exception;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ArticleFetcher
|
|
{
|
|
public static function getNewArticles(): Collection
|
|
{
|
|
try {
|
|
$allArticles = collect();
|
|
|
|
foreach (HomepageParserFactory::getAllParsers() as $parser) {
|
|
$html = HttpFetcher::fetchHtml($parser->getHomepageUrl());
|
|
$urls = $parser->extractArticleUrls($html);
|
|
|
|
$articles = collect($urls)
|
|
->map(fn (string $url) => self::saveArticle($url));
|
|
|
|
$allArticles = $allArticles->merge($articles);
|
|
}
|
|
|
|
return $allArticles->filter();
|
|
} catch (Exception $e) {
|
|
logger()->error("Failed to get new articles", ['error' => $e->getMessage()]);
|
|
|
|
return new Collection([]);
|
|
}
|
|
}
|
|
|
|
public static function fetchArticleData(Article $article): array
|
|
{
|
|
try {
|
|
$html = HttpFetcher::fetchHtml($article->url);
|
|
$parser = ArticleParserFactory::getParser($article->url);
|
|
|
|
return $parser->extractData($html);
|
|
} catch (Exception $e) {
|
|
logger()->error('Exception while fetching article data', [
|
|
'url' => $article->url,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
|
|
private static function saveArticle(string $url): Article
|
|
{
|
|
$existingArticle = Article::where('url', $url)->first();
|
|
|
|
if ($existingArticle) {
|
|
return $existingArticle;
|
|
}
|
|
|
|
return Article::create(['url' => $url]);
|
|
}
|
|
}
|