Browse Source

🎉 1er essai

master
Shikiryu 4 years ago
commit
a9f48effb5
  1. 4
      .gitignore
  2. 20
      composer.json
  3. 1870
      composer.lock
  4. 75
      cron.php

4
.gitignore

@ -0,0 +1,4 @@
.idea
logs/
deals/
vendor/

20
composer.json

@ -0,0 +1,20 @@
{
"name": "shikiryu/lbcreposter-cli",
"description": "Manage deals in LBC",
"type": "project",
"require": {
"shikiryu/lbcreposter-api": "0.*"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"authors": [
{
"name": "Shikiryu",
"email": "clement@desmidt.fr"
}
],
"autoload": {
"psr-4": {}
}
}

1870
composer.lock
File diff suppressed because it is too large
View File

75
cron.php

@ -0,0 +1,75 @@
<?php
include __DIR__ . '/vendor/autoload.php';
define('APP_DIR', __DIR__);
define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
define('LOGS_DIR', sprintf('%s/logs', APP_DIR));
define('NEW_DEALS_DIR', sprintf('%s/deals/new', APP_DIR));
define('EXISTING_DEALS_DIR', sprintf('%s/deals/list', APP_DIR));
define('BACKUP_DEALS_DIR', sprintf('%s/deals/backup', APP_DIR));
$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');
}
}
echo sprintf('%u annonces locales, normalement en ligne.%s', count($existing_deals), "\n");
$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))) {
echo 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())) {
echo sprintf('elle est vieille (%s), on la supprime. %s', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i'), "\n");
// $actions->delete($deal);
rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
// rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), BACKUP_DEALS_DIR.'/'.$deal->getId());
} else {
// sinon on la laisse
echo sprintf('et tout va bien. %s', "\n");
}
unset($deals_to_backup[$deal->getId()]);
} else {
echo sprintf('L\'annonce %s n\'existait pas et est sauvegardée. %s', $deal->getSubject(), "\n");
// Une annonce non sauvegardée ? on la sauve
$deal->save(EXISTING_DEALS_DIR);
}
}
// Backup
foreach ($deals_to_backup as $deal_to_backup) {
rename(EXISTING_DEALS_DIR.'/'.$deal_to_backup->getId(), BACKUP_DEALS_DIR.'/'.$deal_to_backup->getId());
}
// Creation
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
if ($new_deal->isDir() && !$new_deal->isDot()) {
$new_deal = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
if ($actions->create($new_deal)) {
echo sprintf('L\'annonce %s a bien été créée. %s', $new_deal->getSubject(), "\n");
break;
} else {
echo sprintf('L\'annonce %s n\'a pas bien été créée. %s', $new_deal->getSubject(), "\n");
}
sleep(30);
}
}
} else {
echo 'not connected';
}
Loading…
Cancel
Save