Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1.     private function getItems($query, $region, $pmin, $pmax, $category = '', $in_title_only = 0, $page = 0)
  2.     {
  3.         $client = new Client();
  4.         $client->addSubscriber($this->cookiePlugin);
  5.         if (strlen($category) > 0) {
  6.             $categoryString = '/' . $category;
  7.         } else {
  8.             $categoryString = '';
  9.         }
  10.         $queryString = 'https://m.avito.ru/'
  11.             . $region
  12.             . $categoryString
  13.             . '?bt='
  14.             . $in_title_only
  15.             . '&q=' . $query;
  16.         if ($page !== 0) {
  17.             $queryString = $queryString.'&p='.$page;
  18.         }
  19.         if (strlen($pmin) > 0) {
  20.             $queryString .= '&pmin=' . $pmin;
  21.         };
  22.  
  23.         if (strlen($pmax) > 0) {
  24.             $queryString .= '&pmax=' . $pmax;
  25.         };
  26.  
  27.         $this->logger->notice($queryString);
  28.         $request = $client->get($queryString);
  29.  
  30.         try {
  31.             $response = $request->send();
  32.         } catch (ClientErrorResponseException $e) {
  33.             $e->getResponse()->getStatusCode();
  34.             $this->logger->notice('Got err code ' . $e->getCode());
  35.  
  36.             $e->getCode();
  37.             return array();
  38.         }
  39.  
  40.         $code = $response->getStatusCode();
  41.         if ($code !== 200) {
  42.             return array();
  43.         }
  44.         $body = (string)$response->getBody();
  45.         $crawler = new Crawler($body);
  46.  
  47.         $totalItems = null;
  48.         if (count($crawler->filter('.nav-helper-text')) > 0 ){
  49.             $totalItems = $crawler->filter('.nav-helper-text')->text();
  50.             $this->logger->notice("total items is {$totalItems} ");
  51.         }
  52.         $crawler = $crawler->filter('article.b-item:not(.item-vip):not(.item-highlight)');
  53.  
  54.         $items = array();
  55.         foreach ($crawler as $item) {
  56.             $item = new Crawler($item);
  57.  
  58.             $title = $item->filter('span.header-text')->text();
  59.             ###not every item have image and price
  60.  
  61.             $image = $item->filter('span.pseudo-img');
  62.             $image = (count($image) > 0) ? $image->attr('style') : '';
  63.             $image = str_replace(array('background-image: url(', ')', ';', '//'), '', $image);
  64.             $price = $item->filter('div.item-price');
  65.             $price = (count($price) > 0) ? $price->text() : '';
  66.             $price = preg_replace('#[^0-9]#', '', $price);
  67.             $avitoDatetime = $item->filter('div.info-date');
  68.             $avitoDatetime = (count($avitoDatetime) > 0) ? $avitoDatetime->text() : '';
  69.             $avitoUnixTime = $this->avitoTimeToUnixtime($avitoDatetime);
  70.             $avitoId = $item->attr('data-item-id');
  71.  
  72.             $newItem = array(
  73.                 'title' => trim($title),
  74.                 'price' => trim($price),
  75.                 'image' => trim($image),
  76.                 'avitoId' => $avitoId,
  77.                 'avitoUnixTime' => $avitoUnixTime,
  78.             );
  79.  
  80.             $this->logger->notice(print_r($newItem, true));
  81.             $items[] = $newItem;
  82.         }
  83.         $this->logger->notice('got ' . count($items) . ' items from parser');
  84.  
  85.         if ($page === 0){
  86.             $items = array_merge($items, $this->getItems($query, $region, $pmin, $pmax, $category, $in_title_only, 2));
  87.         }
  88.  
  89.         return $items;
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement