Add scheduler, fetch service

This commit is contained in:
myrmidex 2025-06-29 09:37:49 +02:00
parent 9be9e2ec91
commit f141ab7889
7 changed files with 180 additions and 5 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace App\Console\Commands;
use App\Services\Articles\ArticleFetcher;
use Illuminate\Console\Command;
class FetchNewArticlesCommand extends Command
{
protected $signature = 'articles:fetch';
protected $description = 'Fetches new articles';
public function handle(): int
{
logger('Fetch new articles command');
ArticleFetcher::getNewArticles();
return self::SUCCESS;
}
}

27
app/Models/Article.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @method static firstOrCreate(string[] $array)
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
'url',
];
public function casts(): array
{
return [
'created_at' => 'datetime',
];
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace App\Services\Articles;
use App\Models\Article;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Http;
class ArticleFetcher
{
public static function getNewArticles(): Collection
{
return self::fetchArticles()
->map(fn (string $url) => self::saveArticle($url));
}
private static function fetchArticles(): Collection
{
try {
$response = Http::get('https://www.vrt.be/vrtnws/en/');
$html = $response->body();
// Extract article links using regex
preg_match_all('/href="(\/vrtnws\/en\/\d{4}\/\d{2}\/\d{2}\/[^"]+)"/', $html, $matches);
$urls = collect($matches[1] ?? [])
->unique()
->take(10) // Limit to 10 articles
->map(fn($path) => 'https://www.vrt.be' . $path)
->toArray();
$responses = Http::pool(function ($pool) use ($urls) {
foreach ($urls as $url) {
$pool->get($url);
}
});
return collect($responses)
->map(function ($response, $index) use ($urls) {
$url = $urls[$index];
try {
if ($response->successful()) {
return $url;
} else {
return null;
}
} catch (Exception) {
return null;
}
})
->filter(fn($article) => !empty($article));
} catch (Exception $e) {
logger('article_fetcher')->error("Failed to fetch VRT homepage", ['error' => $e->getMessage()]);
return new Collection([]);
}
}
protected static function saveArticle(string $url): Article
{
return Article::firstOrCreate(['url' => $url]);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
*/
class ArticleFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

View file

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->timestamp('created_at')->default(now());
});
}
public function down(): void
{
Schema::dropIfExists('articles');
}
};

View file

@ -23,6 +23,25 @@ services:
- sail
depends_on:
- mysql
scheduler:
build:
context: './vendor/laravel/sail/runtimes/8.4'
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: 'sail-8.4/app'
extra_hosts:
- 'host.docker.internal:host-gateway'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
command: php artisan schedule:work
mysql:
image: 'mysql/mysql-server:8.0'
ports:

View file

@ -1,8 +1,6 @@
<?php
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use App\Console\Commands\FetchNewArticlesCommand;
use Illuminate\Support\Facades\Schedule;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
Schedule::command(FetchNewArticlesCommand::class)->hourly();