feat: use brave search for the logos if google fails (#169)
This commit is contained in:
parent
284cf62fcf
commit
fff783e4e8
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
if (isset($_GET['search'])) {
|
if (isset($_GET['search'])) {
|
||||||
$searchTerm = urlencode($_GET['search'] . " logo");
|
$searchTerm = urlencode($_GET['search'] . " logo");
|
||||||
$url = "https://www.google.com/search?q={$searchTerm}&tbm=isch&tbs=iar:xw,ift:png";
|
|
||||||
|
|
||||||
// Use cURL to fetch the search results page
|
$url = "https://www.google.com/search?q={$searchTerm}&tbm=isch&tbs=iar:xw,ift:png";
|
||||||
|
$backupUrl = "https://search.brave.com/search?q={$searchTerm}";
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
@ -23,11 +24,30 @@
|
|||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
// If cURL fails to access google images, use brave image search as a backup
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $backupUrl);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
$envVars = array_change_key_case($_SERVER, CASE_LOWER);
|
||||||
|
$httpProxy = isset($envVars['http_proxy']) ? $envVars['http_proxy'] : null;
|
||||||
|
$httpsProxy = isset($envVars['https_proxy']) ? $envVars['https_proxy'] : null;
|
||||||
|
if (!empty($httpProxy)) {
|
||||||
|
curl_setopt($ch, CURLOPT_PROXY, $httpProxy);
|
||||||
|
} elseif (!empty($httpsProxy)) {
|
||||||
|
curl_setopt($ch, CURLOPT_PROXY, $httpsProxy);
|
||||||
|
}
|
||||||
|
$response = curl_exec($ch);
|
||||||
if ($response === false) {
|
if ($response === false) {
|
||||||
echo json_encode(['error' => 'Failed to fetch data from Google.']);
|
echo json_encode(['error' => 'Failed to fetch data from Google.']);
|
||||||
|
} else {
|
||||||
|
$imageUrls = extractImageUrlsFromPage($response);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['imageUrls' => $imageUrls]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Parse the HTML response to extract image URLs
|
// Parse the HTML response to extract image URLs
|
||||||
$imageUrls = extractImageUrlsFromGoogle($response);
|
$imageUrls = extractImageUrlsFromPage($response);
|
||||||
|
|
||||||
// Pass the image URLs to the client
|
// Pass the image URLs to the client
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
@ -39,7 +59,7 @@
|
|||||||
echo json_encode(['error' => 'Invalid request.']);
|
echo json_encode(['error' => 'Invalid request.']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractImageUrlsFromGoogle($html) {
|
function extractImageUrlsFromPage($html) {
|
||||||
$imageUrls = [];
|
$imageUrls = [];
|
||||||
|
|
||||||
$doc = new DOMDocument();
|
$doc = new DOMDocument();
|
||||||
@ -48,10 +68,12 @@
|
|||||||
$imgTags = $doc->getElementsByTagName('img');
|
$imgTags = $doc->getElementsByTagName('img');
|
||||||
foreach ($imgTags as $imgTag) {
|
foreach ($imgTags as $imgTag) {
|
||||||
$src = $imgTag->getAttribute('src');
|
$src = $imgTag->getAttribute('src');
|
||||||
|
if (!strstr($imgTag->getAttribute('class'), "favicon") && !strstr($imgTag->getAttribute('class'), "logo")) {
|
||||||
if (filter_var($src, FILTER_VALIDATE_URL)) {
|
if (filter_var($src, FILTER_VALIDATE_URL)) {
|
||||||
$imageUrls[] = $src;
|
$imageUrls[] = $src;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $imageUrls;
|
return $imageUrls;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
<?php
|
<?php
|
||||||
$version = "v1.9.1";
|
$version = "v1.10.0";
|
||||||
?>
|
?>
|
||||||
@ -213,6 +213,9 @@ function displayImageResults(imageSources) {
|
|||||||
img.onclick = function() {
|
img.onclick = function() {
|
||||||
selectWebLogo(src);
|
selectWebLogo(src);
|
||||||
};
|
};
|
||||||
|
img.onerror = function() {
|
||||||
|
this.parentNode.removeChild(this);
|
||||||
|
};
|
||||||
logoResults.appendChild(img);
|
logoResults.appendChild(img);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user