Ajoute l'envoi d'annonce (sans images)

This commit is contained in:
2017-09-06 06:29:28 +02:00
parent 4f12d3da3b
commit 0245bda600
3 changed files with 109 additions and 73 deletions

View File

@@ -47,7 +47,7 @@ class Account
{
if (!$this->is_connected) {
$crawler = $this->client->request('GET', self::HOME_URL);
$this->is_connected = $crawler->filter('.loggedOut')->count() == 0;
$this->is_connected = $crawler->filter('#account_logout')->count() == 1;
}
return $this->is_connected;
}
@@ -62,9 +62,14 @@ class Account
public function connect()
{
if (!$this->isConnected()) {
// $t = date('YmdHis');
// $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
// mkdir($log_folder);
$crawler = $this->client->request('GET', self::LOGIN_URL);
// file_put_contents(sprintf('%s/connect.html', $log_folder), $crawler->html());
$form = $crawler->selectButton('Se connecter')->form();
$crawler = $this->client->submit($form, ['st_username' => $this->config->login, 'st_passwd' => $this->config->password]);
// file_put_contents(sprintf('%s/connected.html', $log_folder), $crawler->html());
$this->is_connected = $crawler->filter('.account_userinfo')->count() > 0;
}
return $this->is_connected;
@@ -88,60 +93,58 @@ class Account
);
}
);
return new Deals($this, $deals);
$deals = array_map('current', $deals);
return (new Deals())->setAccount($this)->setDeals($deals);
}
return new Deals($this, []);
return (new Deals())->setAccount($this);
}
public function addDeal(Deal $deal)
{
try {
$t = date('YmdHis');
$log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
mkdir($log_folder);
// $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());
// 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 = $form->getPhpValues();
$fields = array_merge(
$fields,
[
'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,
'geo_source' => 'user',
'geo_provider' => 'lbc',
'latitude' => '49.0707',
'longitude' => '2.31882',
'accept_localisation' => 'on',
'check_type_diff' => '0',
'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' => 1,
]
);
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);
$uri = $form->getUri();
// It needs to be done twice
$this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
$crawler = $this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
// 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
$crawler = $this->client->submit($form, ['accept_rule' => 1]);
} catch (\Exception $e) {
echo $e->getTraceAsString();
}

View File

@@ -6,6 +6,56 @@ class Deals extends \ArrayObject
{
protected $account;
public function addDeal(Deal $deal)
{
$this->append($deal);
return $this;
}
public function addDeals(array $deals)
{
array_walk($deals, function(&$deal) {
if (is_string($deal)) {
$deal = Deal::fromURL($this->account, $deal);
}
});
foreach ($deals as $deal) {
$this->append($deal);
}
return $this;
}
public function setDeals(array $deals)
{
array_walk($deals, function(&$deal) {
if (is_string($deal)) {
$deal = Deal::fromURL($this->account, $deal);
}
});
$this->exchangeArray($deals);
return $this;
}
/**
* @param Account $account
* @return Deals
*/
public function setAccount($account)
{
$this->account = $account;
return $this;
}
public function append($value)
{
if (is_array($value)) {
parent::append($this->parseArray($value));
}
if ($value instanceof Deal) {
parent::append($value);
}
}
private function parseArray(array $array = [])
{
if (count($array) == 1) {
@@ -19,33 +69,4 @@ class Deals extends \ArrayObject
return $deal;
}
/**
* Deals constructor.
*
* @param Account $account
* @param array|Deal $input
*/
public function __construct(Account $account, $input)
{
$this->account = $account;
if (is_array($input)) {
foreach ($input as $item) {
$this->append($item);
}
}
if ($input instanceof Deal) {
$this->append($input);
}
}
public function append($value)
{
if (is_array($value)) {
parent::append($this->parseArray($value));
}
if ($value instanceof Deal) {
parent::append($value);
}
}
}