2017-11-06 23:35:49 +01:00
|
|
|
<?php
|
|
|
|
include __DIR__ . '/vendor/autoload.php';
|
2017-11-16 22:25:31 +01:00
|
|
|
include __DIR__ . '/define.php';
|
2017-11-06 23:35:49 +01:00
|
|
|
|
2017-11-15 22:59:41 +01:00
|
|
|
use Monolog\Logger;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
|
|
|
|
$log = new Logger('lbcreposter');
|
|
|
|
$log->pushHandler(new StreamHandler(LOGS_DIR.'/cron.log', Logger::WARNING));
|
|
|
|
$log->pushHandler(new \Monolog\Handler\NativeMailerHandler(LOG_MAIL_TO, LOG_MAIL_SUBJECT, LOG_MAIL_FROM));
|
2017-11-06 23:35:49 +01:00
|
|
|
|
|
|
|
$existing_deals = [];
|
|
|
|
foreach (new DirectoryIterator(EXISTING_DEALS_DIR) as $existing_deal) {
|
|
|
|
if ($existing_deal->isDir() && !$existing_deal->isDot()) {
|
|
|
|
$existing_deals[$existing_deal->getFilename()] = \Shikiryu\LBCReposter\Deal::fromJSON($existing_deal->getRealPath() . '/data.json');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 21:11:26 +01:00
|
|
|
function copy_folder($from, $to)
|
|
|
|
{
|
|
|
|
$directory = new RecursiveDirectoryIterator($from);
|
|
|
|
|
|
|
|
foreach (new RecursiveIteratorIterator($directory) as $filename => $current) {
|
|
|
|
|
|
|
|
$source = $current->getPathName();
|
2017-11-08 23:17:18 +01:00
|
|
|
$destination = $to . '/' . $current->getFileName();
|
2017-11-07 21:11:26 +01:00
|
|
|
|
|
|
|
copy($source, $destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 20:31:44 +01:00
|
|
|
function delete_folder($folder) {
|
|
|
|
$files = array_diff(scandir($folder), ['.','..']);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
(is_dir(sprintf('%s/%s', $folder, $file))) ? delete_folder(sprintf('%s/%s', $folder, $file)) : unlink(sprintf('%s/%s', $folder, $file));
|
|
|
|
}
|
|
|
|
return rmdir($folder);
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->info(sprintf('%u annonces locales, normalement en ligne.', count($existing_deals)));
|
2017-11-06 23:35:49 +01:00
|
|
|
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true);
|
|
|
|
$config = new \Shikiryu\LBCReposter\Config($script_params);
|
|
|
|
$account = new \Shikiryu\LBCReposter\Account($config);
|
|
|
|
$actions = new \Shikiryu\LBCReposter\Actions($account);
|
|
|
|
$actions->setDebug(LOGS_DIR);
|
2017-11-08 20:31:44 +01:00
|
|
|
$deals_to_backup = $existing_deals;
|
2017-11-06 23:35:49 +01:00
|
|
|
|
|
|
|
if ($actions->connect()) {
|
|
|
|
// existing deals
|
|
|
|
$deals = $actions->retrieve();
|
|
|
|
|
|
|
|
// Synchro
|
|
|
|
/** @var \Shikiryu\LBCReposter\Deal $deal */
|
|
|
|
foreach ($deals as $i => $deal) {
|
|
|
|
// l'annonce existe déjà
|
|
|
|
if (in_array($deal->getId(), array_keys($existing_deals))) {
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->info(sprintf('L\'annonce %s existe déjà, ', $deal->getId()));
|
2017-11-06 23:35:49 +01:00
|
|
|
// si elle est vieille, on supprime et on recréé
|
2017-11-26 23:06:09 +01:00
|
|
|
if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) >= (new DateTime())) {
|
2017-11-07 21:11:26 +01:00
|
|
|
// Suppression
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->error(sprintf('elle est vieille (%s), on la supprime.', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i')));
|
2017-11-16 22:07:53 +01:00
|
|
|
$actions->delete($deal);
|
2017-11-07 21:11:26 +01:00
|
|
|
// Backup
|
2017-11-07 21:13:44 +01:00
|
|
|
if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal->getId())) {
|
|
|
|
mkdir(BACKUP_DEALS_DIR . '/' . $deal->getId());
|
|
|
|
copy_folder(EXISTING_DEALS_DIR . '/' . $deal->getId(), BACKUP_DEALS_DIR . '/' . $deal->getId());
|
|
|
|
}
|
2017-11-07 21:11:26 +01:00
|
|
|
// Placement en "à créer"
|
2017-11-06 23:35:49 +01:00
|
|
|
rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
|
|
|
|
} else {
|
|
|
|
// sinon on la laisse
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->info('et tout va bien.');
|
2017-11-06 23:35:49 +01:00
|
|
|
}
|
2017-11-08 20:31:44 +01:00
|
|
|
unset($deals_to_backup[$deal->getId()]);
|
2017-11-06 23:35:49 +01:00
|
|
|
} else {
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->error(sprintf('L\'annonce %s n\'existait pas et est sauvegardée.', $deal->getSubject()));
|
2017-11-06 23:35:49 +01:00
|
|
|
// Une annonce non sauvegardée ? on la sauve
|
|
|
|
$deal->save(EXISTING_DEALS_DIR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 20:31:44 +01:00
|
|
|
// Backup
|
|
|
|
foreach ($deals_to_backup as $deal_to_backup) {
|
2017-11-08 20:39:39 +01:00
|
|
|
if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal_to_backup->getId())) {
|
|
|
|
rename(EXISTING_DEALS_DIR . '/' . $deal_to_backup->getId(), BACKUP_DEALS_DIR . '/' . $deal_to_backup->getId());
|
2017-11-08 22:17:51 +01:00
|
|
|
} else {
|
|
|
|
delete_folder(EXISTING_DEALS_DIR . '/' . $deal_to_backup->getId());
|
2017-11-08 20:39:39 +01:00
|
|
|
}
|
2017-11-08 20:31:44 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 23:35:49 +01:00
|
|
|
// Creation
|
|
|
|
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
|
|
|
|
if ($new_deal->isDir() && !$new_deal->isDot()) {
|
2017-11-08 20:31:44 +01:00
|
|
|
$deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
|
2017-11-16 22:07:53 +01:00
|
|
|
if ($actions->create($deal_to_create)) {
|
2017-11-08 20:31:44 +01:00
|
|
|
delete_folder($new_deal->getRealPath());
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->info(sprintf('L\'annonce %s a bien été créée.', $deal_to_create->getSubject()));
|
2017-11-06 23:35:49 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2017-12-08 10:42:37 +01:00
|
|
|
$log->error(sprintf('L\'annonce %s n\'a pas bien été créée. %s',
|
|
|
|
$deal_to_create->getId(), $deal_to_create->getSubject()));
|
2017-11-16 22:07:53 +01:00
|
|
|
}
|
2017-11-06 23:35:49 +01:00
|
|
|
sleep(30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->error('not connected');
|
2017-11-06 23:35:49 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 22:59:41 +01:00
|
|
|
$log->info('CRON terminé !');
|