Veranstaltungen-APP/app/Services/EventImportService.php

96 lines
2.6 KiB
PHP

<?php
namespace App\Services;
use App\Jobs\ImportEventsJob;
use App\Models\Source;
use Illuminate\Support\Facades\Http;
/**
* EventImportService
*
* Service-Klasse für Event-Import-Logik.
* Orchestriert den Import von verschiedenen Quellen.
*/
class EventImportService
{
/**
* Importiere Events von allen aktiven Quellen
*/
public function importFromAllSources($synchronous = false)
{
$sources = Source::active()->get();
foreach ($sources as $source) {
if ($synchronous) {
ImportEventsJob::dispatchSync($source);
} else {
ImportEventsJob::dispatch($source);
}
}
return count($sources);
}
/**
* Importiere von einer spezifischen Quelle
*/
public function importFromSource(Source $source, $synchronous = false)
{
if ($synchronous) {
ImportEventsJob::dispatchSync($source);
} else {
ImportEventsJob::dispatch($source);
}
}
/**
* Beispiel: API-Client für Stadt Dresden
*
* Hinweis: Dies müsste in der fetchExternalEvents-Methode
* des ImportEventsJob verwendet werden.
*/
public function fetchFromDresdenCityAPI($limit = 1000)
{
$response = Http::withHeaders([
'Accept' => 'application/json',
])->get('https://api.stadt-dresden.de/events', [
'limit' => $limit,
'offset' => 0,
]);
if ($response->failed()) {
throw new \Exception("Dresden API request failed: " . $response->status());
}
return $response->json('data');
}
/**
* Beispiel: Web-Scraping mit Symfony/DomCrawler
*
* Installiere: composer require symfony/dom-crawler symfony/http-client
*/
public function scrapeFromWebsite($url)
{
$client = new \Symfony\Component\HttpClient\HttpClient();
$response = $client->request('GET', $url);
$html = $response->getContent();
// Verwende DomCrawler zum Parsen
$crawler = new \Symfony\Component\DomCrawler\Crawler($html);
$events = [];
$crawler->filter('.event-item')->each(function ($node) use (&$events) {
$events[] = [
'title' => $node->filter('.event-title')->text(),
'description' => $node->filter('.event-description')->text(),
'location' => $node->filter('.event-location')->text(),
'date' => $node->filter('.event-date')->attr('data-date'),
];
});
return $events;
}
}