Actualiser api.php
This commit is contained in:
@@ -64,6 +64,61 @@ function getTmdbApiKey($pdo) {
|
|||||||
return $row ? decryptData($row['key_value']) : null;
|
return $row ? decryptData($row['key_value']) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getConfigValue($pdo, $keyName) {
|
||||||
|
try {
|
||||||
|
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = ?");
|
||||||
|
$stmt->execute([$keyName]);
|
||||||
|
$row = $stmt->fetch();
|
||||||
|
return $row ? decryptData($row['key_value']) : null;
|
||||||
|
} catch (\Exception $e) { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchPosterByEAN($ean, $pdo) {
|
||||||
|
if (empty($ean) || strlen($ean) < 8) return null;
|
||||||
|
|
||||||
|
// Vérification du cache
|
||||||
|
$cacheKey = 'ean_poster_' . md5($ean);
|
||||||
|
$cached = getCache($pdo, $cacheKey);
|
||||||
|
if ($cached && !empty($cached['poster'])) return $cached['poster'];
|
||||||
|
|
||||||
|
// 1. UPCitemDB (Pas de clé nécessaire, version trial)
|
||||||
|
$upcData = fetchUPCitemdb($ean, $pdo);
|
||||||
|
if ($upcData && !empty($upcData['poster'])) {
|
||||||
|
setCache($pdo, $cacheKey, ['poster' => $upcData['poster']], 'upc');
|
||||||
|
return $upcData['poster'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. EAN-Search API
|
||||||
|
$eanSearchKey = getConfigValue($pdo, 'ean_search_key');
|
||||||
|
if ($eanSearchKey) {
|
||||||
|
$url = "https://api.ean-search.org/api?token={$eanSearchKey}&op=barcode-lookup&ean={$ean}&format=json";
|
||||||
|
$res = httpGet($url, 5);
|
||||||
|
if ($res) {
|
||||||
|
$data = json_decode($res, true);
|
||||||
|
if (!empty($data[0]['imageUrl'])) {
|
||||||
|
setCache($pdo, $cacheKey, ['poster' => $data[0]['imageUrl']], 'ean_search');
|
||||||
|
return $data[0]['imageUrl'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Barcode Lookup API
|
||||||
|
$blKey = getConfigValue($pdo, 'barcode_lookup_key');
|
||||||
|
if ($blKey) {
|
||||||
|
$url = "https://api.barcodelookup.com/v3/products?barcode={$ean}&key={$blKey}&formatted=y";
|
||||||
|
$res = httpGet($url, 5);
|
||||||
|
if ($res) {
|
||||||
|
$data = json_decode($res, true);
|
||||||
|
if (!empty($data['products'][0]['images'][0])) {
|
||||||
|
setCache($pdo, $cacheKey, ['poster' => $data['products'][0]['images'][0]], 'barcode_lookup');
|
||||||
|
return $data['products'][0]['images'][0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// ─ HTTP avec cURL (timeout court pour vitesse) ──
|
// ─ HTTP avec cURL (timeout court pour vitesse) ──
|
||||||
function httpGet($url, $timeout = 5) {
|
function httpGet($url, $timeout = 5) {
|
||||||
if (!function_exists('curl_init')) {
|
if (!function_exists('curl_init')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user