You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.4 KiB

<?php
include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/define.php';
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));
$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');
}
}
function copy_folder($from, $to)
{
$directory = new RecursiveDirectoryIterator($from);
foreach (new RecursiveIteratorIterator($directory) as $filename => $current) {
$source = $current->getPathName();
$destination = $to . '/' . $current->getFileName();
copy($source, $destination);
}
}
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);
}
$log->info(sprintf('%u annonces locales, normalement en ligne.', count($existing_deals)));
$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);
$deals_to_backup = $existing_deals;
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))) {
$log->info(sprintf('L\'annonce %s existe déjà, ', $deal->getId()));
// si elle est vieille, on supprime et on recréé
if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) >= (new DateTime())) {
// Suppression
$log->error(sprintf('elle est vieille (%s), on la supprime.', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i')));
$actions->delete($deal);
// Backup
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());
}
// Placement en "à créer"
rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
} else {
// sinon on la laisse
$log->info('et tout va bien.');
}
unset($deals_to_backup[$deal->getId()]);
} else {
$log->error(sprintf('L\'annonce %s n\'existait pas et est sauvegardée.', $deal->getSubject()));
// Une annonce non sauvegardée ? on la sauve
$deal->save(EXISTING_DEALS_DIR);
}
}
// Backup
foreach ($deals_to_backup as $deal_to_backup) {
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());
} else {
delete_folder(EXISTING_DEALS_DIR . '/' . $deal_to_backup->getId());
}
}
// Creation
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
if ($new_deal->isDir() && !$new_deal->isDot()) {
$deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
if ($actions->create($deal_to_create)) {
delete_folder($new_deal->getRealPath());
$log->info(sprintf('L\'annonce %s a bien été créée.', $deal_to_create->getSubject()));
break;
} else {
$log->error(sprintf('L\'annonce %s n\'a pas bien été créée. %s',
$deal_to_create->getId(), $deal_to_create->getSubject()));
}
sleep(30);
}
}
} else {
$log->error('not connected');
}
$log->info('CRON terminé !');