diff --git a/deals/lbcreposter.ini.dev b/deals/lbcreposter.ini.dev index 898104f..741e4cf 100644 --- a/deals/lbcreposter.ini.dev +++ b/deals/lbcreposter.ini.dev @@ -1,3 +1,12 @@ [CREDENTIALS] login=xxxxx -password=xxxxx \ No newline at end of file +password=xxxxx + +[ACCOUNT] +region=xx +department=xx +city=xxxxx +postal_code=xxxxx +address=xxxxxxxxxx +name=xxxx +phone=xxxxxxxxxx \ No newline at end of file diff --git a/library/Account.php b/library/Account.php index ea6bc46..70ffaac 100644 --- a/library/Account.php +++ b/library/Account.php @@ -9,28 +9,24 @@ class Account const HOME_URL = 'https://www.leboncoin.fr/'; const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html'; const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html'; - const ADD_URL = 'https://www2.leboncoin.fr/ai/form/0'; + const ADD_URL = 'https://www.leboncoin.fr/ai/form/0'; /** @var Client */ protected $client; - /** @var string */ - protected $login; - /** @var string */ - protected $password; + /** @var Config */ + protected $config; /** @var bool */ protected $is_connected = false; /** * Account constructor. * @param Client $client - * @param string $login - * @param string $password + * @param Config $config */ - public function __construct(Client $client, $login, $password) + public function __construct(Client $client, Config $config) { $this->client = $client; - $this->login = $login; - $this->password = $password; + $this->config = $config; } /** @@ -68,7 +64,7 @@ class Account if (!$this->isConnected()) { $crawler = $this->client->request('GET', self::LOGIN_URL); $form = $crawler->selectButton('Se connecter')->form(); - $crawler = $this->client->submit($form, ['st_username' => $this->login, 'st_passwd' => $this->password]); + $crawler = $this->client->submit($form, ['st_username' => $this->config->login, 'st_passwd' => $this->config->password]); $this->is_connected = $crawler->filter('.account_userinfo')->count() > 0; } return $this->is_connected; @@ -99,20 +95,54 @@ class Account public function addDeal(Deal $deal) { - $crawler = $this->client->request('GET', self::ADD_URL); - $form = $crawler->selectButton('Valider')->form(); - $image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'rb'); - // TODO manage location - $fields = [ - 'category' => $deal->getCategory(), - 'type' => $deal->getType(), - 'subject' => $deal->getSubject(), - 'body' => $deal->getBody(), - 'price' => $deal->getPrice(), - 'image0' => $image0, - 'no_salesmen' => 1, - 'phone_hidden' => 0, - ]; - $crawler = $this->client->request('POST', $form->getUri(), ['Content-Type => multipart/form-data'], [], [], $fields); + try { + $t = date('YmdHis'); + $log_folder = sprintf('%s/logs/%s', APP_DIR, $t); + mkdir($log_folder); + $crawler = $this->client->request('GET', self::ADD_URL); + file_put_contents(sprintf('%s/add.html', $log_folder), $crawler->html()); + $form = $crawler->selectButton('Valider')->form(); + $image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r'); + $fields = [ + 'check_type_diff' => 0, + 'company_ad' => 0, + // TODO what are those ? + 'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code), + 'zipcode' => $this->config->postal_code, + 'city' => $this->config->city, + 'region' => $this->config->region, + 'dpt_code' => $this->config->department, + 'address' => $this->config->address, + 'name' => $this->config->name, + 'email' => $this->config->login, + 'phone' => $this->config->phone, + 'category' => $deal->getCategory(), + 'type' => $deal->getType(), + 'subject' => $deal->getSubject(), + 'body' => $deal->getBody(), + 'price' => $deal->getPrice(), + 'image0' => $image0, + 'no_salesmen' => 1, + 'phone_hidden' => 0, + ]; + array_walk($fields, function (&$item, $key) { + $item = [ + 'name' => $key, + 'contents' => $item, + ]; + }); + + var_dump($form->getUri()); +// $crawler = $this->client->request('POST', $form->getUri(), ['Content-Type => multipart/form-data'], [], [], $fields); + // This one doesn't wor either -> redirect to Home + $crawler = $this->client->request('POST', $form->getUri(), ['multipart' => $fields]); + var_dump($crawler->html()); + var_dump($crawler); + file_put_contents(sprintf('%s/add-1.html', $log_folder), $crawler->html()); + $form = $crawler->selectButton('Valider mon annonce')->form(); + // TODO accept_rule checked and valid form + } catch (\Exception $e) { + echo $e->getTraceAsString(); + } } } \ No newline at end of file diff --git a/library/Config.php b/library/Config.php new file mode 100644 index 0000000..c724484 --- /dev/null +++ b/library/Config.php @@ -0,0 +1,44 @@ +parseData($data); + } + + /** + * @param string $name + * + * @return string + */ + public function __get($name) + { + return $this->data[$name]; + } + + /** + * @param array $data + */ + private function parseData(array $data) + { + foreach ($data as $name => $item) { + if (is_array($item)) { + $this->parseData($item); + } else { + $this->data[$name] = $item; + } + } + } + + +} \ No newline at end of file diff --git a/library/Deal.php b/library/Deal.php index cf698a6..8799945 100644 --- a/library/Deal.php +++ b/library/Deal.php @@ -347,4 +347,20 @@ class Deal } return true; } + + /** + * @param string $json path to json + * + * @return Deal + */ + public static function fromJSON($json) + { + $json = \json_decode(file_get_contents($json), true); + $deal = new self(); + foreach ($json as $property => $value) { + $method = sprintf('set%s', ucfirst($property)); + $deal->$method($value); + } + return $deal; + } } \ No newline at end of file