Advertisement
Nexo78

Untitled

Oct 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.71 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();
  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(): \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.         $records = [
  122.             0 => [0 => '4031'],
  123.             1 => [0 => '4030'],
  124.             2 => [0 => '4029']
  125.         ];
  126.  
  127.         foreach($records as $record => $value){
  128.             yield $value;
  129.         }
  130.  
  131.     }
  132. }
  133.  
  134. function curl_content($url){
  135.     $ch = curl_init($url);
  136.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  137.     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; WinNT; en; rv:1.0.2) Gecko/20030311 Beonex/0.8.2-stable');
  138.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  139.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  140.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
  141.  
  142.     $html=curl_exec($ch);
  143.     unset($url);
  144.     return $html;
  145.  
  146. }
  147.  
  148. function category_replace($cat, $replace){
  149.  
  150.     $slash_replace = str_replace('/', 'и', $cat);
  151.     $replaces = str_replace($replace, '', $slash_replace);
  152.     return trim($replaces);
  153.  
  154. }
  155.  
  156. /**
  157.  * @param $url
  158.  * @param $id
  159.  * @param $token
  160.  * @return array
  161.  */
  162. function get_phone_number($url)
  163. {
  164.     $cookie_path = $_SERVER['DOCUMENT_ROOT'] . '/cookie.dat';
  165.     preg_match('|-ID(.*).html|', $url, $id);
  166.     $olx = curl_init($url);
  167.     curl_setopt($olx, CURLOPT_SSL_VERIFYPEER, false);
  168.     curl_setopt($olx, CURLOPT_RETURNTRANSFER, true);
  169.     curl_setopt($olx, CURLOPT_HEADER, 1);
  170.     curl_setopt($olx, CURLOPT_COOKIEFILE, $cookie_path);
  171.     curl_setopt($olx, CURLOPT_COOKIEJAR, $cookie_path);
  172.     $result = curl_exec($olx);
  173.     $content = $result;
  174.     curl_close($olx);
  175.     preg_match("|phoneToken = '(.*)';|", $result, $token);
  176.     $olx_number = curl_init('https://www.olx.ua/ajax/misc/contact/phone/' . $id[1] . '/?pt=' . $token[1]);
  177.     curl_setopt($olx_number, CURLOPT_HTTPHEADER, [
  178.         'Host: www.olx.ua',
  179.         'Accept: */*',
  180.         'Accept-Language: uk,ru;q=0.8,en-US;q=0.5,en;q=0.3',
  181.         'Accept-Encoding: gzip, deflate, br',
  182.         'Connection: keep-alive',
  183.         'X-Requested-With: XMLHttpRequest'
  184.     ]);
  185.     curl_setopt($olx_number, CURLOPT_REFERER, $url);
  186.     curl_setopt($olx_number, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  187.     curl_setopt($olx_number, CURLOPT_COOKIEFILE, $cookie_path);
  188.     curl_setopt($olx_number, CURLOPT_SSL_VERIFYPEER, false);
  189.     curl_setopt($olx_number, CURLOPT_RETURNTRANSFER, true);
  190.     $result = curl_exec($olx_number);
  191.     curl_close($olx_number);
  192.     unlink( $_SERVER['DOCUMENT_ROOT'].'/cookie.dat');
  193.     $json = json_decode($result);
  194.     return array($content, $json);
  195. }
  196.  
  197.  
  198. /**
  199.  * @param $db_connect
  200.  * @param $linkimgbd
  201.  */
  202. function get_img($db_connect, $imgs , $feth){
  203.  
  204.     var_dump($feth);
  205.     $pk_i_id = $feth[0][0];
  206.     $orig_img_name = $pk_i_id;
  207.     $thumb_img_name = $pk_i_id . '_thumbnail' ;
  208.     $preview_img_name = $pk_i_id . '_preview' ;
  209.  
  210.     $saveto1 = '../dpm/oc-content/uploads/28/' . $orig_img_name . '.jpg';
  211.     $saveto2 = '../dpm/oc-content/uploads/28/' . $thumb_img_name . '.jpg';
  212.     $saveto3 = '../dpm/oc-content/uploads/28/' . $preview_img_name . '.jpg';
  213.     $ch = curl_init($imgs);
  214.     curl_setopt($ch, CURLOPT_HEADER, 0);
  215.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  216.     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  217.     $raw = curl_exec($ch);
  218.     curl_close($ch);
  219.  
  220.     $fp = fopen($saveto1, 'w');
  221.     $fp2 = fopen($saveto2, 'w');
  222.     $fp3 = fopen($saveto3, 'w');
  223.  
  224.     fwrite($fp, $raw);
  225.     fwrite($fp2, $raw);
  226.     fwrite($fp3, $raw);
  227.     fclose($fp);
  228.     fclose($fp2);
  229.     fclose($fp3);
  230. }
  231.  
  232. /**
  233.  * @param $pricebd
  234.  * @param $titlebd
  235.  * @return string
  236.  */
  237. function price_replace($pricebd)
  238. {
  239.     $price_replace = str_replace(" грн.", "", $pricebd);
  240.     $r = str_replace(' ', '', $price_replace) . '000000';
  241.     return $r;
  242. }
  243.  
  244. function country ($db_connect, $location){
  245.  
  246.     $explode = explode(",", $location);
  247.  
  248.     $city = trim($explode[0]);
  249.     $reg = trim($explode[1]);
  250.     $region_sql = mysqli_query($db_connect, "select pk_i_id from oc_t_region where s_name = '$reg'");
  251.     $city_sql = mysqli_query($db_connect, "select pk_i_id from oc_t_city where s_name = '$city'");
  252.     $region_res = mysqli_fetch_all($region_sql);
  253.     $city_res = mysqli_fetch_all($city_sql);
  254.  
  255.     return array($region_res, $city_res, $reg, $city);
  256.  
  257. }
  258.  
  259. /**
  260.  * @param $db_connect
  261.  * @param $titlebd
  262.  * @param $r
  263.  * @param $namedb
  264.  * @param $contentbd
  265.  * @param $telbd
  266.  * @param $img_name
  267.  */
  268. function insert_querys($db_connect, $titlebd, $r, $namedb, $contentbd, $telbd, $t, $reg, $c, $reg_name, $city_name)
  269. {
  270.     $select_query = mysqli_query($db_connect, "select s_title from `oc_t_item_description`");
  271.     $query_res = mysqli_fetch_all($select_query, MYSQLI_ASSOC);
  272.     $time = date('Y-m-d H:i:s');
  273.  
  274.     if ($query_res !== $titlebd) {
  275.  
  276.         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' )");
  277.         $id = mysqli_insert_id($db_connect);
  278.         mysqli_query($db_connect, "insert into `oc_t_item_description` (fk_i_item_id, s_title, s_description) values ($id , '$titlebd', '$contentbd')");
  279.         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')");
  280.         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/')");
  281.         $sel = mysqli_query($db_connect, "select pk_i_id from oc_t_item_resource where `fk_i_item_id` = $id");
  282.         $feth = mysqli_fetch_all($sel);
  283.         return $feth;
  284.     }
  285. }
  286.  
  287. foreach ($links as $l) {
  288.     set_time_limit(0);
  289.     $html = curl_content($l);
  290.     phpQuery::newDocument($html);
  291.     $menu = pq('#offers_table')->find('.breakword');
  292.     $prices = pq('.space')->find('a.detailsLink');
  293.     $cat = pq('color-9')->find('.color-9>small')->text();
  294.  
  295.     foreach ($menu as $res) {
  296.  
  297.         $pqres = pq($res);
  298.         $linkarray = $pqres->find('a')->attr('href');
  299.         $pricearray = $pqres->find('p.price>strong')->text();
  300.         $title = $pqres->find('a.marginright5>strong')->text();
  301.         $cat = $pqres->find('.color-9>small')->text();
  302.         $url = $linkarray;
  303.  
  304.         list($content, $json) = get_phone_number($url);
  305.  
  306.         phpQuery::newDocument($content);
  307.         $htmlcon = pq('#textContent>p')->text();
  308.         $big_img = pq('.photo-glow')->find('img')->attr('src');
  309.  
  310.         foreach (pq('.tcenter>.photo-glow')->find('img') as $imgs) {
  311.             $imgs = pq($imgs)->attr('src');
  312.  
  313.         }
  314.  
  315.         $name = pq('.offer-user__details>h4')->find('a')->text();
  316.         $location = pq('.offer-titlebox__details')->find('.show-map-link')->text();
  317.         phpQuery::newDocument($json->value);
  318.         $tel = pq('span')->text();
  319.  
  320.         if (empty($tel)) {
  321.             $tel = $json->value;
  322.         }
  323.  
  324.         $resarray = array(
  325.             'link' => $linkarray,
  326.             'price' => $pricearray,
  327.             'title' => $title,
  328.             'tel' => $tel,
  329.             'content' => $htmlcon,
  330.             'name' => $name,
  331.         );
  332.  
  333.         $linkbd = $resarray['link'];
  334.         $pricebd = $resarray['price'];
  335.         $titlebd = $resarray['title'];
  336.         $telbd = $resarray['tel'];
  337.         $namedb = $resarray['name'];
  338.         $contentbd = $resarray['content'];
  339.  
  340.         $r = price_replace($pricebd);
  341.  
  342.         $rep = category_replace($cat, $replace);
  343.         $sql = mysqli_query($db_connect, "SELECT `fk_i_category_id`, `s_name` FROM `oc_t_category_description` WHERE 1");
  344.         $result = mysqli_fetch_all($sql);
  345.         list($reg_num, $city_num, $reg_name, $city_name) = country($db_connect, $location);
  346.  
  347.         foreach ($reg_num as $region) {
  348.             foreach ($region as $reg) {
  349.  
  350.             }
  351.         }
  352.  
  353.         foreach ($city_num as $city) {
  354.             foreach ($city as $c) {
  355.  
  356.             }
  357.         }
  358.  
  359.         foreach ($result as $t) {
  360.             if ($t[1] == $rep) {
  361.  
  362.                 $feth = insert_querys($db_connect, $titlebd, $r, $namedb, $contentbd, $telbd, $t[0], $reg, $c, $reg_name, $city_name);
  363.                 [$thumb,$preview] = ImagesHelper::generateImagesPath($feth);
  364.                 var_dump($thumb);
  365.                 var_dump($preview);
  366.  
  367.             }
  368.  
  369.         }
  370.     }
  371.  
  372. }
  373.  
  374. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement