Skip to content

Commit

Permalink
debug messages added and option to turn debugging directly
Browse files Browse the repository at this point in the history
  • Loading branch information
nekromoff committed Feb 12, 2016
1 parent fb1833d commit 29af415
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions helpers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ function getClient()

}

function debug($message)
{
if (DEBUG==1) echo $message,'<br />'; flush();
}

?>
26 changes: 21 additions & 5 deletions scrape.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</head>
<body>
<?php
//define('DEBUG',1);
ini_set('max_execution_time', 300);
require('config.php');
require('simple_html_dom.php');
Expand Down Expand Up @@ -76,6 +77,7 @@
$projects[$i]["institution"]=trim($htmlchild->find('.table-list li',2)->getElementByTagName('span')->plaintext);
$projects[$i]["proponent"]=trim($htmlchild->find('.table-list li',3)->getElementByTagName('span')->plaintext);
$projects[$i]["proponentidnumber"]=trim($htmlchild->find('.table-list li',4)->getElementByTagName('span')->plaintext);
debug($projects[$i]["name"]);
// if screenshot API key exists, make lots of additional effort
if ($config["screenshot-apikey"])
{
Expand All @@ -85,6 +87,7 @@
if (strpos($text,'Oznámenie o predložení zámeru:')!==FALSE)
{
$infofile=$text->parent->parent->find('li',0)->find('a',0)->href;
debug('zamer');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, 'https://www.enviroportal.sk'.$infofile);
Expand All @@ -93,6 +96,7 @@
curl_setopt($ch, CURLOPT_NOBODY, 1);
$content=curl_exec ($ch);
$contenttype=curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
debug($contenttype.' detected ');
if ($contenttype=='application/rtf')
{
$rtfcontent=file_get_contents('https://www.enviroportal.sk'.$infofile);
Expand All @@ -114,6 +118,7 @@
// basic info RTF exists, extract exact location and pull a screenshot
if ($rtfcontent)
{
debug('in rtf');
$content=rtf2text($rtfcontent);
$content=str_replace('?','',$content);
$content=preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $content);
Expand Down Expand Up @@ -176,6 +181,7 @@
}
}
}
debug($location);
/*
if (!$long AND !$lat) echo('location not identified');
echo $location;
Expand Down Expand Up @@ -206,6 +212,8 @@
$mail->Body="";
$existingprojectstate=0;

debug('processing finished; starting mailing');

foreach($projects as $project)
{
$mail->Subject = $config["subject"];
Expand All @@ -214,6 +222,7 @@
// arbitraty set number of existing (3) projects between already found project in the same state
if ($hashkey AND $hashkey<count($hashes)-3) $existingprojectstate=1;
//echo $project["name"],' ',$existingproject,' ',$hashkey;
debug('project '.$hash.' (hashkey '.$haskey.') exists? '.$existingprojectstate);
if (!$hashkey OR $existingprojectstate)
{
file_put_contents('lasthash.txt',$hash."\n",FILE_APPEND);
Expand All @@ -227,6 +236,7 @@
// include screenshot in the email
if ($config["screenshot-apikey"] AND isset($project["long"]) AND isset($project["lat"]))
{
debug('getting screenshot');
$mapimage=imagecreatefrompng('http://api.screenshotmachine.com/?key='.$config["screenshot-apikey"].'&size=F&format=PNG&cacheLimit=0&timeout=1000&url=http%3A%2F%2Flabs.strava.com%2Fheatmap%2F%2315%2F'.$long.'%2F'.$lat.'%2Fblue%2Fbike');
$mapimagenew=imagecreatetruecolor(579,708);
imagecopy($mapimagenew,$mapimage,0,0,445,60,1024,708);
Expand Down Expand Up @@ -259,7 +269,7 @@
$templateProcessor->setValue('ico',$project["proponentidnumber"]);
$filename=uniqid().".docx";
$templateProcessor->saveAs($config["tempdir"].$filename);
echo 'File generated';
debug('generating document');
}
if ($config["gdrive-clientid"] AND !$existingprojectstate)
{
Expand Down Expand Up @@ -295,22 +305,28 @@
}
}
$mail->Body.="\nEdit: https://docs.google.com/document/d/".$createdfileid."/edit\n";
echo ', uploaded to GDrive';
debug('uploaded to GDrive');
}
if ($config["template"] AND !$existingprojectstate)
{
unlink($config["tempdir"].$filename);
}
if ($mail->send())
try
{
if ($mail->send())
{
debug('email '.$mail->Subject.' has been sent');
}
}
catch (phpmailerException $e)
{
echo ', email '.$mail->Subject.' has been sent';
debug('error sending mail: '.$e->errorMessage());
}
if ($config["screenshot-apikey"] AND isset($project["long"]) AND isset($project["lat"]))
{
unlink($config["tempdir"].$filename);
}
$mail->clearAttachments();
echo '<br />';
}
}

Expand Down

0 comments on commit 29af415

Please sign in to comment.