mirror of
				https://git.bakhai.co.in/FbIN/LibreY.git
				synced 2025-11-04 12:01:31 +05:30 
			
		
		
		
	Synced
(Bing seems to be returning garbage results if they don't see certain parameters. This adds random bytes every request to the `rdrig` parameter. (a45ccad548) Refactor Bing backend, fix paging, add did-you-mean/language/safe-search. Refactored the backend for Bing to (slightly) increase faster parsing. Also fixed the paging bugs (some copy-pasta broke it and went unnoticed on last commit). Added 'did you mean' support as well as language and safe-search (defaults to Bing default, 'Moderate'). (15e153ddd7) Added `rdr=1` parameter to indicate redirected by JavaS**t. (c42f135261)
This commit is contained in:
		
					parent
					
						
							
								4f26290346
							
						
					
				
			
			
				commit
				
					
						845b229312
					
				
			
		
					 1 changed files with 29 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -6,12 +6,25 @@
 | 
			
		|||
            $results_language = $this->opts->language;
 | 
			
		||||
            $number_of_results = $this->opts->number_of_results;
 | 
			
		||||
 | 
			
		||||
            // TODO figure out how to not autocorrect
 | 
			
		||||
            $url = "https://www.bing.com/search?q=$query_encoded&first=" . ((10 * $this->page) + 1);
 | 
			
		||||
            // NOTE Page(0,1)=1, Page(2)=9, Page(3+)=23..37..51..
 | 
			
		||||
            if ($this->page <= 1)
 | 
			
		||||
                $page = 1;
 | 
			
		||||
            elseif($this->page == 2)
 | 
			
		||||
                $page = 9;
 | 
			
		||||
            else
 | 
			
		||||
                $page = 9 + (($this->page - 2) * 14);
 | 
			
		||||
            $url = "https://www.bing.com/search?q=$query_encoded&first=$page&rdr=1";
 | 
			
		||||
 | 
			
		||||
            $randomBytes = strtoupper(bin2hex(random_bytes(16)));
 | 
			
		||||
            $url = "https://www.bing.com/search?q=$query_encoded&first=$page&rdr=1&rdrig=$randomBytes";
 | 
			
		||||
 | 
			
		||||
            // TODO language setting
 | 
			
		||||
            if (!is_null($results_language))
 | 
			
		||||
                $url .= "&lang=$results_language";
 | 
			
		||||
                $url .= "&srchlang=$results_language";
 | 
			
		||||
 | 
			
		||||
            // TODO Reconsider current safe-search implementation for granularity
 | 
			
		||||
            // NOTE Possible values are strict, demote (moderate, default), off
 | 
			
		||||
            if (isset($_COOKIE["safe_search"]))
 | 
			
		||||
                $url .= "&adlt=demote";
 | 
			
		||||
 | 
			
		||||
            return $url;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -23,8 +36,8 @@
 | 
			
		|||
            if (!$xpath)
 | 
			
		||||
                return $results;
 | 
			
		||||
 | 
			
		||||
            foreach($xpath->query("//ol[@id='b_results']//li") as $result) {
 | 
			
		||||
                $href_url = $xpath->evaluate(".//h2//a//@href", $result)[0];
 | 
			
		||||
            foreach($xpath->query("//ol[@id='b_results']/li") as $result) {
 | 
			
		||||
                $href_url = $xpath->evaluate(".//h2/a/@href", $result)[0];
 | 
			
		||||
 | 
			
		||||
                if ($href_url == null)
 | 
			
		||||
                    continue;
 | 
			
		||||
| 
						 | 
				
			
			@ -63,14 +76,14 @@
 | 
			
		|||
                if (!empty($results) && array_key_exists("url", $results) && end($results)["url"] == $url->textContent)
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                $title = $xpath->evaluate(".//h2//a", $result)[0];
 | 
			
		||||
                $title = $xpath->evaluate("./h2/a", $result)[0];
 | 
			
		||||
 | 
			
		||||
                if ($title == null)
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                $title = $title->textContent;
 | 
			
		||||
 | 
			
		||||
                $description = ($xpath->evaluate(".//div[contains(@class, 'b_caption')]//p", $result)[0] ?? null) ?->textContent ?? '';
 | 
			
		||||
                $description = ($xpath->evaluate("./div[contains(@class, 'b_caption')]/p", $result)[0] ?? null) ?->textContent ?? '';
 | 
			
		||||
 | 
			
		||||
                array_push($results,
 | 
			
		||||
                    array (
 | 
			
		||||
| 
						 | 
				
			
			@ -83,10 +96,16 @@
 | 
			
		|||
                                          htmlspecialchars($description)
 | 
			
		||||
                    )
 | 
			
		||||
                );
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $didyoumean = $xpath->evaluate("//ol[@id='b_results']/li/div[contains(@class, 'sp_requery')]/a/strong")[0] ?? null;
 | 
			
		||||
 | 
			
		||||
            if (!is_null($didyoumean))
 | 
			
		||||
                array_push($results, array(
 | 
			
		||||
                    "did_you_mean" => $didyoumean->textContent
 | 
			
		||||
                ));
 | 
			
		||||
 | 
			
		||||
           return $results;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
?>
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue