first commit
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WikipediaService
|
||||
{
|
||||
private string $baseUrl;
|
||||
private int $cacheTtl;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->baseUrl = config('services.wikipedia.url', 'https://en.wikipedia.org/api/rest_v1');
|
||||
$this->cacheTtl = config('services.wikipedia.cache_ttl', 30) * 24 * 60; // Convert days to minutes
|
||||
}
|
||||
|
||||
/**
|
||||
* Get city summary from Wikipedia
|
||||
*
|
||||
* @param string $cityName
|
||||
* @return array|null
|
||||
*/
|
||||
public function getCitySummary(string $cityName): ?array
|
||||
{
|
||||
$cacheKey = "wikipedia:summary:{$cityName}";
|
||||
|
||||
return Cache::remember($cacheKey, $this->cacheTtl, function () use ($cityName) {
|
||||
try {
|
||||
$response = Http::withHeaders([
|
||||
'User-Agent' => 'WikiTravelApp/1.0 (contact@example.com)'
|
||||
])
|
||||
->timeout(10)
|
||||
->get("{$this->baseUrl}/page/summary/" . urlencode($cityName));
|
||||
|
||||
if ($response->successful()) {
|
||||
$data = $response->json();
|
||||
|
||||
Log::info("Wiki message ". json_encode($data));
|
||||
|
||||
return [
|
||||
'title' => $data['title'] ?? null,
|
||||
'country' => $data['description'] ?? null,
|
||||
'summary' => $data['extract'] ?? null,
|
||||
'url' => $data['content_urls']['desktop']['page'] ?? null,
|
||||
'page_id' => $data['pageid'] ?? null,
|
||||
'thumbnail' => $data['thumbnail']['source'] ?? null,
|
||||
'image' => $data['originalimage']['source'] ?? null,
|
||||
'latitude' => $data['coordinates']['lat'] ?? null,
|
||||
'longitude' => $data['coordinates']['lon'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
Log::warning("Wikipedia API request failed for city: {$cityName}", [
|
||||
'status' => $response->status(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Wikipedia API error for city: {$cityName}", [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for cities on Wikipedia
|
||||
*
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
* @return array
|
||||
*/
|
||||
public function searchCities(string $query, int $limit = 10): array
|
||||
{
|
||||
try {
|
||||
$response = Http::withHeaders([
|
||||
'User-Agent' => 'WikiTravelApp/1.0 (contact@example.com)'
|
||||
])
|
||||
->timeout(10)
|
||||
->get("{$this->baseUrl}/../w/api.php", [
|
||||
'action' => 'opensearch',
|
||||
'search' => $query,
|
||||
'limit' => $limit,
|
||||
'namespace' => 0,
|
||||
'format' => 'json',
|
||||
]);
|
||||
|
||||
if ($response->successful()) {
|
||||
$data = $response->json();
|
||||
|
||||
$results = [];
|
||||
$titles = $data[1] ?? [];
|
||||
$descriptions = $data[2] ?? [];
|
||||
$urls = $data[3] ?? [];
|
||||
|
||||
foreach ($titles as $index => $title) {
|
||||
$results[] = [
|
||||
'title' => $title,
|
||||
'description' => $descriptions[$index] ?? '',
|
||||
'url' => $urls[$index] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
return [];
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Wikipedia search error for query: {$query}", [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache for a specific city
|
||||
*
|
||||
* @param string $cityName
|
||||
* @return void
|
||||
*/
|
||||
public function clearCache(string $cityName): void
|
||||
{
|
||||
Cache::forget("wikipedia:summary:{$cityName}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user