Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once __DIR__ . '/functions.php';
- require_once __DIR__ . '/src/PHPMailer.php';
- require_once __DIR__ . '/src/SMTP.php';
- require_once __DIR__ . '/vendor/autoload.php';
- error_reporting(E_ERROR);
- $config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
- $logger = new Katzgrau\KLogger\Logger(__DIR__.'/logs');
- function downloadPage($url) {
- $curl = curl_init();
- // You can also set the URL you want to communicate with by doing this:
- // $curl = curl_init('http://localhost/echoservice');
- $headers = [
- 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding:gzip, deflate',
- 'Accept-Language:en-US,en;q=0.8',
- 'Accept-Encoding: gzip, deflate',
- 'Cache-Control: no-cache',
- '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',
- 'Host:kurses.com.ua',
- 'Pragma:no-cache',
- 'Upgrade-Insecure-Requests:1',
- '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'
- ];
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- // Set the url path we want to call
- curl_setopt($curl, CURLOPT_URL, $url);
- // Make it so the data coming back is put into a string
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- // You can also bunch the above commands into an array if you choose using: curl_setopt_array
- // Send the request
- $result = curl_exec($curl);
- // Get some cURL session information back
- $info = curl_getinfo($curl);
- // echo 'content type: ' . $info['content_type'] . PHP_EOL;
- // echo 'http code: ' . $info['http_code'] . PHP_EOL;
- // Free up the resources $curl is using
- curl_close($curl);
- // echo $result;
- return $result;
- }
- function parseColumns($htmlString, $compareValue, $compareReserve) {
- $isMatch = false;
- $links = [];
- $floats = [];
- $reserves = [];
- $title = '';
- $doc = DOMLoadHTML($htmlString);
- $table = getElementById($doc, 'search-results');
- $caption = $table->getElementsByTagName('caption')->item(0);
- $title = $caption->textContent;
- $title = trim(str_replace(['Список курсов обмена из','в','1'], ['','->',''], $title));
- $stringFloatValue = "0.1";
- $isFirstRow = true;
- $tableParsed = false;
- $tableHTML = '';
- $nodes2delete = [];
- $matchedRows = 0;
- $firstColumnsRowKeys = [];
- foreach ($table->getElementsByTagName('tr') as $key => $row) {
- foreach($row->getElementsByTagName('td') as $col) {
- if ((int)$col->getAttribute('rowspan') > 4) {
- $floatValue = (float)$col->textContent;
- $firstColumnsRowKeys[] = [
- "index" => $key,
- "value" => $floatValue
- ];
- }
- $row->setAttribute('data-value', $floatValue);
- }
- }
- foreach ($table->getElementsByTagName('tr') as $key => $row) {
- $isRowMatchByValue = false;
- foreach ($firstColumnsRowKeys as $k => $v) {
- if ($v['index'] == $key) {
- $firstColumnRowKey = $k;
- }
- }
- if (isset($firstColumnsRowKeys[$firstColumnRowKey + 1])) {
- $floatValueCurr = (float)$row->getAttribute('data-value');
- $floatValueNext = (float)$firstColumnsRowKeys[$firstColumnRowKey + 1]['value'];
- $isRowMatchByValue = ($floatValueCurr > $floatValueNext) && $floatValueNext > $compareValue;
- $row->setAttribute('data-match-value', (int)$isRowMatchByValue);
- // if ($isRowMatchByValue) {
- // echo PHP_EOL;
- // echo $title . PHP_EOL;
- // echo "floatValue curr: " . $floatValueCurr . PHP_EOL;
- // echo "floatValue next: " . $floatValueNext . PHP_EOL;
- // echo "floatValue CMPP: " . $compareValue . PHP_EOL;
- // echo "isRowMatch: " . (int)$isRowMatch . PHP_EOL;
- // $matchedRows++;
- // } else {
- // $nodes2delete[] = $row;
- // }
- }
- foreach($row->getElementsByTagName('th') as $col) {
- $col->setAttribute('style', 'border: 1px solid #aaa;');
- }
- foreach($row->getElementsByTagName('td') as $col) {
- $col->setAttribute('style', 'border: 1px solid #aaa;');
- $spanElements = $col->getElementsByTagName('span');
- if ($spanElements->length) {
- $firstSpan = $spanElements->item(0);
- $spanReserveValue = (int)$firstSpan->textContent;
- $isRowMatchByReserve = false;
- if ($spanReserveValue) {
- $isRowMatchByReserve = $spanReserveValue >= $compareReserve;
- }
- if ($isRowMatchByValue && $isRowMatchByReserve) {
- $row->setAttribute('style', 'background-color: rgba(16, 119, 16, 0.64); font-size: 150%;');
- }
- $row->setAttribute('data-match-reserve', (int)$isRowMatchByReserve);
- }
- if ($row->getAttribute('class') == 'separate') {
- $col->setAttribute('style', 'border: 1px solid #aaa; background-color: #FCF8D2;');
- }
- if ((int)$col->getAttribute('rowspan') == 2) {
- $colHref = $col->getElementsByTagName('a')->item(0);
- $colItalic = $col->getElementsByTagName('i')->item(0);
- $colUrl = 'http://kurses.com.ua/' . $colHref->getAttribute('data-url');
- $colHref->setAttribute('href', $colUrl);
- $colHref->removeAttribute('data-url', $colUrl);
- $colHref->removeAttribute('class', $colUrl);
- if ($colItalic) {
- $colItalic->setAttribute('style', 'display: none');
- // $colHref->setAttribute('style', 'color: red');
- } else {
- $colHref->setAttribute('style', 'color: green');
- }
- $colHTML = DOMInnerHTML($col);
- $links[$stringFloatValue][] = $colHTML;
- }
- }
- }
- foreach ($table->getElementsByTagName('tr') as $key => $row) {
- $isRowMatchByValue = (bool)$row->getAttribute('data-match-value');
- $isRowMatchByReserve = (bool)$row->getAttribute('data-match-reserve');
- if ($isRowMatchByValue) {
- $matchedRows++;
- }
- }
- $tableStyles = [
- 'width: 100%;',
- 'font-size: 13px;',
- 'border-spacing: 1px;',
- 'border: 1px;',
- 'border-collapse: collapse;',
- 'text-align: center;',
- 'margin: 0 auto;'
- ];
- $tableHTML = '<table cellspacing="0" cellpadding="0" border="0" class="table center" id="search-results" ';
- $tableHTML .= 'style="' . implode(' ', $tableStyles) . '">';
- $tableHTML .= DOMInnerHTML($table);
- $tableHTML .= '</table>';
- return [
- 'isMatch' => ((int)$matchedRows > 0),
- 'title' => $title,
- 'tableHTML' => $tableHTML
- ];
- }
- function compileBody($values, $compareValue, $compareReserve, $url) {
- $body = '';
- $suffix = '';
- $body .= '<div>' . $suffix;
- $body .= '<h1 align="center"> Установлен коефициент: ' . $compareValue . '</h1>' . $suffix;
- $body .= '<h2 align="center"> Установлен минимальный резерв: ' . $compareReserve . '</h2>' . $suffix;
- $body .= '<br>' . $suffix;
- $body .= '<h3 align="center" ><a href="' . $url . '" target="_blank" >перейти к странице kurses.com.ua</a></h3>' . $suffix;
- $body .= '<br><br>' . $suffix;
- $body .= $values['tableHTML'];
- $body .= '<br><br>' . $suffix;
- $body .= '</div>' . $suffix;
- return $body;
- }
- function sendEmail($values, $config, $compareValue, $body) {
- global $logger;
- $mail = new PHPMailer(); // create a new object
- $mail->IsSMTP(); // enable SMTP
- $mail->CharSet = 'UTF-8';
- $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
- $mail->SMTPAuth = true; // authentication enabled
- $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
- $mail->Host = "smtp.gmail.com";
- $mail->Port = 465; // or 587
- $mail->IsHTML(true);
- $mail->Username = $config['from_email'];
- $mail->Password = $config['from_password'];
- $mail->SetFrom($config['from_email']);
- $mail->Subject = "KURSES.COM.UA - " . $values['title'];
- $mail->Body = $body;
- // $mail->AddAddress($config['to_email']);
- $mail->AddAddress('oleggg.normal@gmail.com');
- $mail->AddAddress('igorshinal@gmail.com');
- // $mail->AddAddress('mikeai686@gmail.com');
- if(!$mail->Send()) {
- // echo "Mailer Error: " . $mail->ErrorInfo;
- $logger->error("Mailer Error: " . $mail->ErrorInfo);
- } else {
- // echo "Message has been sent to" . $config['to_email'] . PHP_EOL;
- $logger->info("Message has been sent to" . $config['to_email']);
- }
- }
- if (!file_exists(__DIR__ . '/urls.txt')) {
- throw new Exception('file urls.txt is not exists');
- }
- $urlsList = file(__DIR__ . '/urls.txt');
- foreach ($urlsList as $key => $string) {
- // echo $string . PHP_EOL;
- $exploded = explode('->', $string);
- $value = trim($exploded[0]);
- $reserve = trim($exploded[1]);
- $url = trim($exploded[2]);
- if (empty($value) || empty($url) || empty($reserve)) {
- continue;
- }
- $result = downloadPage($url);
- // file_put_contents('result' . $key . '.html', $result);
- // echo '------------------------------------' . PHP_EOL;
- // echo $value . PHP_EOL;
- $values = parseColumns($result, $value, $reserve);
- $isMatch = $values['isMatch'];
- $logger->info('key = ' . $key);
- $logger->info("set value:" . $value);
- $logger->info("set reserve:" . $value);
- $logger->info("isMatch: " . (int)$isMatch);
- if ($values['isMatch']) {
- $body = compileBody($values, $value, $reserve, $url);
- // var_dump($body);
- echo $url . PHP_EOL;
- sendEmail($values, $config, $value, $body);
- }
- sleep(1);
- }
- // for ($i=0; $i<=10; $i=$i+2) {
- // $value = 1.01;
- // $reserve = 3;
- // $result = file_get_contents('result' . $i . '.html');
- // echo '------------------------------------' . PHP_EOL;
- // $values = parseColumns($result, $value, $reserve);
- // if ($values['isMatch']) {
- // // print_r($values);
- // echo $i . PHP_EOL;
- // echo $values['title'] . PHP_EOL;
- // }
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement