Ajoute l'envoi d'annonce (sans images)

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

View File

@ -7,18 +7,27 @@ $client->setClient(
new \GuzzleHttp\Client([
'timeout' => 90,
'verify' => false,
'curl' => [
CURLOPT_TIMEOUT => 60,
CURLOPT_TIMEOUT_MS => 60,
CURLOPT_CONNECTTIMEOUT => 60,
],
'headers' => [
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
],
])
);
$client->setMaxRedirects(10);
define('APP_DIR', dirname(__FILE__));
define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini.dev', DEALS_DIR), true);
$credentials = $script_params['CREDENTIALS'];
$login = $credentials['login'];
$password = $credentials['password'];
$config = new \Shikiryu\LBCReposter\Config($script_params);
$account = new \Shikiryu\LBCReposter\Account($client, $config);
$deal = \Shikiryu\LBCReposter\Deal::fromJSON(sprintf('%s/deals/tmp/data.json', APP_DIR));
$account = new \Shikiryu\LBCReposter\Account($client, $login, $password);
if ($account->connect()) {
// existing deals
$deals = $account->getDeals();
@ -26,5 +35,8 @@ if ($account->connect()) {
foreach ($deals as $deal) {
$deal->save(DEALS_DIR);
}
$account->addDeal($deal);
var_dump($deals);
}

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,25 +93,32 @@ 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,
[
'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,
@ -121,27 +133,18 @@ class Account
'subject' => $deal->getSubject(),
'body' => $deal->getBody(),
'price' => $deal->getPrice(),
'image0' => $image0,
// 'image0' => $image0,
'no_salesmen' => 1,
'phone_hidden' => 0,
'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);
}
}
}