Browse Source

Sauvegarde les annonces

api
Shikiryu 5 years ago
parent
commit
b6b2480865
  1. 4
      .gitignore
  2. 3
      deals/lbcreposter.ini.dev
  3. 13
      lbcreposter.php
  4. 47
      library/Deal.php

4
.gitignore

@ -2,4 +2,6 @@
/index.php
/composer.lock
/vendor/
/.idea/
/.idea/
/deals/lbcreposter.ini
/deals/*/

3
deals/lbcreposter.ini.dev

@ -0,0 +1,3 @@
[CREDENTIALS]
login=xxxxx
password=xxxxx

13
lbcreposter.php

@ -10,12 +10,21 @@ $client->setClient(
])
);
$login = 'xxxx';
$password = 'xxxx';
define('APP_DIR', dirname(__FILE__));
define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini.dev', DEALS_DIR), true);
$credentials = $script_params['CREDENTIALS'];
$login = $credentials['login'];
$password = $credentials['password'];
$account = new \Shikiryu\LBCReposter\Account($client, $login, $password);
if ($account->connect()) {
// existing deals
$deals = $account->getDeals();
/** @var \Shikiryu\LBCReposter\Deal $deal */
foreach ($deals as $deal) {
$deal->save(DEALS_DIR);
}
var_dump($deals);
}

47
library/Deal.php

@ -72,7 +72,7 @@ class Deal
];
/** @var Account */
protected $account;
private $account;
/** @var int */
protected $id;
/** @var int */
@ -302,4 +302,49 @@ class Deal
$this->account = $account;
return $this;
}
private function toJSON()
{
$reflection = new \ReflectionClass($this);
$props = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
$json = [];
foreach ($props as $prop) {
$method = sprintf('get%s', ucfirst($prop->getName()));
$json[$prop->getName()] = $this->$method();
}
return \json_encode($json);
}
/**
* @param string $dir
*
* @return bool
*/
public function save($dir)
{
try {
$save_dir = sprintf('%s/%s', $dir, $this->id);
if (!is_dir($save_dir)) {
mkdir($save_dir);
}
file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON());
foreach (range(0,2) as $i) {
$image = sprintf('image%u',$i);
if (empty($this->$image)) {
break;
}
$ch = curl_init($this->$image);
$fp = fopen(sprintf('%s/%s.jpg', $save_dir, $image), 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
} catch(\Exception $e) {
return false;
}
return true;
}
}
Loading…
Cancel
Save