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

@ -5,20 +5,29 @@ include 'library/Account.php';
$client = new \Goutte\Client(); $client = new \Goutte\Client();
$client->setClient( $client->setClient(
new \GuzzleHttp\Client([ new \GuzzleHttp\Client([
'timeout' => 90, 'timeout' => 90,
'verify' => false, '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('APP_DIR', dirname(__FILE__));
define('DEALS_DIR', sprintf('%s/deals', APP_DIR)); define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini.dev', DEALS_DIR), true); $script_params = parse_ini_file(sprintf('%s/lbcreposter.ini.dev', DEALS_DIR), true);
$credentials = $script_params['CREDENTIALS']; $config = new \Shikiryu\LBCReposter\Config($script_params);
$login = $credentials['login'];
$password = $credentials['password']; $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()) { if ($account->connect()) {
// existing deals // existing deals
$deals = $account->getDeals(); $deals = $account->getDeals();
@ -26,5 +35,8 @@ if ($account->connect()) {
foreach ($deals as $deal) { foreach ($deals as $deal) {
$deal->save(DEALS_DIR); $deal->save(DEALS_DIR);
} }
$account->addDeal($deal);
var_dump($deals); var_dump($deals);
} }

View File

@ -47,7 +47,7 @@ class Account
{ {
if (!$this->is_connected) { if (!$this->is_connected) {
$crawler = $this->client->request('GET', self::HOME_URL); $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; return $this->is_connected;
} }
@ -62,9 +62,14 @@ class Account
public function connect() public function connect()
{ {
if (!$this->isConnected()) { 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); $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(); $form = $crawler->selectButton('Se connecter')->form();
$crawler = $this->client->submit($form, ['st_username' => $this->config->login, 'st_passwd' => $this->config->password]); $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; $this->is_connected = $crawler->filter('.account_userinfo')->count() > 0;
} }
return $this->is_connected; 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) public function addDeal(Deal $deal)
{ {
try { try {
$t = date('YmdHis'); // $t = date('YmdHis');
$log_folder = sprintf('%s/logs/%s', APP_DIR, $t); // $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
mkdir($log_folder); // mkdir($log_folder);
$crawler = $this->client->request('GET', self::ADD_URL); $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(); $form = $crawler->selectButton('Valider')->form();
$image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r'); $image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r');
$fields = $form->getPhpValues(); $fields = $form->getPhpValues();
$fields = array_merge( $fields = array_merge(
$fields, $fields,
[ [
'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code), 'geo_source' => 'user',
'zipcode' => $this->config->postal_code, 'geo_provider' => 'lbc',
'city' => $this->config->city, 'latitude' => '49.0707',
'region' => $this->config->region, 'longitude' => '2.31882',
'dpt_code' => $this->config->department, 'accept_localisation' => 'on',
'address' => $this->config->address, 'check_type_diff' => '0',
'name' => $this->config->name, 'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code),
'email' => $this->config->login, 'zipcode' => $this->config->postal_code,
'phone' => $this->config->phone, 'city' => $this->config->city,
'category' => $deal->getCategory(), 'region' => $this->config->region,
'type' => $deal->getType(), 'dpt_code' => $this->config->department,
'subject' => $deal->getSubject(), 'address' => $this->config->address,
'body' => $deal->getBody(), 'name' => $this->config->name,
'price' => $deal->getPrice(), 'email' => $this->config->login,
'image0' => $image0, 'phone' => $this->config->phone,
'no_salesmen' => 1, 'category' => $deal->getCategory(),
'phone_hidden' => 0, '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) { $uri = $form->getUri();
$item = [ // It needs to be done twice
'name' => $key, $this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
'contents' => $item, $crawler = $this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
];
});
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 // 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(); $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) { } catch (\Exception $e) {
echo $e->getTraceAsString(); echo $e->getTraceAsString();
} }

View File

@ -6,6 +6,56 @@ class Deals extends \ArrayObject
{ {
protected $account; 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 = []) private function parseArray(array $array = [])
{ {
if (count($array) == 1) { if (count($array) == 1) {
@ -19,33 +69,4 @@ class Deals extends \ArrayObject
return $deal; 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);
}
}
} }