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.

119 lines
4.8 KiB

  1. <?php
  2. include __DIR__ . '/vendor/autoload.php';
  3. use Monolog\Logger;
  4. use Monolog\Handler\StreamHandler;
  5. define('APP_DIR', __DIR__);
  6. define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
  7. define('LOGS_DIR', sprintf('%s/logs', APP_DIR));
  8. define('NEW_DEALS_DIR', sprintf('%s/deals/new', APP_DIR));
  9. define('EXISTING_DEALS_DIR', sprintf('%s/deals/list', APP_DIR));
  10. define('BACKUP_DEALS_DIR', sprintf('%s/deals/backup', APP_DIR));
  11. define('LOG_MAIL_TO', '');
  12. define('LOG_MAIL_SUBJECT', '[LBCReposter] erreur cron');
  13. define('LOG_MAIL_FROM', '');
  14. $log = new Logger('lbcreposter');
  15. $log->pushHandler(new StreamHandler(LOGS_DIR.'/cron.log', Logger::WARNING));
  16. $log->pushHandler(new \Monolog\Handler\NativeMailerHandler(LOG_MAIL_TO, LOG_MAIL_SUBJECT, LOG_MAIL_FROM));
  17. $existing_deals = [];
  18. foreach (new DirectoryIterator(EXISTING_DEALS_DIR) as $existing_deal) {
  19. if ($existing_deal->isDir() && !$existing_deal->isDot()) {
  20. $existing_deals[$existing_deal->getFilename()] = \Shikiryu\LBCReposter\Deal::fromJSON($existing_deal->getRealPath() . '/data.json');
  21. }
  22. }
  23. function copy_folder($from, $to)
  24. {
  25. $directory = new RecursiveDirectoryIterator($from);
  26. foreach (new RecursiveIteratorIterator($directory) as $filename => $current) {
  27. $source = $current->getPathName();
  28. $destination = $to . '/' . $current->getFileName();
  29. copy($source, $destination);
  30. }
  31. }
  32. function delete_folder($folder) {
  33. $files = array_diff(scandir($folder), ['.','..']);
  34. foreach ($files as $file) {
  35. (is_dir(sprintf('%s/%s', $folder, $file))) ? delete_folder(sprintf('%s/%s', $folder, $file)) : unlink(sprintf('%s/%s', $folder, $file));
  36. }
  37. return rmdir($folder);
  38. }
  39. $log->info(sprintf('%u annonces locales, normalement en ligne.', count($existing_deals)));
  40. $script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true);
  41. $config = new \Shikiryu\LBCReposter\Config($script_params);
  42. $account = new \Shikiryu\LBCReposter\Account($config);
  43. $actions = new \Shikiryu\LBCReposter\Actions($account);
  44. $actions->setDebug(LOGS_DIR);
  45. $deals_to_backup = $existing_deals;
  46. if ($actions->connect()) {
  47. // existing deals
  48. $deals = $actions->retrieve();
  49. // Synchro
  50. /** @var \Shikiryu\LBCReposter\Deal $deal */
  51. foreach ($deals as $i => $deal) {
  52. // l'annonce existe déjà
  53. if (in_array($deal->getId(), array_keys($existing_deals))) {
  54. $log->info(sprintf('L\'annonce %s existe déjà, ', $deal->getId()));
  55. // si elle est vieille, on supprime et on recréé
  56. if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) <= (new DateTime())) {
  57. // Suppression
  58. $log->error(sprintf('elle est vieille (%s), on la supprime.', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i')));
  59. // $actions->delete($deal);
  60. // Backup
  61. if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal->getId())) {
  62. mkdir(BACKUP_DEALS_DIR . '/' . $deal->getId());
  63. copy_folder(EXISTING_DEALS_DIR . '/' . $deal->getId(), BACKUP_DEALS_DIR . '/' . $deal->getId());
  64. }
  65. // Placement en "à créer"
  66. rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
  67. } else {
  68. // sinon on la laisse
  69. $log->info('et tout va bien.');
  70. }
  71. unset($deals_to_backup[$deal->getId()]);
  72. } else {
  73. $log->error(sprintf('L\'annonce %s n\'existait pas et est sauvegardée.', $deal->getSubject()));
  74. // Une annonce non sauvegardée ? on la sauve
  75. $deal->save(EXISTING_DEALS_DIR);
  76. }
  77. }
  78. // Backup
  79. foreach ($deals_to_backup as $deal_to_backup) {
  80. if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal_to_backup->getId())) {
  81. rename(EXISTING_DEALS_DIR . '/' . $deal_to_backup->getId(), BACKUP_DEALS_DIR . '/' . $deal_to_backup->getId());
  82. } else {
  83. delete_folder(EXISTING_DEALS_DIR . '/' . $deal_to_backup->getId());
  84. }
  85. }
  86. // Creation
  87. foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
  88. if ($new_deal->isDir() && !$new_deal->isDot()) {
  89. $deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
  90. /*if ($actions->create($deal_to_create)) {
  91. delete_folder($new_deal->getRealPath());
  92. $log->info(sprintf('L\'annonce %s a bien été créée.', $deal_to_create->getSubject()));
  93. break;
  94. } else {
  95. $log->error(sprintf('L\'annonce %s n\'a pas bien été créée. %s', $deal_to_create->getSubject()));
  96. }*/
  97. sleep(30);
  98. }
  99. }
  100. } else {
  101. $log->error('not connected');
  102. }
  103. $log->info('CRON terminé !');