Ajoute la configuration supplementaire pour la creation

-> Meme avec ceci, cela ne fonctionne toujours pas.
 -> Soit il manque tous les parametres, soit c'est le parametre en GET.
This commit is contained in:
Shikiryu 2016-09-11 22:53:40 +02:00
parent f6f4bab169
commit fa069edee8
4 changed files with 126 additions and 27 deletions

View File

@ -1,3 +1,12 @@
[CREDENTIALS] [CREDENTIALS]
login=xxxxx login=xxxxx
password=xxxxx password=xxxxx
[ACCOUNT]
region=xx
department=xx
city=xxxxx
postal_code=xxxxx
address=xxxxxxxxxx
name=xxxx
phone=xxxxxxxxxx

View File

@ -9,28 +9,24 @@ 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'; const ADD_URL = 'https://www.leboncoin.fr/ai/form/0';
/** @var Client */ /** @var Client */
protected $client; protected $client;
/** @var string */ /** @var Config */
protected $login; protected $config;
/** @var string */
protected $password;
/** @var bool */ /** @var bool */
protected $is_connected = false; protected $is_connected = false;
/** /**
* Account constructor. * Account constructor.
* @param Client $client * @param Client $client
* @param string $login * @param Config $config
* @param string $password
*/ */
public function __construct(Client $client, $login, $password) public function __construct(Client $client, Config $config)
{ {
$this->client = $client; $this->client = $client;
$this->login = $login; $this->config = $config;
$this->password = $password;
} }
/** /**
@ -68,7 +64,7 @@ class Account
if (!$this->isConnected()) { if (!$this->isConnected()) {
$crawler = $this->client->request('GET', self::LOGIN_URL); $crawler = $this->client->request('GET', self::LOGIN_URL);
$form = $crawler->selectButton('Se connecter')->form(); $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; $this->is_connected = $crawler->filter('.account_userinfo')->count() > 0;
} }
return $this->is_connected; return $this->is_connected;
@ -99,20 +95,54 @@ class Account
public function addDeal(Deal $deal) public function addDeal(Deal $deal)
{ {
$crawler = $this->client->request('GET', self::ADD_URL); try {
$form = $crawler->selectButton('Valider')->form(); $t = date('YmdHis');
$image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'rb'); $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
// TODO manage location mkdir($log_folder);
$fields = [ $crawler = $this->client->request('GET', self::ADD_URL);
'category' => $deal->getCategory(), file_put_contents(sprintf('%s/add.html', $log_folder), $crawler->html());
'type' => $deal->getType(), $form = $crawler->selectButton('Valider')->form();
'subject' => $deal->getSubject(), $image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r');
'body' => $deal->getBody(), $fields = [
'price' => $deal->getPrice(), 'check_type_diff' => 0,
'image0' => $image0, 'company_ad' => 0,
'no_salesmen' => 1, // TODO what are those ?
'phone_hidden' => 0, 'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code),
]; 'zipcode' => $this->config->postal_code,
$crawler = $this->client->request('POST', $form->getUri(), ['Content-Type => multipart/form-data'], [], [], $fields); '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();
}
} }
} }

44
library/Config.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace Shikiryu\LBCReposter;
class Config
{
protected $data = [];
/**
* Config constructor.
* @param array $data
*/
public function __construct(array $data = [])
{
$this->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;
}
}
}
}

View File

@ -347,4 +347,20 @@ class Deal
} }
return true; 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;
}
} }