2016-08-20 23:02:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\LBCReposter;
|
|
|
|
|
|
|
|
use Goutte\Client;
|
|
|
|
|
|
|
|
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';
|
2016-09-11 22:53:40 +02:00
|
|
|
const ADD_URL = 'https://www.leboncoin.fr/ai/form/0';
|
2016-08-20 23:02:05 +02:00
|
|
|
|
|
|
|
/** @var Client */
|
|
|
|
protected $client;
|
2016-09-11 22:53:40 +02:00
|
|
|
/** @var Config */
|
|
|
|
protected $config;
|
2016-08-20 23:02:05 +02:00
|
|
|
/** @var bool */
|
|
|
|
protected $is_connected = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Account constructor.
|
|
|
|
* @param Client $client
|
2016-09-11 22:53:40 +02:00
|
|
|
* @param Config $config
|
2016-08-20 23:02:05 +02:00
|
|
|
*/
|
2016-09-11 22:53:40 +02:00
|
|
|
public function __construct(Client $client, Config $config)
|
2016-08-20 23:02:05 +02:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2016-09-11 22:53:40 +02:00
|
|
|
$this->config = $config;
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
public function getClient()
|
|
|
|
{
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:37:29 +02:00
|
|
|
/**
|
|
|
|
* Check if it's connected
|
|
|
|
* (if there's a "logout" link)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-20 23:02:05 +02:00
|
|
|
public function isConnected()
|
|
|
|
{
|
|
|
|
if (!$this->is_connected) {
|
|
|
|
$crawler = $this->client->request('GET', self::HOME_URL);
|
2017-09-06 06:29:28 +02:00
|
|
|
$this->is_connected = $crawler->filter('#account_logout')->count() == 1;
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
|
|
|
return $this->is_connected;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:37:29 +02:00
|
|
|
/**
|
|
|
|
* Check if connected, if not, try to connect you
|
|
|
|
*
|
|
|
|
* @see this#isConnected
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-20 23:02:05 +02:00
|
|
|
public function connect()
|
|
|
|
{
|
|
|
|
if (!$this->isConnected()) {
|
2017-09-06 06:29:28 +02:00
|
|
|
// $t = date('YmdHis');
|
|
|
|
// $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
|
|
|
|
// mkdir($log_folder);
|
2016-08-20 23:02:05 +02:00
|
|
|
$crawler = $this->client->request('GET', self::LOGIN_URL);
|
2017-09-06 06:29:28 +02:00
|
|
|
// file_put_contents(sprintf('%s/connect.html', $log_folder), $crawler->html());
|
2016-08-20 23:02:05 +02:00
|
|
|
$form = $crawler->selectButton('Se connecter')->form();
|
2016-09-11 22:53:40 +02:00
|
|
|
$crawler = $this->client->submit($form, ['st_username' => $this->config->login, 'st_passwd' => $this->config->password]);
|
2017-09-06 06:29:28 +02:00
|
|
|
// file_put_contents(sprintf('%s/connected.html', $log_folder), $crawler->html());
|
2016-08-20 23:02:05 +02:00
|
|
|
$this->is_connected = $crawler->filter('.account_userinfo')->count() > 0;
|
|
|
|
}
|
|
|
|
return $this->is_connected;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:37:29 +02:00
|
|
|
/**
|
|
|
|
* Retrieve all deals from the current account
|
|
|
|
*
|
|
|
|
* @return Deals
|
|
|
|
*/
|
2016-08-20 23:02:05 +02:00
|
|
|
public function getDeals()
|
|
|
|
{
|
2016-08-24 23:37:29 +02:00
|
|
|
if ($this->connect()) {
|
2016-08-20 23:02:05 +02:00
|
|
|
$crawler = $this->client->request('GET', self::ACCOUNT_URL);
|
|
|
|
$deals = $crawler->filter('#dashboard .list .element')->each(
|
|
|
|
function ($node) {
|
|
|
|
return $node->filter('.detail .title')->each(
|
|
|
|
function ($n) {
|
|
|
|
return $n->filter('a')->first()->attr('href');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-09-06 06:29:28 +02:00
|
|
|
$deals = array_map('current', $deals);
|
|
|
|
return (new Deals())->setAccount($this)->setDeals($deals);
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
2017-09-06 06:29:28 +02:00
|
|
|
return (new Deals())->setAccount($this);
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
2016-08-24 23:37:29 +02:00
|
|
|
|
|
|
|
public function addDeal(Deal $deal)
|
|
|
|
{
|
2016-09-11 22:53:40 +02:00
|
|
|
try {
|
2017-09-06 06:29:28 +02:00
|
|
|
// $t = date('YmdHis');
|
|
|
|
// $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
|
|
|
|
// mkdir($log_folder);
|
2016-09-11 22:53:40 +02:00
|
|
|
$crawler = $this->client->request('GET', self::ADD_URL);
|
2017-09-06 06:29:28 +02:00
|
|
|
// file_put_contents(sprintf('%s/add.html', $log_folder), $crawler->html());
|
2016-09-11 22:53:40 +02:00
|
|
|
$form = $crawler->selectButton('Valider')->form();
|
|
|
|
$image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r');
|
2016-09-12 16:41:57 +02:00
|
|
|
$fields = $form->getPhpValues();
|
|
|
|
$fields = array_merge(
|
|
|
|
$fields,
|
|
|
|
[
|
2017-09-06 06:29:28 +02:00
|
|
|
'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,
|
2016-09-12 16:41:57 +02:00
|
|
|
]
|
|
|
|
);
|
2017-09-06 06:29:28 +02:00
|
|
|
$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']);
|
2016-09-11 22:53:40 +02:00
|
|
|
// This one doesn't wor either -> redirect to Home
|
|
|
|
$form = $crawler->selectButton('Valider mon annonce')->form();
|
2017-09-06 06:29:28 +02:00
|
|
|
$crawler = $this->client->submit($form, ['accept_rule' => 1]);
|
2016-09-11 22:53:40 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
echo $e->getTraceAsString();
|
|
|
|
}
|
2016-08-24 23:37:29 +02:00
|
|
|
}
|
2016-09-12 16:41:57 +02:00
|
|
|
}
|