LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

duckduckgo.php (2505B)

      1 <?php
      2     class DuckDuckGoRequest extends EngineRequest {
      3         public function get_request_url() {
      4             $query_encoded = str_replace("%22", "\"", urlencode($this->query));
      5             $results = array();
      6 
      7             $domain = 'com';
      8             $results_language = $this->opts->language;
      9             $number_of_results = $this->opts->number_of_results;
     10 
     11             $url = "https://html.duckduckgo.$domain/html/?q=$query_encoded&kd=-1&s=" . 3 * $this->page;
     12 
     13             if (3 > strlen($results_language) && 0 < strlen($results_language))
     14                 $url .= "&lr=lang_$results_language";
     15 
     16             if (3 > strlen($number_of_results) && 0 < strlen($number_of_results))
     17                 $url .= "&num=$number_of_results";
     18 
     19             if (isset($_COOKIE["safe_search"]))
     20                 $url .= "&safe=medium";
     21 
     22             return $url;
     23         }
     24 
     25         public function parse_results($response) {
     26             $results = array();
     27             $xpath = get_xpath($response);
     28 
     29             if (!$xpath)
     30                 return $results;
     31             
     32             foreach($xpath->query("/html/body/div[1]/div[". count($xpath->query('/html/body/div[1]/div')) ."]/div/div/div[contains(@class, 'web-result')]/div") as $result) {
     33                 $url = $xpath->evaluate(".//h2[@class='result__title']//a/@href", $result)[0];
     34                 
     35                 if ($url == null)
     36                     continue;
     37 
     38                 if (!empty($results)) { // filter duplicate results
     39                     if (end($results)["url"] == $url->textContent)
     40                         continue;
     41                 }
     42 
     43                 $url = $url->textContent;
     44 
     45                 $title = $xpath->evaluate(".//h2[@class='result__title']", $result)[0];
     46                 $description = $xpath->evaluate(".//a[@class='result__snippet']", $result)[0];
     47 
     48                 array_push($results,
     49                     array (
     50                         "title" => htmlspecialchars($title->textContent),
     51                         "url" =>  htmlspecialchars($url),
     52                         // base_url is to be removed in the future, see #47
     53                         "base_url" => htmlspecialchars(get_base_url($url)),
     54                         "description" =>  $description == null ?
     55                                           TEXTS["result_no_description"] :
     56                                           htmlspecialchars($description->textContent)
     57                     )
     58                 );
     59            }
     60            return $results;
     61         }
     62 
     63     }
     64 ?>