diff --git a/.gitignore b/.gitignore index f34971a..6600fc5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ /index.php /composer.lock /vendor/ -/.idea/ \ No newline at end of file +/.idea/ +/deals/lbcreposter.ini +/deals/*/ \ No newline at end of file diff --git a/deals/lbcreposter.ini.dev b/deals/lbcreposter.ini.dev new file mode 100644 index 0000000..898104f --- /dev/null +++ b/deals/lbcreposter.ini.dev @@ -0,0 +1,3 @@ +[CREDENTIALS] +login=xxxxx +password=xxxxx \ No newline at end of file diff --git a/lbcreposter.php b/lbcreposter.php index 7144656..b6b5241 100644 --- a/lbcreposter.php +++ b/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); } \ No newline at end of file diff --git a/library/Deal.php b/library/Deal.php index e4d2628..cf698a6 100644 --- a/library/Deal.php +++ b/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; + } } \ No newline at end of file