Sauvegarde les annonces

This commit is contained in:
Shikiryu 2016-08-24 22:30:13 +02:00
parent 3afe0e7aca
commit b6b2480865
4 changed files with 63 additions and 4 deletions

4
.gitignore vendored
View File

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

View File

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

View File

@ -10,12 +10,21 @@ $client->setClient(
]) ])
); );
$login = 'xxxx'; define('APP_DIR', dirname(__FILE__));
$password = 'xxxx'; 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); $account = new \Shikiryu\LBCReposter\Account($client, $login, $password);
if ($account->connect()) { if ($account->connect()) {
// existing deals // existing deals
$deals = $account->getDeals(); $deals = $account->getDeals();
/** @var \Shikiryu\LBCReposter\Deal $deal */
foreach ($deals as $deal) {
$deal->save(DEALS_DIR);
}
var_dump($deals); var_dump($deals);
} }

View File

@ -72,7 +72,7 @@ class Deal
]; ];
/** @var Account */ /** @var Account */
protected $account; private $account;
/** @var int */ /** @var int */
protected $id; protected $id;
/** @var int */ /** @var int */
@ -302,4 +302,49 @@ class Deal
$this->account = $account; $this->account = $account;
return $this; 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;
}
} }