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]
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 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();
}
}
}

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;
}
/**
* @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;
}
}