Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function getItems($query, $region, $pmin, $pmax, $category = '', $in_title_only = 0, $page = 0)
- {
- $client = new Client();
- $client->addSubscriber($this->cookiePlugin);
- if (strlen($category) > 0) {
- $categoryString = '/' . $category;
- } else {
- $categoryString = '';
- }
- $queryString = 'https://m.avito.ru/'
- . $region
- . $categoryString
- . '?bt='
- . $in_title_only
- . '&q=' . $query;
- if ($page !== 0) {
- $queryString = $queryString.'&p='.$page;
- }
- if (strlen($pmin) > 0) {
- $queryString .= '&pmin=' . $pmin;
- };
- if (strlen($pmax) > 0) {
- $queryString .= '&pmax=' . $pmax;
- };
- $this->logger->notice($queryString);
- $request = $client->get($queryString);
- try {
- $response = $request->send();
- } catch (ClientErrorResponseException $e) {
- $e->getResponse()->getStatusCode();
- $this->logger->notice('Got err code ' . $e->getCode());
- $e->getCode();
- return array();
- }
- $code = $response->getStatusCode();
- if ($code !== 200) {
- return array();
- }
- $body = (string)$response->getBody();
- $crawler = new Crawler($body);
- $totalItems = null;
- if (count($crawler->filter('.nav-helper-text')) > 0 ){
- $totalItems = $crawler->filter('.nav-helper-text')->text();
- $this->logger->notice("total items is {$totalItems} ");
- }
- $crawler = $crawler->filter('article.b-item:not(.item-vip):not(.item-highlight)');
- $items = array();
- foreach ($crawler as $item) {
- $item = new Crawler($item);
- $title = $item->filter('span.header-text')->text();
- ###not every item have image and price
- $image = $item->filter('span.pseudo-img');
- $image = (count($image) > 0) ? $image->attr('style') : '';
- $image = str_replace(array('background-image: url(', ')', ';', '//'), '', $image);
- $price = $item->filter('div.item-price');
- $price = (count($price) > 0) ? $price->text() : '';
- $price = preg_replace('#[^0-9]#', '', $price);
- $avitoDatetime = $item->filter('div.info-date');
- $avitoDatetime = (count($avitoDatetime) > 0) ? $avitoDatetime->text() : '';
- $avitoUnixTime = $this->avitoTimeToUnixtime($avitoDatetime);
- $avitoId = $item->attr('data-item-id');
- $newItem = array(
- 'title' => trim($title),
- 'price' => trim($price),
- 'image' => trim($image),
- 'avitoId' => $avitoId,
- 'avitoUnixTime' => $avitoUnixTime,
- );
- $this->logger->notice(print_r($newItem, true));
- $items[] = $newItem;
- }
- $this->logger->notice('got ' . count($items) . ' items from parser');
- if ($page === 0){
- $items = array_merge($items, $this->getItems($query, $region, $pmin, $pmax, $category, $in_title_only, 2));
- }
- return $items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement