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.

111 lines
4.4KB

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