diff --git a/library/Account.php b/library/Account.php index e14ae0f..ea6bc46 100644 --- a/library/Account.php +++ b/library/Account.php @@ -9,6 +9,7 @@ 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'; /** @var Client */ protected $client; @@ -40,6 +41,12 @@ class Account return $this->client; } + /** + * Check if it's connected + * (if there's a "logout" link) + * + * @return bool + */ public function isConnected() { if (!$this->is_connected) { @@ -49,6 +56,13 @@ class Account return $this->is_connected; } + /** + * Check if connected, if not, try to connect you + * + * @see this#isConnected + * + * @return bool + */ public function connect() { if (!$this->isConnected()) { @@ -60,9 +74,14 @@ class Account return $this->is_connected; } + /** + * Retrieve all deals from the current account + * + * @return Deals + */ public function getDeals() { - if ($this->isConnected()) { + if ($this->connect()) { $crawler = $this->client->request('GET', self::ACCOUNT_URL); $deals = $crawler->filter('#dashboard .list .element')->each( function ($node) { @@ -77,4 +96,23 @@ class Account } return new Deals($this, []); } + + 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); + } } \ No newline at end of file diff --git a/library/Deals.php b/library/Deals.php index 55b2fac..5e61930 100644 --- a/library/Deals.php +++ b/library/Deals.php @@ -19,6 +19,12 @@ class Deals extends \ArrayObject return $deal; } + /** + * Deals constructor. + * + * @param Account $account + * @param array|Deal $input + */ public function __construct(Account $account, $input) { $this->account = $account;