陈德本 5 tahun lalu
induk
melakukan
0457245e1a
1 mengubah file dengan 32 tambahan dan 28 penghapusan
  1. 32 28
      Baidu.php

+ 32 - 28
Baidu.php

@@ -13,6 +13,7 @@ use QL\QueryList;
 
 class Baidu implements PluginContract
 {
+    private $is_pc = true;
     protected $ql;
     protected $keyword;
     protected $pageNumber = 10;
@@ -22,24 +23,34 @@ class Baidu implements PluginContract
             'Accept-Encoding' => 'gzip, deflate, br',
         ]
     ];
-    const API = 'https://www.baidu.com/s';
+    const API = [
+        'PC' => 'https://www.baidu.com/s',
+        'M' => 'https://www.baidu.com/s',
+    ];
     const RULES = [
-      'title' => ['h3','text'],
-      'link' => ['h3>a','href']
+        'title' => ['h3', 'text'],
+        'link' => ['h3>a', 'href']
     ];
     const RANGE = '.result';
+    /**
+     * @var string
+     */
+    private $api;
 
-    public function __construct(QueryList $ql, $pageNumber)
+    public function __construct(QueryList $ql, $pageNumber, $api)
     {
         $this->ql = $ql->rules(self::RULES)->range(self::RANGE);
         $this->pageNumber = $pageNumber;
+        $this->api = self::API[$api];
+        $this->is_pc = $api == "PC";
+
     }
 
     public static function install(QueryList $queryList, ...$opt)
     {
         $name = $opt[0] ?? 'baidu';
-        $queryList->bind($name,function ($pageNumber = 10){
-            return new Baidu($this,$pageNumber);
+        $queryList->bind($name, function ($pageNumber = 10, $api = 'PC') {
+            return new Baidu($this, $pageNumber, $api);
         });
     }
 
@@ -55,9 +66,9 @@ class Baidu implements PluginContract
         return $this;
     }
 
-    public function page($page = 1,$realURL = false)
+    public function page($page = 1, $realURL = false)
     {
-        return $this->query($page)->query()->getData(function ($item) use($realURL){
+        return $this->query($page)->query()->getData(function ($item) use ($realURL) {
             $realURL && $item['link'] = $this->getRealURL($item['link']);
             return $item;
         });
@@ -66,10 +77,9 @@ class Baidu implements PluginContract
     public function getCount()
     {
         $count = 0;
-        $text =  $this->query(1)->find('.nums')->text();
-        if(preg_match('/[\d,]+/',$text,$arr))
-        {
-            $count = str_replace(',','',$arr[0]);
+        $text = $this->query(1)->find('.nums')->text();
+        if (preg_match('/[\d,]+/', $text, $arr)) {
+            $count = str_replace(',', '', $arr[0]);
         }
         return (int)$count;
     }
@@ -83,11 +93,11 @@ class Baidu implements PluginContract
 
     protected function query($page = 1)
     {
-        $this->ql->get(self::API,[
+        $this->ql->get($this->api, [
             'wd' => $this->keyword,
             'rn' => $this->pageNumber,
-            'pn' => $this->pageNumber * ($page-1)
-        ],$this->httpOpt);
+            'pn' => $this->pageNumber * ($page - 1)
+        ], $this->httpOpt);
         return $this->ql;
     }
 
@@ -96,24 +106,18 @@ class Baidu implements PluginContract
      * @param $url
      * @return mixed
      */
-    protected  function getRealURL($url)
+    protected function getRealURL($url)
     {
-        if(empty($url)) return $url;
-        $header = get_headers($url,1);
-        if (strpos($header[0],'301') || strpos($header[0],'302'))
-        {
-            if(is_array($header['Location']))
-            {
+        if (empty($url)) return $url;
+        $header = get_headers($url, 1);
+        if (strpos($header[0], '301') || strpos($header[0], '302')) {
+            if (is_array($header['Location'])) {
                 //return $header['Location'][count($header['Location'])-1];
                 return $header['Location'][0];
-            }
-            else
-            {
+            } else {
                 return $header['Location'];
             }
-        }
-        else
-        {
+        } else {
             return $url;
         }
     }