Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once "simple_html_dom.php";
- class db {
- public $db;
- public $err = false;
- private $db_name = "auru";
- private $db_host = "localhost";
- private $db_user = "auru";
- private $db_pass = "auru";
- function __construct(){
- $opt = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_TIMEOUT => "3"
- );
- try{
- $dsn = "mysql:host=$this->db_host;dbname=$this->db_name;charset=utf8";
- $this->db = new PDO($dsn, $this->db_user, $this->db_pass, $opt);
- }
- catch (PDOException $e){
- $this->err = "database error: ".$e->getMessage();
- }
- }
- public function fillParcer($a){
- $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)";
- $stmt = $this->db->prepare($sql);
- foreach ($a as $row){
- $b = [
- 'jobid' => $row['jobid'],
- 'lotid' => $row['lotid'],
- 'llink' => $row['llink'],
- 'lname' => $row['lname'],
- 'mark' => $row['mark'],
- 'price' => $row['price'],
- 'blitz' => $row['blitz'],
- 'ldate' => $row['ldate'],
- 'pers' => $row['pers'],
- 'prat' => $row['prat'],
- 'img' => $row['img']
- ];
- try{
- $stmt->execute($b);
- }catch (PDOException $e){
- $this->err = "database error: ".$e->getMessage();
- return false;
- }
- }
- return true;
- }
- public function getLastlot($job){
- $sql = "select MAX(lotid) from parser where jobid=?";
- $stmt = $this->db->prepare($sql);
- $stmt->execute([$job]);
- return $stmt->fetchColumn(0);
- }
- public function getJobs($clientid = false){
- if($clientid){
- $sql = "select * from jobs where clientid=?";
- $stmt = $this->db->prepare($sql);
- $stmt->execute([$clientid]);
- return $stmt->fetchAll();
- } else {
- return $this->db->query("select * from jobs");
- }
- }
- function fillJobs($a){
- $sql = "insert into jobs (clientid, jlink, maxlots) values (:clientid, :jlink, :maxlots)";
- $stmt = $this->db->prepare($sql);
- try{
- $stmt->execute($a);
- return $this->db->lastInsertId();
- } catch (PDOException $e){
- $this->err = $e->getMessage();
- return false;
- }
- }
- function removeJob($jobid){
- $stmt = $this->db->prepare("delete from parser where jobid=?");
- if($stmt->execute([$jobid])){
- $stmt = $this->db->prepare("delete from jobs where id=?");
- $stmt->execute([$jobid]);
- return true;
- }
- return false;
- }
- function removeAll($clientid){
- $stmt = $this->db->prepare("select id from jobs where clientid=?");
- $stmt->execute([$clientid]);
- $ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
- $this->db->query("delete from parser where jobid in (".implode(", ", $ids).")");
- $this->db->query("delete from jobs where clientid=".$clientid);
- return true;
- }
- }
- class Auru extends db {
- public $data;
- // public $link;
- public $html;
- function load($link){
- /*$link = $this->validUrl($link);
- if(!$link){
- return false;
- }*/
- $this->html = file_get_html($link);
- if(!$this->html){
- return false;
- }
- if($this->html->find('h1.au-errpage__title')->innertext == "Категория не найдена"){
- return false;
- }
- return true;
- }
- function getPrice($p){
- if($p !== NULL){
- $p = $p->find('span.au-price__value', 0)->innertext;
- return (int)preg_replace("/\D/","",$p);
- }
- return "";
- }
- public function parseLink($j, $i){
- $b = [];
- $c = array_slice($this->html->find('div.au-lots-item'), 0, $i);
- foreach ($c as $l){
- $a['lotid'] = preg_replace("/\D/","",$l->getAttribute('data-lamber-object'));
- $a['jobid'] = $j;
- $a['lname'] = $l->find('a.au-item-name', 0)->innertext;
- $a['llink'] = "https:".$l->find('a.au-item-name', 0)->href;
- $a['mark'] = $l->find('div.au-geo-mark__name', 0)->innertext;
- $a['price'] = $this->getPrice($l->find('span.au-price_current', 0));
- $a['blitz'] = $this->getPrice($l->find('div.au-price_blitz', 0));
- $a['ldate'] = $l->find('span.au-date-marker__item', 0)->innertext;
- $a['pers'] = strip_tags($l->find('span.au-person-name__text', 0)->innertext);
- $a['prat'] = (int)$l->find('a.au-person-rating', 0)->innertext;
- $a['img'] = $l->find('img.au-lot-card-pic__img', 0)->src;
- if($a['img'] !== NULL){
- $a['img'] = "https:".$a['img'];
- } else {
- $a['img'] = "";
- }
- $b[] = $a;
- }
- return $b;
- }
- function addJob($a){
- $a['jlink'] = $this->validUrl($a['jlink']);
- if($a['jlink'] && $a['maxlots']){
- $jobid = $this->fillJobs($a);
- if($jobid){
- if($this->load($a['jlink'])){
- $lots = $this->parseLink($jobid, $a['maxlots']);
- if(count($lots)) $this->fillParcer($lots);
- //return array with lots
- return $lots;
- }
- }
- }
- return false;
- }
- function validUrl($url){
- $url = parse_url($url);
- $host = explode(".", $url['host']);
- if(array_pop($host) != "ru" || array_pop($host) != "au"){
- $this->err = "url not valid";
- return false;
- }
- $query = explode("&", $url['query']);
- foreach ($query as $k => $str){
- $str = explode("=", $str);
- if($str[0] == "sort") unset($query[$k]);
- if($str[0] == "dir") unset($query[$k]);
- if($str[0] == "lot") unset($query[$k]);
- }
- $query[] = "sort=id";
- $query[] = "dir=desc";
- $query[] = "type=lot";
- $url['query'] = implode("&", $query);
- return $url['scheme']."://".$url['host'].$url['path']."?".$url['query'];
- }
- function runJobs(){
- // $links = [];
- $lots = [];
- $jobs = $this->getJobs();
- foreach ($jobs as $job){
- $i = $this->getLastlot($job['id']);
- $this->load($job['jlink']);
- $a = $this->parseLink($job['id'], $job['maxlots']);
- foreach ($a as $b){
- if($b['lotid'] > $i){
- $b['jobid'] = $job['id'];
- $b['clientid'] = $job['clientid'];
- $lots[] = $b;
- }
- }
- }
- if($this->fillParcer($lots)) return $lots;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement