Skip to content

Commit 3d54dbf

Browse files
authored
chore: merge pull request #215 from wrldspawn/google-fix-march-2025
Fix Google results failing after March 2025
2 parents 2aeb0e0 + b531406 commit 3d54dbf

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

engines/text/google.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
<?php
22
class GoogleRequest extends EngineRequest {
3+
protected string $arc_id;
4+
protected int $arc_timestamp = 0;
5+
6+
private function generate_arc_id() {
7+
$charset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
8+
$this->arc_id = "srp_";
9+
10+
for ($i = 0; $i < 24; $i++) {
11+
$c = random_int(0, strlen($charset) - 1);
12+
$this->arc_id .= $charset[$c];
13+
}
14+
15+
$this->arc_id .= "_1";
16+
$this->arc_timestamp = time();
17+
}
18+
319
public function get_request_url() {
20+
if ($this->arc_timestamp + 3600 < time())
21+
$this->generate_arc_id();
422

523
$query_encoded = str_replace("%22", "\"", urlencode($this->query));
624
$results = array();
725

826
$domain = $this->opts->google_domain;
927
$results_language = $this->opts->language;
1028
$number_of_results = $this->opts->number_of_results;
29+
$arc_page = sprintf("%02d", $this->page * 10);
1130

1231
$url = "https://www.google.$domain/search?q=$query_encoded&nfpr=1&start=$this->page";
1332

@@ -22,6 +41,8 @@ public function get_request_url() {
2241
if (isset($_COOKIE["safe_search"]))
2342
$url .= "&safe=medium";
2443

44+
$url .= "&asearch=arc&async=arc_id:$this->arc_id$arc_page,use_ac:true,_fmt:html";
45+
2546
return $url;
2647
}
2748

@@ -33,21 +54,21 @@ public function parse_results($response) {
3354
if (!$xpath)
3455
return $results;
3556

36-
$didyoumean = $xpath->query(".//a[@class='gL9Hy']")[0];
57+
$didyoumean = $xpath->query(".//p[@class='QRYxYe NNMgCf']/a/b/i")[0];
3758

3859
if (!is_null($didyoumean))
3960
array_push($results, array(
4061
"did_you_mean" => $didyoumean->textContent
4162
));
4263

43-
foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result) {
44-
$url = $xpath->evaluate(".//div[@class='yuRUbf']//a/@href", $result)[0];
64+
foreach($xpath->query("//div[@class='MjjYud']") as $result) {
65+
$url = $xpath->evaluate(".//a[@class='zReHs']/@href", $result)[0];
4566

4667
if ($url == null)
4768
continue;
4869

4970
if (!empty($results) && array_key_exists("url", end($results)) && end($results)["url"] == $url->textContent)
50-
continue;
71+
continue;
5172

5273
$url = $url->textContent;
5374

0 commit comments

Comments
 (0)