Zefix_search/taskExecuter.php

119 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2023-09-25 12:43:51 +02:00
<?php
2023-09-25 15:45:26 +02:00
include "zefixAPI.php";
2023-09-25 23:11:53 +02:00
include "read_write_xlsx.php";
2023-09-25 12:43:51 +02:00
2023-09-26 13:26:19 +02:00
include "emailSender.php";
2023-09-25 23:49:38 +02:00
$maxExecutionTime = 120;
2023-09-25 13:49:34 +02:00
$taskDir = 'tasks';
2023-09-25 15:45:26 +02:00
$downloadDir = 'download';
2023-09-26 15:24:18 +02:00
$minTaskOldness = 10;
2023-09-25 14:29:53 +02:00
$latesEndTime = time() + $maxExecutionTime;
2023-09-25 23:49:38 +02:00
$smtppassword = getenv("smtppassword");
2023-09-25 15:45:26 +02:00
if(!is_dir($downloadDir)){
mkdir($downloadDir, 0755, true);
}
function doRequest($data, $filename, $username, $password)
2023-09-25 13:49:34 +02:00
{
2023-09-25 15:45:26 +02:00
$response = sendAPICompanySearchRequest($username, $password, $data);
$responseObject = json_decode($response, true);
if(array_key_exists("error", $responseObject)) {
return false;
}
$companyArray = $responseObject['list'];
2023-09-25 15:45:26 +02:00
$companyData = array();
foreach ($companyArray as $company) {
2023-09-26 14:25:43 +02:00
$companyFullData = json_decode(sendAPICompanyInfoRequest($username, $password, $company['uid']), true)[0];
$companyData[] = [$companyFullData['name'],$companyFullData['address']['street'], $companyFullData['address']['houseNumber'], $companyFullData['address']['swissZipCode'], $companyFullData['address']['city'],$companyFullData['uid'],$companyFullData['legalSeat'],$companyFullData['legalForm']['name']['de'],$companyFullData['status'],$companyFullData['sogcDate'],$companyFullData['deletionDate']];
2023-09-25 15:45:26 +02:00
}
if (! file_exists($filename)) {
// If File doesn't exist yet
$file = fopen($filename, 'w');
if($file) {
2023-09-26 14:25:43 +02:00
fputcsv($file, ['name', 'street', 'houseNumber', 'swissZipCode', 'city', 'uid', 'legalSeat', 'legalForm', 'status', 'sogcDate', 'deletionDate']);
2023-09-25 15:45:26 +02:00
}
} else {
// If file already exists
$file = fopen($filename, 'a');
}
if ($file) {
foreach ($companyData as $row){
fputcsv($file, $row);
}
fclose($file);
}
if($responseObject['hasMoreResults']) {
return $responseObject['maxOffset'] + 50;
} else {
return false;
}
2023-09-25 13:49:34 +02:00
}
2023-09-25 12:43:51 +02:00
2023-09-25 22:52:09 +02:00
2023-09-25 23:49:38 +02:00
2023-09-25 14:29:53 +02:00
// Stop executing bevore reaching PHP Max Execution Time
2023-09-25 20:51:32 +02:00
function convertcsvToXlsx(string $csvFile, string $xlsxFile)
2023-09-25 20:18:24 +02:00
{
2023-09-25 22:22:50 +02:00
$csvData = [];
2023-09-25 20:18:24 +02:00
2023-09-25 22:22:50 +02:00
if (($handle = fopen($csvFile, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$csvData[] = $data;
2023-09-25 20:51:32 +02:00
}
2023-09-25 22:22:50 +02:00
fclose($handle);
} else {
echo "Failed to open $csvFile for reading.";
2023-09-25 20:51:32 +02:00
}
2023-09-25 22:22:50 +02:00
write_xlsxFile($xlsxFile, $csvData);
2023-09-25 20:18:24 +02:00
}
while($latesEndTime - time() > 30){
2023-09-25 14:29:53 +02:00
// If the directory for Tasks is already created
if(is_dir($taskDir)) {
$taskfiles = scandir($taskDir);
$taskfiles = array_diff($taskfiles, array('.', '..'));
sort($taskfiles);
2023-09-25 22:52:09 +02:00
// if there are any taks which are older then min oldness. To be sure the file writing process is finished.
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + $minTaskOldness < time()) {
2023-09-25 14:29:53 +02:00
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
$task = json_decode($taskString, true);
2023-09-25 13:49:34 +02:00
2023-09-25 14:29:53 +02:00
// if there are any requests to do, do the first
if(count($task['requests']) > 0) {
$nextOffset = doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password);
if($nextOffset){
$task['requests'][0]['offset'] = $nextOffset;
} else {
array_shift($task['requests']);
}
2023-09-25 14:29:53 +02:00
$taskString = json_encode($task);
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
if($taskfile){
fwrite($taskfile, $taskString);
fclose($taskfile);
}
2023-09-25 13:49:34 +02:00
}
2023-09-25 14:29:53 +02:00
// if there are no Requests left, send the E-Mail
2023-09-25 13:49:34 +02:00
else {
2023-09-25 20:16:47 +02:00
convertcsvToXlsx(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
2023-09-25 22:52:09 +02:00
unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
2023-09-25 14:29:53 +02:00
unlink($taskDir.'/'.$taskfiles[0]);
echo "Task File deleted";
2023-09-25 15:45:26 +02:00
echo "<br>";
2023-09-26 00:13:28 +02:00
sendEmail($task['email'], str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]), $smtppassword);
2023-09-25 14:29:53 +02:00
}
} else {
// acutal Time - start time
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
echo "sleeping 10 seconds";
2023-09-25 15:45:26 +02:00
echo "<br>";
2023-09-25 14:29:53 +02:00
sleep(10);
2023-09-25 13:49:34 +02:00
}
}
}
?>