Nexo78

Untitled

Nov 2nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Copyright (c) 2018.
  4.  * Author NightTrolley
  5.  */
  6.  
  7. require ('../parser/phpQuery-onefile.php');
  8.  
  9. $db_connect = mysqli_connect("localhost", "root", "", "osclass");
  10.  
  11. if(!empty($_POST['url'])){
  12.  
  13. }
  14. else{
  15.     echo "Введите url";
  16.     exit();
  17. }
  18. if(!empty($_POST['page_count'])) {
  19.  
  20.     $i = 1;
  21.     $j = $_POST['page_count'];
  22.     for ($i; $i<=$j; $i++){
  23.         $links[] = $_POST['url'] . '&page=' . $i;
  24.     }
  25.  
  26. }
  27. else{
  28.     echo "Введите кол-во страниц";
  29.     exit();
  30. }
  31.  
  32. $replace = array(
  33.     'Детский мир » ',
  34.     '» Одежда для девочек',
  35.     '» Одежда для мальчиков',
  36.     'Детская одежда »',
  37.     'Одежда для новорожденных',
  38.     'Квартиры, комнаты » ',
  39.     'Дома » ',
  40.     'Посуточная аренда жилья » ',
  41.     'Земля » ',
  42.     '» Audi',
  43.     '» Volkswagen',
  44.     '» Opel',
  45.     'Транспорт » ',
  46.     '» Saab',
  47.     '» Skoda',
  48.     '» Renault',
  49.     '» Ford',
  50.     '» Nissan',
  51.     '» Peugeot',
  52.     '» Hyundai',
  53.     '» Honda',
  54.     '» BMW',
  55.     '» Chevrolet',
  56.     '» Mazda',
  57.     '» Kia',
  58.     '» ВАЗ',
  59.     'и аксессуары » Аксессуары для телефонов',
  60.     'и аксессуары » Мобильные телефоны / смартфоны',
  61.     '» Прочая техника для дома',
  62.     '» Портативная акустика',
  63.     '» Пылесосы',
  64.     'Электроника » ',
  65.     '» Приставки',
  66.     ' и комплектующие » Настольные компьютеры',
  67.     '» Кофеварки / кофемолки',
  68.     'Животные »',
  69.     '» Медицина',
  70.     '» Volvo',
  71.     '» Chery',
  72.     '» Daewoo',
  73.     '» Fiat',
  74.     '» Renault',
  75.     '» Mercedes-Benz',
  76.     '» Dacia',
  77.     '» Subaru',
  78.     '» Toyota',
  79.     '» Suzuki',
  80.     'Все',
  81.     '/ напитки',
  82.     '» Кухонная мебель',
  83.     '» Мебель для гостиной',
  84.     '» Прочие стройматериалы',
  85.     '» Отделочные и облицовочные материалы',
  86.     '» Электроинструмент',
  87.     'Дом и сад »',
  88.     '» Отопление',
  89.     '» Мебель для спальни',
  90.     '» Кирпич и бетон и пеноблоки',
  91.     '» Бензоинструмент',
  92.     '» Офисная мебель'
  93.  
  94. );
  95.  
  96. class ImagesHelper {
  97.     public const PATH_TO_FILES = '../dpm/oc-content/uploads/28/';
  98.  
  99.     public static function generateImagesPath($feth): ?array
  100.     {
  101.         $feth = self::itemResolver($feth);
  102.  
  103.         $imageThumbNailsPath = [];
  104.         $imagePreviewPath = [];
  105.         foreach(iterator_to_array($feth,true) as $field){
  106.             $thumbNailsPath = sprintf('%s_thumbnail.jpg',$field[0]);
  107.             $previewsPath = sprintf('%s_preview.jpg',$field[0]);
  108.             $imageThumbNailsPath[] = sprintf('%s%s',self::PATH_TO_FILES,$thumbNailsPath);
  109.             $imagePreviewPath[] = sprintf('%s%s',self::PATH_TO_FILES,$previewsPath);
  110.  
  111.  
  112.         }
  113.  
  114.         return [$imageThumbNailsPath,$imagePreviewPath];
  115.     }
  116.     public static function itemResolver($feth): \Generator
  117.     {
  118.         // $stmt = mysqli_query($db_connect, "select pk_i_id from oc_t_item_resource ORDER by pk_i_id desc");
  119.         // $records = mysqli_fetch_all($stmt);
  120.  
  121.         foreach($feth as $record => $value){
  122.             yield $value;
  123.         }
  124.  
  125.     }
  126. }
  127.  
  128. function curl_content($url){
  129.     $ch = curl_init($url);
  130.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  131.     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; WinNT; en; rv:1.0.2) Gecko/20030311 Beonex/0.8.2-stable');
  132.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  133.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  134.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
  135.  
  136.     $html=curl_exec($ch);
  137.     unset($url);
  138.     return $html;
  139.  
  140. }
  141.  
  142. function category_replace($cat, $replace){
  143.  
  144.     $slash_replace = str_replace('/', 'и', $cat);
  145.     $replaces = str_replace($replace, '', $slash_replace);
  146.     return trim($replaces);
  147.  
  148. }
  149.  
  150. /**
  151.  * @param $url
  152.  * @param $id
  153.  * @param $token
  154.  * @return array
  155.  */
  156. function get_phone_number($url)
  157. {
  158.     $cookie_path = $_SERVER['DOCUMENT_ROOT'] . '/cookie.dat';
  159.     preg_match('|-ID(.*).html|', $url, $id);
  160.     $olx = curl_init($url);
  161.     curl_setopt($olx, CURLOPT_SSL_VERIFYPEER, false);
  162.     curl_setopt($olx, CURLOPT_RETURNTRANSFER, true);
  163.     curl_setopt($olx, CURLOPT_HEADER, 1);
  164.     curl_setopt($olx, CURLOPT_COOKIEFILE, $cookie_path);
  165.     curl_setopt($olx, CURLOPT_COOKIEJAR, $cookie_path);
  166.     $result = curl_exec($olx);
  167.     $content = $result;
  168.     curl_close($olx);
  169.     preg_match("|phoneToken = '(.*)';|", $result, $token);
  170.     $olx_number = curl_init('https://www.olx.ua/ajax/misc/contact/phone/' . $id[1] . '/?pt=' . $token[1]);
  171.     curl_setopt($olx_number, CURLOPT_HTTPHEADER, [
  172.         'Host: www.olx.ua',
  173.         'Accept: */*',
  174.         'Accept-Language: uk,ru;q=0.8,en-US;q=0.5,en;q=0.3',
  175.         'Accept-Encoding: gzip, deflate, br',
  176.         'Connection: keep-alive',
  177.         'X-Requested-With: XMLHttpRequest'
  178.     ]);
  179.     curl_setopt($olx_number, CURLOPT_REFERER, $url);
  180.     curl_setopt($olx_number, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  181.     curl_setopt($olx_number, CURLOPT_COOKIEFILE, $cookie_path);
  182.     curl_setopt($olx_number, CURLOPT_SSL_VERIFYPEER, false);
  183.     curl_setopt($olx_number, CURLOPT_RETURNTRANSFER, true);
  184.     $result = curl_exec($olx_number);
  185.     curl_close($olx_number);
  186.     unlink( $_SERVER['DOCUMENT_ROOT'].'/cookie.dat');
  187.     $json = json_decode($result);
  188.     return array($content, $json);
  189. }
  190.  
  191.  
  192. /**
  193.  * @param $db_connect
  194.  * @param $linkimgbd
  195.  */
  196. function get_img($imgs, $thumbs, $preview){
  197.  
  198.     $saveto1 = $thumbs[0];
  199.     $saveto2 = $preview[0];
  200.     $ch = curl_init($imgs);
  201.     curl_setopt($ch, CURLOPT_HEADER, 0);
  202.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  203.     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  204.     $raw = curl_exec($ch);
  205.     curl_close($ch);
  206.  
  207.     $fp = fopen($saveto1, 'w');
  208.     $fp2 = fopen($saveto2, 'w');
  209.  
  210.     fwrite($fp, $raw);
  211.     fwrite($fp2, $raw);
  212.     fclose($fp);
  213.     fclose($fp2);
  214. }
  215.  
  216. /**
  217.  * @param $pricebd
  218.  * @param $titlebd
  219.  * @return string
  220.  */
  221. function price_replace($pricebd)
  222. {
  223.     $price_replace = str_replace(" грн.", "", $pricebd);
  224.     $r = str_replace(' ', '', $price_replace) . '000000';
  225.     return $r;
  226. }
  227.  
  228. function country ($db_connect, $location){
  229.  
  230.     $explode = explode(",", $location);
  231.  
  232.     $city = trim($explode[0]);
  233.     $reg = trim($explode[1]);
  234.     $region_sql = mysqli_query($db_connect, "select pk_i_id from oc_t_region where s_name = '$reg'");
  235.     $city_sql = mysqli_query($db_connect, "select pk_i_id from oc_t_city where s_name = '$city'");
  236.     $region_res = mysqli_fetch_all($region_sql);
  237.     $city_res = mysqli_fetch_all($city_sql);
  238.  
  239.     return array($region_res, $city_res, $reg, $city);
  240.  
  241. }
  242.  
  243. /**
  244.  * @param $db_connect
  245.  * @param $titlebd
  246.  * @param $r
  247.  * @param $namedb
  248.  * @param $contentbd
  249.  * @param $telbd
  250.  * @param $img_name
  251.  */
  252. function insert_querys($db_connect, $titlebd, $r, $namedb, $contentbd, $telbd, $t, $reg, $c, $reg_name, $city_name)
  253. {
  254.     $select_query = mysqli_query($db_connect, "select s_title from `oc_t_item_description`");
  255.     $query_res = mysqli_fetch_all($select_query, MYSQLI_ASSOC);
  256.     $time = date('Y-m-d H:i:s');
  257.  
  258.     if ($query_res !== $titlebd) {
  259.  
  260.         mysqli_query($db_connect, "insert into `oc_t_item` (fk_i_category_id, dt_pub_date, i_price, fk_c_currency_code, s_contact_name, b_active) values ( '$t', '$time', '$r', 'GRN','$namedb', '1' )");
  261.         $id = mysqli_insert_id($db_connect);
  262.         mysqli_query($db_connect, "insert into `oc_t_item_description` (fk_i_item_id, s_title, s_description) values ($id , '$titlebd', '$contentbd')");
  263.         mysqli_query($db_connect, "insert into `oc_t_item_location` (fk_i_item_id, fk_i_region_id, fk_i_city_id, s_city_area, fk_c_country_code, s_country, s_region, s_city) values ('$id', '$reg', '$c', '$telbd', 'UA', 'Ukraine', '$reg_name', '$city_name')");
  264.         mysqli_query($db_connect, "insert into `oc_t_item_resource` (fk_i_item_id, s_name, s_extension, s_content_type, s_path) values ($id, 'img', 'jpg', 'image/jpeg',  'oc-content/uploads/28/')");
  265.         $sel = mysqli_query($db_connect, "select pk_i_id from oc_t_item_resource where `fk_i_item_id` = $id");
  266.         $feth = mysqli_fetch_all($sel);
  267.         return $feth;
  268.     }
  269. }
  270.  
  271. foreach ($links as $l) {
  272.     set_time_limit(0);
  273.     $html = curl_content($l);
  274.     phpQuery::newDocument($html);
  275.     $menu = pq('#offers_table')->find('.breakword');
  276.     $prices = pq('.space')->find('a.detailsLink');
  277.     $cat = pq('color-9')->find('.color-9>small')->text();
  278.  
  279.     foreach ($menu as $res) {
  280.  
  281.         $pqres = pq($res);
  282.         $linkarray = $pqres->find('a')->attr('href');
  283.         $pricearray = $pqres->find('p.price>strong')->text();
  284.         $title = $pqres->find('a.marginright5>strong')->text();
  285.         $cat = $pqres->find('.color-9>small')->text();
  286.         $url = $linkarray;
  287.  
  288.         list($content, $json) = get_phone_number($url);
  289.  
  290.         phpQuery::newDocument($content);
  291.         $htmlcon = pq('#textContent>p')->text();
  292.         $big_img = pq('.photo-glow')->find('img')->attr('src');
  293.  
  294.         foreach (pq('.tcenter>.photo-glow')->find('img') as $imgs) {
  295.             $imgs = pq($imgs)->attr('src');
  296.  
  297.         }
  298.  
  299.         $name = pq('.offer-user__details>h4')->find('a')->text();
  300.         $location = pq('.offer-titlebox__details')->find('.show-map-link')->text();
  301.         phpQuery::newDocument($json->value);
  302.         $tel = pq('span')->text();
  303.  
  304.         if (empty($tel)) {
  305.             $tel = $json->value;
  306.         }
  307.  
  308.         $resarray = array(
  309.             'link' => $linkarray,
  310.             'price' => $pricearray,
  311.             'title' => $title,
  312.             'tel' => $tel,
  313.             'content' => $htmlcon,
  314.             'name' => $name,
  315.         );
  316.  
  317.         $linkbd = $resarray['link'];
  318.         $pricebd = $resarray['price'];
  319.         $titlebd = $resarray['title'];
  320.         $telbd = $resarray['tel'];
  321.         $namedb = $resarray['name'];
  322.         $contentbd = $resarray['content'];
  323.  
  324.         $r = price_replace($pricebd);
  325.  
  326.         $rep = category_replace($cat, $replace);
  327.         $sql = mysqli_query($db_connect, "SELECT `fk_i_category_id`, `s_name` FROM `oc_t_category_description` WHERE 1");
  328.         $result = mysqli_fetch_all($sql);
  329.         list($reg_num, $city_num, $reg_name, $city_name) = country($db_connect, $location);
  330.  
  331.         foreach ($reg_num as $region) {
  332.             foreach ($region as $reg) {
  333.  
  334.             }
  335.         }
  336.  
  337.         foreach ($city_num as $city) {
  338.             foreach ($city as $c) {
  339.  
  340.             }
  341.         }
  342.  
  343.         foreach ($result as $t) {
  344.             if ($t[1] == $rep) {
  345.  
  346.                 $feth = insert_querys($db_connect, $titlebd, $r, $namedb, $contentbd, $telbd, $t[0], $reg, $c, $reg_name, $city_name);
  347.                 [$thumb,$preview] = ImagesHelper::generateImagesPath($feth);
  348.                 get_img($imgs ,$thumb, $preview);
  349.                 var_dump($thumb);
  350.                 var_dump($preview);
  351.  
  352.             }
  353.  
  354.         }
  355.     }
  356.  
  357. }
  358.  
  359. ?>
Add Comment
Please, Sign In to add comment