Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use brave search for the logos if google fails #169

Merged
merged 2 commits into from
Feb 29, 2024
Merged
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
34 changes: 28 additions & 6 deletions endpoints/logos/search.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
if (isset($_GET['search'])) {
$searchTerm = urlencode($_GET['search'] . " logo");

$url = "https://www.google.com/search?q={$searchTerm}&tbm=isch&tbs=iar:xw,ift:png";
$backupUrl = "https://search.brave.com/search?q={$searchTerm}";

// Use cURL to fetch the search results page
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -24,10 +25,29 @@
$response = curl_exec($ch);

if ($response === false) {
echo json_encode(['error' => 'Failed to fetch data from Google.']);
// 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) {
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 {
// Parse the HTML response to extract image URLs
$imageUrls = extractImageUrlsFromGoogle($response);
$imageUrls = extractImageUrlsFromPage($response);

// Pass the image URLs to the client
header('Content-Type: application/json');
Expand All @@ -39,7 +59,7 @@
echo json_encode(['error' => 'Invalid request.']);
}

function extractImageUrlsFromGoogle($html) {
function extractImageUrlsFromPage($html) {
$imageUrls = [];

$doc = new DOMDocument();
Expand All @@ -48,8 +68,10 @@ function extractImageUrlsFromGoogle($html) {
$imgTags = $doc->getElementsByTagName('img');
foreach ($imgTags as $imgTag) {
$src = $imgTag->getAttribute('src');
if (filter_var($src, FILTER_VALIDATE_URL)) {
$imageUrls[] = $src;
if (!strstr($imgTag->getAttribute('class'), "favicon") && !strstr($imgTag->getAttribute('class'), "logo")) {
if (filter_var($src, FILTER_VALIDATE_URL)) {
$imageUrls[] = $src;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v1.9.1";
$version = "v1.10.0";
?>
3 changes: 3 additions & 0 deletions scripts/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ function displayImageResults(imageSources) {
img.onclick = function() {
selectWebLogo(src);
};
img.onerror = function() {
this.parentNode.removeChild(this);
};
logoResults.appendChild(img);
});
}
Expand Down