Advertisement
Guest User

script.php

a guest
Oct 9th, 2017
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.70 KB | None | 0 0
  1. <?php
  2.  
  3. require_once __DIR__ . '/functions.php';
  4. require_once __DIR__ . '/src/PHPMailer.php';
  5. require_once __DIR__ . '/src/SMTP.php';
  6. require_once __DIR__ . '/vendor/autoload.php';
  7.  
  8. error_reporting(E_ERROR);
  9.  
  10. $config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
  11.  
  12. $logger = new Katzgrau\KLogger\Logger(__DIR__.'/logs');
  13.  
  14. function downloadPage($url) {
  15.  
  16.     $curl = curl_init();
  17.     // You can also set the URL you want to communicate with by doing this:
  18.     // $curl = curl_init('http://localhost/echoservice');
  19.  
  20.     $headers = [
  21.         'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  22.         'Accept-Encoding:gzip, deflate',
  23.         'Accept-Language:en-US,en;q=0.8',
  24.         'Accept-Encoding: gzip, deflate',
  25.         'Cache-Control: no-cache',
  26.         'Cookie:jv_enter_ts_S3BYRQsqay=1507005563608; jv_visits_count_S3BYRQsqay=1; jv_utm_S3BYRQsqay=; client=hdvgfjdstv5u6e6gohhfij03m3; _ga=GA1.3.535033701.1507005562; _gid=GA1.3.473500222.1507376143; jv_pages_count_S3BYRQsqay=2',
  27.         'Host:kurses.com.ua',
  28.         'Pragma:no-cache',
  29.         'Upgrade-Insecure-Requests:1',
  30.         'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/61.0.3163.100 Chrome/61.0.3163.100 Safari/537.36'
  31.     ];
  32.  
  33.     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  34.  
  35.     // Set the url path we want to call
  36.     curl_setopt($curl, CURLOPT_URL, $url);
  37.     // Make it so the data coming back is put into a string
  38.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  39.     // You can also bunch the above commands into an array if you choose using: curl_setopt_array
  40.  
  41.     // Send the request
  42.     $result = curl_exec($curl);
  43.  
  44.     // Get some cURL session information back
  45.     $info = curl_getinfo($curl);
  46.     // echo 'content type: ' . $info['content_type'] . PHP_EOL;
  47.     // echo 'http code: ' . $info['http_code'] . PHP_EOL;
  48.  
  49.     // Free up the resources $curl is using
  50.     curl_close($curl);
  51.  
  52.     // echo $result;
  53.     return $result;
  54. }
  55.  
  56. function parseColumns($htmlString, $compareValue, $compareReserve) {
  57.  
  58.     $isMatch = false;
  59.     $links = [];
  60.     $floats = [];
  61.     $reserves = [];
  62.  
  63.     $title = '';
  64.  
  65.     $doc = DOMLoadHTML($htmlString);
  66.  
  67.     $table = getElementById($doc, 'search-results');
  68.  
  69.     $caption = $table->getElementsByTagName('caption')->item(0);
  70.     $title = $caption->textContent;
  71.     $title = trim(str_replace(['Список курсов обмена из','в','1'], ['','->',''], $title));
  72.  
  73.     $stringFloatValue = "0.1";
  74.  
  75.     $isFirstRow = true;
  76.     $tableParsed = false;
  77.  
  78.     $tableHTML = '';
  79.  
  80.     $nodes2delete = [];
  81.     $matchedRows = 0;
  82.  
  83.  
  84.     $firstColumnsRowKeys = [];
  85.  
  86.     foreach ($table->getElementsByTagName('tr') as $key => $row) {
  87.         foreach($row->getElementsByTagName('td') as $col) {
  88.  
  89.             if ((int)$col->getAttribute('rowspan') > 4) {
  90.  
  91.                 $floatValue = (float)$col->textContent;
  92.  
  93.                 $firstColumnsRowKeys[] = [
  94.                     "index" => $key,
  95.                     "value" => $floatValue
  96.                 ];
  97.  
  98.             }
  99.  
  100.             $row->setAttribute('data-value', $floatValue);
  101.  
  102.         }
  103.     }
  104.  
  105.  
  106.     foreach ($table->getElementsByTagName('tr') as $key => $row) {
  107.  
  108.         $isRowMatchByValue = false;
  109.  
  110.         foreach ($firstColumnsRowKeys as $k => $v) {
  111.             if ($v['index'] == $key) {
  112.                 $firstColumnRowKey = $k;
  113.             }
  114.         }
  115.  
  116.         if (isset($firstColumnsRowKeys[$firstColumnRowKey + 1])) {
  117.  
  118.  
  119.             $floatValueCurr = (float)$row->getAttribute('data-value');
  120.             $floatValueNext = (float)$firstColumnsRowKeys[$firstColumnRowKey + 1]['value'];
  121.  
  122.             $isRowMatchByValue = ($floatValueCurr > $floatValueNext) && $floatValueNext > $compareValue;
  123.  
  124.             $row->setAttribute('data-match-value', (int)$isRowMatchByValue);
  125.  
  126.             // if ($isRowMatchByValue) {
  127.             //  echo PHP_EOL;
  128.             //  echo $title . PHP_EOL;
  129.             //  echo "floatValue curr: " . $floatValueCurr . PHP_EOL;
  130.             //  echo "floatValue next: " . $floatValueNext . PHP_EOL;
  131.             //  echo "floatValue CMPP: " . $compareValue . PHP_EOL;
  132.             //  echo "isRowMatch: " . (int)$isRowMatch . PHP_EOL;
  133.             //  $matchedRows++;
  134.             // } else {
  135.             //  $nodes2delete[] = $row;
  136.             // }
  137.  
  138.         }
  139.  
  140.  
  141.         foreach($row->getElementsByTagName('th') as $col) {
  142.             $col->setAttribute('style', 'border: 1px solid #aaa;');
  143.         }
  144.  
  145.         foreach($row->getElementsByTagName('td') as $col) {
  146.  
  147.             $col->setAttribute('style', 'border: 1px solid #aaa;');
  148.  
  149.             $spanElements = $col->getElementsByTagName('span');
  150.  
  151.             if ($spanElements->length) {
  152.                 $firstSpan = $spanElements->item(0);
  153.                 $spanReserveValue = (int)$firstSpan->textContent;
  154.  
  155.                 $isRowMatchByReserve = false;
  156.                 if ($spanReserveValue) {
  157.                     $isRowMatchByReserve = $spanReserveValue >= $compareReserve;
  158.                 }
  159.  
  160.                 if ($isRowMatchByValue && $isRowMatchByReserve) {
  161.                     $row->setAttribute('style', 'background-color: rgba(16, 119, 16, 0.64); font-size: 150%;');
  162.                 }
  163.  
  164.                 $row->setAttribute('data-match-reserve', (int)$isRowMatchByReserve);
  165.  
  166.             }
  167.  
  168.             if ($row->getAttribute('class') == 'separate') {
  169.                 $col->setAttribute('style', 'border: 1px solid #aaa; background-color: #FCF8D2;');
  170.             }
  171.  
  172.  
  173.             if ((int)$col->getAttribute('rowspan') == 2) {
  174.  
  175.                 $colHref = $col->getElementsByTagName('a')->item(0);
  176.                 $colItalic = $col->getElementsByTagName('i')->item(0);
  177.                 $colUrl = 'http://kurses.com.ua/' . $colHref->getAttribute('data-url');
  178.                 $colHref->setAttribute('href', $colUrl);
  179.                 $colHref->removeAttribute('data-url', $colUrl);
  180.                 $colHref->removeAttribute('class', $colUrl);
  181.  
  182.  
  183.                 if ($colItalic) {
  184.                     $colItalic->setAttribute('style', 'display: none');
  185.                     // $colHref->setAttribute('style', 'color: red');
  186.                 } else {
  187.                     $colHref->setAttribute('style', 'color: green');
  188.                 }
  189.  
  190.                 $colHTML = DOMInnerHTML($col);
  191.                 $links[$stringFloatValue][] = $colHTML;
  192.             }
  193.         }
  194.  
  195.     }
  196.  
  197.     foreach ($table->getElementsByTagName('tr') as $key => $row) {
  198.  
  199.         $isRowMatchByValue = (bool)$row->getAttribute('data-match-value');
  200.         $isRowMatchByReserve = (bool)$row->getAttribute('data-match-reserve');
  201.  
  202.         if ($isRowMatchByValue) {
  203.             $matchedRows++;
  204.         }
  205.     }
  206.  
  207.     $tableStyles = [
  208.         'width: 100%;',
  209.         'font-size: 13px;',
  210.         'border-spacing: 1px;',
  211.         'border: 1px;',
  212.         'border-collapse: collapse;',
  213.         'text-align: center;',
  214.         'margin: 0 auto;'
  215.     ];
  216.  
  217.     $tableHTML = '<table cellspacing="0" cellpadding="0" border="0" class="table center" id="search-results" ';
  218.     $tableHTML .= 'style="' . implode(' ', $tableStyles) . '">';
  219.     $tableHTML .= DOMInnerHTML($table);
  220.     $tableHTML .= '</table>';
  221.  
  222.     return [
  223.         'isMatch' => ((int)$matchedRows > 0),
  224.         'title' => $title,
  225.         'tableHTML' => $tableHTML
  226.     ];
  227.  
  228. }
  229.  
  230.  
  231. function compileBody($values, $compareValue, $compareReserve, $url) {
  232.  
  233.     $body = '';
  234.     $suffix = '';
  235.  
  236.     $body .= '<div>' . $suffix;
  237.     $body .= '<h1 align="center"> Установлен коефициент: ' . $compareValue . '</h1>' . $suffix;
  238.     $body .= '<h2 align="center"> Установлен минимальный резерв: ' . $compareReserve . '</h2>' . $suffix;
  239.  
  240.     $body .= '<br>' . $suffix;
  241.  
  242.     $body .= '<h3 align="center" ><a href="' . $url . '" target="_blank" >перейти к странице kurses.com.ua</a></h3>' . $suffix;
  243.  
  244.     $body .= '<br><br>' . $suffix;
  245.  
  246.     $body .= $values['tableHTML'];
  247.  
  248.     $body .= '<br><br>' . $suffix;
  249.     $body .= '</div>' . $suffix;
  250.  
  251.     return $body;
  252. }
  253.  
  254. function sendEmail($values, $config, $compareValue, $body) {
  255.  
  256.     global $logger;
  257.  
  258.     $mail = new PHPMailer(); // create a new object
  259.     $mail->IsSMTP(); // enable SMTP
  260.     $mail->CharSet = 'UTF-8';
  261.     $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
  262.     $mail->SMTPAuth = true; // authentication enabled
  263.     $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
  264.     $mail->Host = "smtp.gmail.com";
  265.     $mail->Port = 465; // or 587
  266.     $mail->IsHTML(true);
  267.     $mail->Username = $config['from_email'];
  268.     $mail->Password = $config['from_password'];
  269.     $mail->SetFrom($config['from_email']);
  270.     $mail->Subject = "KURSES.COM.UA - " . $values['title'];
  271.     $mail->Body = $body;
  272.     // $mail->AddAddress($config['to_email']);
  273.     $mail->AddAddress('oleggg.normal@gmail.com');
  274.     $mail->AddAddress('igorshinal@gmail.com');
  275.     // $mail->AddAddress('mikeai686@gmail.com');
  276.  
  277.      if(!$mail->Send()) {
  278.         // echo "Mailer Error: " . $mail->ErrorInfo;
  279.         $logger->error("Mailer Error: " . $mail->ErrorInfo);
  280.      } else {
  281.         // echo "Message has been sent to" . $config['to_email'] . PHP_EOL;
  282.         $logger->info("Message has been sent to" . $config['to_email']);
  283.      }
  284. }
  285.  
  286.  
  287. if (!file_exists(__DIR__ . '/urls.txt')) {
  288.     throw new Exception('file urls.txt is not exists');
  289. }
  290.  
  291. $urlsList = file(__DIR__ . '/urls.txt');
  292.  
  293. foreach ($urlsList as $key => $string) {
  294.  
  295.     // echo $string . PHP_EOL;
  296.  
  297.     $exploded = explode('->', $string);
  298.  
  299.     $value = trim($exploded[0]);
  300.     $reserve = trim($exploded[1]);
  301.     $url = trim($exploded[2]);
  302.  
  303.     if (empty($value) || empty($url) || empty($reserve)) {
  304.         continue;
  305.     }
  306.  
  307.     $result = downloadPage($url);
  308.  
  309.     // file_put_contents('result' . $key . '.html', $result);
  310.  
  311.     // echo '------------------------------------' . PHP_EOL;
  312.     // echo $value . PHP_EOL;
  313.  
  314.     $values = parseColumns($result, $value, $reserve);
  315.  
  316.     $isMatch = $values['isMatch'];
  317.  
  318.     $logger->info('key = ' . $key);
  319.  
  320.     $logger->info("set value:" . $value);
  321.     $logger->info("set reserve:" . $value);
  322.  
  323.     $logger->info("isMatch: " . (int)$isMatch);
  324.  
  325.     if ($values['isMatch']) {
  326.         $body = compileBody($values, $value, $reserve, $url);
  327.         // var_dump($body);
  328.         echo $url . PHP_EOL;
  329.         sendEmail($values, $config, $value, $body);
  330.     }
  331.  
  332.     sleep(1);
  333. }
  334.  
  335. // for ($i=0; $i<=10; $i=$i+2) {
  336.  
  337. //  $value = 1.01;
  338. //  $reserve = 3;
  339.  
  340. //  $result = file_get_contents('result' . $i . '.html');
  341. //  echo '------------------------------------' . PHP_EOL;
  342.  
  343. //  $values = parseColumns($result, $value, $reserve);
  344.  
  345. //  if ($values['isMatch']) {
  346. //      // print_r($values);
  347. //      echo $i . PHP_EOL;
  348. //      echo $values['title'] . PHP_EOL;
  349. //  }
  350.  
  351. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement