Skip to content

Commit 524c287

Browse files
authored
chore: merge pull request #225 from OdinVex/main
Refactor Bing backend, fix paging, add did-you-mean/language/safe-search
2 parents 3d54dbf + a45ccad commit 524c287

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

engines/text/bing.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ public function get_request_url() {
66
$results_language = $this->opts->language;
77
$number_of_results = $this->opts->number_of_results;
88

9-
// TODO figure out how to not autocorrect
10-
$url = "https://www.bing.com/search?q=$query_encoded&first=" . ((10 * $this->page) + 1);
9+
// NOTE Page(0,1)=1, Page(2)=9, Page(3+)=23..37..51..
10+
if ($this->page <= 1)
11+
$page = 1;
12+
elseif($this->page == 2)
13+
$page = 9;
14+
else
15+
$page = 9 + (($this->page - 2) * 14);
16+
$url = "https://www.bing.com/search?q=$query_encoded&first=$page&rdr=1";
17+
18+
$randomBytes = strtoupper(bin2hex(random_bytes(16)));
19+
$url = "https://www.bing.com/search?q=$query_encoded&first=$page&rdr=1&rdrig=$randomBytes";
1120

12-
// TODO language setting
1321
if (!is_null($results_language))
14-
$url .= "&lang=$results_language";
22+
$url .= "&srchlang=$results_language";
23+
24+
// TODO Reconsider current safe-search implementation for granularity
25+
// NOTE Possible values are strict, demote (moderate, default), off
26+
if (isset($_COOKIE["safe_search"]))
27+
$url .= "&adlt=demote";
1528

1629
return $url;
1730
}
@@ -23,8 +36,8 @@ public function parse_results($response) {
2336
if (!$xpath)
2437
return $results;
2538

26-
foreach($xpath->query("//ol[@id='b_results']//li") as $result) {
27-
$href_url = $xpath->evaluate(".//h2//a//@href", $result)[0];
39+
foreach($xpath->query("//ol[@id='b_results']/li") as $result) {
40+
$href_url = $xpath->evaluate(".//h2/a/@href", $result)[0];
2841

2942
if ($href_url == null)
3043
continue;
@@ -63,14 +76,14 @@ public function parse_results($response) {
6376
if (!empty($results) && array_key_exists("url", $results) && end($results)["url"] == $url->textContent)
6477
continue;
6578

66-
$title = $xpath->evaluate(".//h2//a", $result)[0];
79+
$title = $xpath->evaluate("./h2/a", $result)[0];
6780

6881
if ($title == null)
6982
continue;
7083

7184
$title = $title->textContent;
7285

73-
$description = ($xpath->evaluate(".//div[contains(@class, 'b_caption')]//p", $result)[0] ?? null) ?->textContent ?? '';
86+
$description = ($xpath->evaluate("./div[contains(@class, 'b_caption')]/p", $result)[0] ?? null) ?->textContent ?? '';
7487

7588
array_push($results,
7689
array (
@@ -83,10 +96,16 @@ public function parse_results($response) {
8396
htmlspecialchars($description)
8497
)
8598
);
86-
8799
}
100+
101+
$didyoumean = $xpath->evaluate("//ol[@id='b_results']/li/div[contains(@class, 'sp_requery')]/a/strong")[0] ?? null;
102+
103+
if (!is_null($didyoumean))
104+
array_push($results, array(
105+
"did_you_mean" => $didyoumean->textContent
106+
));
107+
88108
return $results;
89109
}
90-
91110
}
92111
?>

0 commit comments

Comments
 (0)