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

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
/composer.lock
/vendor/
/.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';
$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);
}

View File

@ -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;
}
}