Skip to content

Commit

Permalink
fix: qwant search now uses a JSON API, instead of lite.qwant.com (mer…
Browse files Browse the repository at this point in the history
…ge pull request #100 from michelematteini/mmatteini-fix-qwantapi0

fix: qwant search now uses a JSON API, instead of lite.qwant.com
  • Loading branch information
Ahwxorg authored Dec 20, 2023
2 parents 18dda2c + ecf1725 commit 64acad1
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions engines/qwant/image.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
<?php
class QwantImageSearch extends EngineRequest {
public function get_request_url() {
$page = $this->page / 10 + 1; // qwant has a different page system
$offset = $page * 5; // load 50 images per page
$query = urlencode($this->query);

return "https://lite.qwant.com/?q=$query&t=images&p=$page";
return "https://api.qwant.com/v3/search/images?q=$query&t=images&count=50&locale=en_us&offset=$offset&device=desktop&tgp=3&safesearch=1";
}

public function parse_results($response) {
$json = json_decode($response, true);
$results = array();
$xpath = get_xpath($response);

if (!$xpath)
return $results;

foreach($xpath->query("//a[@rel='noopener']") as $result)
{
$image = $xpath->evaluate(".//img", $result)[0];

if ($image)
{
$encoded_url = $result->getAttribute("href");
$encoded_url_split1 = explode("==/", $encoded_url)[1];
$encoded_url_split2 = explode("?position", $encoded_url_split1)[0];
$real_url = urldecode(base64_decode($encoded_url_split2));

$alt = $image->getAttribute("alt");
$thumbnail = urlencode($image->getAttribute("src"));
if ($json["status"] != "success")
return $results; // no results

array_push($results,
array (
"thumbnail" => urldecode(htmlspecialchars($thumbnail)),
"alt" => htmlspecialchars($alt),
"url" => htmlspecialchars($real_url)
)
);
$imgs = $json["data"]["result"]["items"];
$imgCount = $json["data"]["result"]["total"];

}
for ($i = 0; $i < $imgCount; $i++)
{
array_push($results,
array (
"thumbnail" => htmlspecialchars($imgs[$i]["thumbnail"]),
"alt" => htmlspecialchars($imgs[$i]["title"]),
"url" => htmlspecialchars($imgs[$i]["url"])
)
);
}

return $results;
Expand Down

0 comments on commit 64acad1

Please sign in to comment.