Skip to content

Instantly share code, notes, and snippets.

@AlexMcowkin
Created August 10, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMcowkin/4445be4c1576cc1b8b91 to your computer and use it in GitHub Desktop.
Save AlexMcowkin/4445be4c1576cc1b8b91 to your computer and use it in GitHub Desktop.
forexfactory parce
<?php
include_once('simple_html_dom.php');
// %progdir%\modules\wget\bin\wget.exe -q --no-cache http://forexfactory.loc/forexfactory.php
define ("PATH_TO_FOLDER", "C:/Users/Vadim/AppData/Roaming/MetaQuotes/Terminal/D0E8209F77C8CF37AD8BF550E51FF075/MQL5/Files/");
// define ("PATH_TO_FOLDER", "E:/openserver522/openserver/domains/forexfactory.loc/");
define ("CSV_FILE_NAME", "forexfactoryparce.csv");
if(!file_exists(PATH_TO_FOLDER.CSV_FILE_NAME))
{
$_tmpFile = fopen(PATH_TO_FOLDER.CSV_FILE_NAME, "w");
fclose($_tmpFile);
}
//$link = 'http://www.forexfactory.com/calendar.php?week=mar29.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=apr5.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=apr19.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=apr12.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=apr26.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=may3.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=may10.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=may17.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=may24.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=may31.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jun7.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jun14.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jun21.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jun28.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jul5.2015';
//$link = 'http://www.forexfactory.com/calendar.php?week=jul12.2015';
$link = 'http://www.forexfactory.com/calendar.php';
$csv_text = '';
$html = file_get_html($link);
$contentParce = $html->find('#flexBox_flex_calendar_mainCal', 0);
function findDataInFile($contentParce)
{
foreach($contentParce->find('.calendar_row') as $rowParce)
{
$dateCurrent = trim($rowParce->find('span.date',0)->innertext);
$realData = date("d.m.Y", strtotime(preg_replace('#<[^>]+>#', ' ', $dateCurrent))); // 25.10.1985
$realFile = fopen(PATH_TO_FOLDER.CSV_FILE_NAME, "r"); // open and read file
fseek($realFile,-15000,SEEK_END); // set pointer to the end of file and sift cursor on -10000 symbols
while (($row = fgetcsv($realFile, 200, ";")) != FALSE)
{
if($row[0] == $realData) // try to find in reading text date like 25.10.1985
{
fclose($realFile);
return TRUE;
}
}
break;
}
fclose($realFile);
return FALSE;
}
function findPositionInFile($contentParce)
{
foreach($contentParce->find('.calendar_row') as $rowParce)
{
$dateCurrent = trim($rowParce->find('span.date',0)->innertext);
$realData = date("d.m.Y", strtotime(preg_replace('#<[^>]+>#', ' ', $dateCurrent))); // 25.10.1985
$realFile = fopen(PATH_TO_FOLDER.CSV_FILE_NAME, "r"); // open and read file
fseek($realFile,-15000,SEEK_END); // set pointer to the end of file and sift cursor on -10000 symbols
while (($row = fgetcsv($realFile, 200, ";")) != FALSE)
{
if($row[0] == $realData) // try to find in reading text date like 25.10.1985
{
// $currentPosition = ftell($realFile);
fclose($realFile);
return $previuosPosition;
}
else
{
$previuosPosition = ftell($realFile);
}
}
break;
}
fclose($realFile);
return 'FALSE';
}
function parcePage($contentParce)
{
foreach($contentParce->find('.calendar_row') as $rowParce)
{
$dateCurrent = trim($rowParce->find('span.date',0)->innertext);
$timeCurrent = trim($rowParce->find('.time',0)->plaintext);
$currencyCurrent = trim($rowParce->find('.currency',0)->plaintext);
$impactCurrent = trim($rowParce->find('.impact',0)->plaintext);
$eventCurrent = trim($rowParce->find('.event',0)->plaintext);
$actualCurrent = trim($rowParce->find('.actual',0)->plaintext);
$forecastCurrent = trim($rowParce->find('.forecast',0)->plaintext);
$previousCurrent = trim($rowParce->find('.previous',0)->plaintext);
if(empty($dateCurrent))
{
$csv_text .= $dateOld.';';
}
else
{
$dateOld = date("d.m.Y", strtotime(preg_replace('#<[^>]+>#', ' ', $dateCurrent)));
$csv_text .= $dateOld.';';
}
if(trim($rowParce->find('.impact span',0)->class) == 'low')
{
$impactCurrent = 1; // yellow
}
elseif(trim($rowParce->find('.impact span',0)->class) == 'medium')
{
$impactCurrent = 2; // orange
}
elseif(trim($rowParce->find('.impact span',0)->class) == 'high')
{
$impactCurrent = 3; // red
}
$findSign = ':';
$_ifTime = strpos($timeCurrent, $findSign);
if ($_ifTime === false)
{
$csv_text .= '00:00;';
}
else
{
$csv_text .= date("H:i", strtotime($timeCurrent)).';';
}
$csv_text .= $currencyCurrent.';';
$csv_text .= $impactCurrent.';';
$csv_text .= $eventCurrent.';';
$csv_text .= $actualCurrent.';';
$csv_text .= $forecastCurrent.';';
$csv_text .= $previousCurrent."\r\n";
}
return $csv_text;
}
$csv_content = parcePage($contentParce);
if(findDataInFile($contentParce))
{
// echo $position = -abs(findPositionInFile($contentParce));
$position = findPositionInFile($contentParce);
$file = fopen(PATH_TO_FOLDER . CSV_FILE_NAME, "r+");
ftruncate($file, $position);
fseek($file, $position, SEEK_SET);
fwrite($file,trim($csv_content)."\n");
fclose($file);
}
else
{
$file = fopen(PATH_TO_FOLDER . CSV_FILE_NAME, "a+");
fwrite($file,trim($csv_content)."\n");
fclose($file);
}
$html->clear();
unset($html);
@AlexMcowkin
Copy link
Author

OpenServer cron
%progdir%\modules\wget\bin\wget.exe -q --no-cache http://forexfactory.loc/forexfactory.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment