Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/app/Console/Commands/SyncWorldHeritageVideoUrls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;

class SyncWorldHeritageVideoUrls extends Command
{
protected $signature = 'world-heritage:sync-video-urls
{--limit=100 : Records per API request}
{--dry-run : Show counts without updating DB}';

protected $description = 'Fetch main_video_url from data.unesco.org API and update world_heritage_sites';

private const API_URL = 'https://data.unesco.org/api/explore/v2.1/catalog/datasets/whc001/records';

public function handle(): int
{
$limit = max(1, (int) $this->option('limit'));
$dryRun = (bool) $this->option('dry-run');

$first = $this->fetch(1, 0);
if ($first === null) {
return self::FAILURE;
}

$total = (int) ($first['total_count'] ?? 0);
$offset = 0;
$updated = 0;
$skipped = 0;

$this->info("Total UNESCO records: {$total}");

while ($offset < $total) {
$response = $this->fetch($limit, $offset);
if ($response === null) {
return self::FAILURE;
}

$results = $response['results'] ?? [];
if ($results === []) {
break;
}

foreach ($results as $row) {
$idNo = (int) ($row['id_no'] ?? 0);
$videoUrl = $row['main_video_url'] ?? null;

if ($idNo === 0) {
$skipped++;
continue;
}

if (!$dryRun) {
DB::table('world_heritage_sites')
->where('id', $idNo)
->update(['main_video_url' => $videoUrl]);
}

$updated++;
}

$offset += count($results);
$this->line("Processed {$offset}/{$total}...");
}

if ($dryRun) {
$this->info("Dry-run: would update {$updated} records, skipped {$skipped}.");
} else {
$this->info("Done. Updated {$updated} records, skipped {$skipped}.");
}

return self::SUCCESS;
}

private function fetch(int $limit, int $offset): ?array
{
$response = Http::acceptJson()
->retry(3, 500)
->get(self::API_URL, [
'select' => 'id_no,main_video_url',
'limit' => $limit,
'offset' => $offset,
'order_by' => 'id_no asc',
]);

if ($response->failed()) {
$this->error("UNESCO API error: HTTP {$response->status()} offset={$offset}");
return null;
}

return $response->json();
}
}
1 change: 1 addition & 0 deletions src/app/Models/WorldHeritage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class WorldHeritage extends Model
'short_description',
'unesco_site_url',
'main_image_url',
'main_video_url',
];

protected $hidden = [
Expand Down
74 changes: 74 additions & 0 deletions src/app/Packages/Features/Controller/HeritageImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Packages\Features\Controller;

use App\Http\Controllers\Controller;
use App\Models\Image;
use App\Models\WorldHeritage;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Http;

class HeritageImageController extends Controller
{
public function proxyImage(Request $request, int $id): Response|JsonResponse
{
$videoUrl = $this->fetchVideoUrlFromUnesco($id);

if ($videoUrl === null) {
return response()->json(['error' => 'No video URL found for this site'], 404);
}

$thumbnailUrl = $this->buildYoutubeThumbnailUrl($videoUrl);

if ($thumbnailUrl === null) {
return response()->json(['error' => 'Could not extract YouTube video ID'], 422);
}

$upstream = Http::get($thumbnailUrl);

if ($upstream->failed()) {
return response()->json(['error' => 'Failed to fetch thumbnail from YouTube'], 502);
}

return response($upstream->body(), 200)
->header('Content-Type', $upstream->header('Content-Type'));
}

private function fetchVideoUrlFromUnesco(int $idNo): ?string
{
$site = WorldHeritage::find($idNo);

return $site?->main_video_url;
}

private function buildYoutubeThumbnailUrl(string $videoUrl): ?string
{
if (preg_match('/(?:embed\/|v=|youtu\.be\/)([A-Za-z0-9_-]{11})/', $videoUrl, $m)) {
return "https://img.youtube.com/vi/{$m[1]}/hqdefault.jpg";
}

return null;
}

public function proxyImageById(Request $request, int $imageId): Response|JsonResponse
{
$image = Image::find($imageId);

if ($image === null) {
return response()->json(['error' => 'Image not found'], 404);
}

$upstream = Http::withHeaders([
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
])->get($image->url);

if ($upstream->failed()) {
return response()->json(['error' => 'Failed to fetch image from upstream'], 502);
}

return response($upstream->body(), 200)
->header('Content-Type', $upstream->header('Content-Type'));
}
}
Original file line number Diff line number Diff line change
@@ -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::table('world_heritage_sites', function (Blueprint $table) {
$table->string('main_video_url')->nullable()->after('main_image_url');
});
}

public function down(): void
{
Schema::table('world_heritage_sites', function (Blueprint $table) {
$table->dropColumn('main_video_url');
});
}
};
1 change: 1 addition & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
Route::get('heritages/region-count', [WorldHeritageController::class, 'getWorldHeritagesCountByRegion']);
Route::get('/heritages/{id}', [WorldHeritageController::class, 'getWorldHeritageById']);
Route::get('/heritage-image/{id}', [HeritageImageController::class, 'proxyImage']);
Route::get('/heritage-image/image/{imageId}', [HeritageImageController::class, 'proxyImageById']);
});
Loading