Advertisement
loderunner84

auru

Dec 21st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.24 KB | None | 0 0
  1. <?php
  2. require_once "simple_html_dom.php";
  3. class db {
  4.     public $db;
  5.     public $err = false;
  6.     private $db_name = "auru";
  7.     private $db_host = "localhost";
  8.     private $db_user = "auru";
  9.     private $db_pass = "auru";
  10.  
  11.     function __construct(){
  12.         $opt = array(
  13.             PDO::ATTR_ERRMODE            => PDO::ERRMODE_WARNING,
  14.             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  15.             PDO::ATTR_TIMEOUT => "3"
  16.         );
  17.         try{
  18.             $dsn = "mysql:host=$this->db_host;dbname=$this->db_name;charset=utf8";
  19.             $this->db = new PDO($dsn, $this->db_user, $this->db_pass, $opt);
  20.         }
  21.         catch (PDOException $e){
  22.             $this->err = "database error: ".$e->getMessage();
  23.         }
  24.     }
  25.  
  26.     public function fillParcer($a){
  27.         $sql = "insert into parser (jobid, lotid, llink, lname, mark, price, blitz, ldate, pers, prat, img) values (:jobid, :lotid, :llink, :lname, :mark, :price, :blitz, :ldate, :pers, :prat, :img)";
  28.         $stmt = $this->db->prepare($sql);
  29.         foreach ($a as $row){
  30.             $b = [
  31.                 'jobid' => $row['jobid'],
  32.                 'lotid' => $row['lotid'],
  33.                 'llink' => $row['llink'],
  34.                 'lname' => $row['lname'],
  35.                 'mark' => $row['mark'],
  36.                 'price' => $row['price'],
  37.                 'blitz' => $row['blitz'],
  38.                 'ldate' => $row['ldate'],
  39.                 'pers' => $row['pers'],
  40.                 'prat' => $row['prat'],
  41.                 'img' => $row['img']
  42.             ];
  43.             try{
  44.                 $stmt->execute($b);
  45.             }catch (PDOException $e){
  46.                 $this->err = "database error: ".$e->getMessage();
  47.                 return false;
  48.             }
  49.         }
  50.         return true;
  51.     }
  52.  
  53.     public function getLastlot($job){
  54.         $sql = "select MAX(lotid) from parser where jobid=?";
  55.         $stmt = $this->db->prepare($sql);
  56.         $stmt->execute([$job]);
  57.         return $stmt->fetchColumn(0);
  58.     }
  59.  
  60.     public function getJobs($clientid = false){
  61.         if($clientid){
  62.             $sql = "select * from jobs where clientid=?";
  63.             $stmt = $this->db->prepare($sql);
  64.             $stmt->execute([$clientid]);
  65.             return $stmt->fetchAll();
  66.         } else {
  67.             return $this->db->query("select * from jobs");
  68.         }
  69.     }
  70.  
  71.     function fillJobs($a){
  72.         $sql = "insert into jobs (clientid, jlink, maxlots) values (:clientid, :jlink, :maxlots)";
  73.         $stmt = $this->db->prepare($sql);
  74.         try{
  75.             $stmt->execute($a);
  76.             return $this->db->lastInsertId();
  77.         } catch (PDOException $e){
  78.             $this->err = $e->getMessage();
  79.             return false;
  80.         }
  81.     }
  82.  
  83.     function removeJob($jobid){
  84.         $stmt = $this->db->prepare("delete from parser where jobid=?");
  85.         if($stmt->execute([$jobid])){
  86.             $stmt = $this->db->prepare("delete from jobs where id=?");
  87.             $stmt->execute([$jobid]);
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.  
  93.     function removeAll($clientid){
  94.         $stmt = $this->db->prepare("select id from jobs where clientid=?");
  95.         $stmt->execute([$clientid]);
  96.         $ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
  97.         $this->db->query("delete from parser where jobid in (".implode(", ", $ids).")");
  98.         $this->db->query("delete from jobs where clientid=".$clientid);
  99.         return true;
  100.     }
  101. }
  102.  
  103. class Auru extends db {
  104.     public $data;
  105. //    public $link;
  106.     public $html;
  107.  
  108.     function load($link){
  109.         /*$link = $this->validUrl($link);
  110.         if(!$link){
  111.             return false;
  112.         }*/
  113.         $this->html = file_get_html($link);
  114.         if(!$this->html){
  115.             return false;
  116.         }
  117.         if($this->html->find('h1.au-errpage__title')->innertext == "Категория не найдена"){
  118.             return false;
  119.         }
  120.         return true;
  121.     }
  122.  
  123.     function getPrice($p){
  124.         if($p !== NULL){
  125.             $p = $p->find('span.au-price__value', 0)->innertext;
  126.             return (int)preg_replace("/\D/","",$p);
  127.         }
  128.         return "";
  129.     }
  130.  
  131.     public function parseLink($j, $i){
  132.         $b = [];
  133.         $c = array_slice($this->html->find('div.au-lots-item'), 0, $i);
  134.         foreach ($c as $l){
  135.             $a['lotid'] = preg_replace("/\D/","",$l->getAttribute('data-lamber-object'));
  136.             $a['jobid'] = $j;
  137.             $a['lname'] = $l->find('a.au-item-name', 0)->innertext;
  138.             $a['llink'] = "https:".$l->find('a.au-item-name', 0)->href;
  139.             $a['mark'] = $l->find('div.au-geo-mark__name', 0)->innertext;
  140.             $a['price'] = $this->getPrice($l->find('span.au-price_current', 0));
  141.             $a['blitz'] = $this->getPrice($l->find('div.au-price_blitz', 0));
  142.             $a['ldate'] = $l->find('span.au-date-marker__item', 0)->innertext;
  143.             $a['pers'] = strip_tags($l->find('span.au-person-name__text', 0)->innertext);
  144.             $a['prat'] = (int)$l->find('a.au-person-rating', 0)->innertext;
  145.             $a['img'] = $l->find('img.au-lot-card-pic__img', 0)->src;
  146.             if($a['img'] !== NULL){
  147.                 $a['img'] = "https:".$a['img'];
  148.             } else {
  149.                 $a['img'] = "";
  150.             }
  151.             $b[] = $a;
  152.         }
  153.         return $b;
  154.     }
  155.  
  156.     function addJob($a){
  157.         $a['jlink'] = $this->validUrl($a['jlink']);
  158.         if($a['jlink'] && $a['maxlots']){
  159.             $jobid = $this->fillJobs($a);
  160.             if($jobid){
  161.                 if($this->load($a['jlink'])){
  162.                     $lots = $this->parseLink($jobid, $a['maxlots']);
  163.                     if(count($lots)) $this->fillParcer($lots);
  164.                     //return array with lots
  165.                     return $lots;
  166.                 }
  167.             }
  168.         }
  169.         return false;
  170.     }
  171.  
  172.     function validUrl($url){
  173.         $url = parse_url($url);
  174.         $host = explode(".", $url['host']);
  175.        
  176.         if(array_pop($host) != "ru" || array_pop($host) != "au"){
  177.             $this->err = "url not valid";
  178.             return false;
  179.         }
  180.         $query = explode("&", $url['query']);
  181.         foreach ($query as $k => $str){
  182.             $str = explode("=", $str);
  183.             if($str[0] == "sort") unset($query[$k]);
  184.             if($str[0] == "dir") unset($query[$k]);
  185.             if($str[0] == "lot") unset($query[$k]);
  186.         }
  187.         $query[] = "sort=id";
  188.         $query[] = "dir=desc";
  189.         $query[] = "type=lot";
  190.         $url['query'] = implode("&", $query);
  191.         return $url['scheme']."://".$url['host'].$url['path']."?".$url['query'];
  192.     }
  193.  
  194.     function runJobs(){
  195. //        $links = [];
  196.         $lots = [];
  197.         $jobs = $this->getJobs();
  198.         foreach ($jobs as $job){
  199.             $i = $this->getLastlot($job['id']);
  200.             $this->load($job['jlink']);
  201.             $a = $this->parseLink($job['id'], $job['maxlots']);
  202.             foreach ($a as $b){
  203.                 if($b['lotid'] > $i){
  204.                     $b['jobid'] = $job['id'];
  205.                     $b['clientid'] = $job['clientid'];
  206.                     $lots[] = $b;
  207.                 }
  208.             }
  209.         }
  210.         if($this->fillParcer($lots)) return $lots;
  211.         return false;
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement