Tente la creation de deal

mais il manque la localisation du compte !
Quelques commentaires.
This commit is contained in:
Shikiryu 2016-08-24 23:37:29 +02:00
parent 920f072fa2
commit f6f4bab169
2 changed files with 45 additions and 1 deletions

View File

@ -9,6 +9,7 @@ class Account
const HOME_URL = 'https://www.leboncoin.fr/'; const HOME_URL = 'https://www.leboncoin.fr/';
const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html'; const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html';
const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html'; const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html';
const ADD_URL = 'https://www2.leboncoin.fr/ai/form/0';
/** @var Client */ /** @var Client */
protected $client; protected $client;
@ -40,6 +41,12 @@ class Account
return $this->client; return $this->client;
} }
/**
* Check if it's connected
* (if there's a "logout" link)
*
* @return bool
*/
public function isConnected() public function isConnected()
{ {
if (!$this->is_connected) { if (!$this->is_connected) {
@ -49,6 +56,13 @@ class Account
return $this->is_connected; return $this->is_connected;
} }
/**
* Check if connected, if not, try to connect you
*
* @see this#isConnected
*
* @return bool
*/
public function connect() public function connect()
{ {
if (!$this->isConnected()) { if (!$this->isConnected()) {
@ -60,9 +74,14 @@ class Account
return $this->is_connected; return $this->is_connected;
} }
/**
* Retrieve all deals from the current account
*
* @return Deals
*/
public function getDeals() public function getDeals()
{ {
if ($this->isConnected()) { if ($this->connect()) {
$crawler = $this->client->request('GET', self::ACCOUNT_URL); $crawler = $this->client->request('GET', self::ACCOUNT_URL);
$deals = $crawler->filter('#dashboard .list .element')->each( $deals = $crawler->filter('#dashboard .list .element')->each(
function ($node) { function ($node) {
@ -77,4 +96,23 @@ class Account
} }
return new Deals($this, []); 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);
}
} }

View File

@ -19,6 +19,12 @@ class Deals extends \ArrayObject
return $deal; return $deal;
} }
/**
* Deals constructor.
*
* @param Account $account
* @param array|Deal $input
*/
public function __construct(Account $account, $input) public function __construct(Account $account, $input)
{ {
$this->account = $account; $this->account = $account;