✨ Ajoute l'envoi d'annonce (sans images)
This commit is contained in:
parent
4f12d3da3b
commit
0245bda600
@ -7,18 +7,27 @@ $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);
|
||||||
}
|
}
|
@ -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,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)
|
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,
|
||||||
[
|
[
|
||||||
|
'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),
|
'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code),
|
||||||
'zipcode' => $this->config->postal_code,
|
'zipcode' => $this->config->postal_code,
|
||||||
'city' => $this->config->city,
|
'city' => $this->config->city,
|
||||||
@ -121,27 +133,18 @@ class Account
|
|||||||
'subject' => $deal->getSubject(),
|
'subject' => $deal->getSubject(),
|
||||||
'body' => $deal->getBody(),
|
'body' => $deal->getBody(),
|
||||||
'price' => $deal->getPrice(),
|
'price' => $deal->getPrice(),
|
||||||
'image0' => $image0,
|
// 'image0' => $image0,
|
||||||
'no_salesmen' => 1,
|
'no_salesmen' => 1,
|
||||||
'phone_hidden' => 0,
|
'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();
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user