Ajoute la suppression d'annonce

This commit is contained in:
Shikiryu 2017-09-06 22:28:00 +02:00
parent 0245bda600
commit c231b7e26b
2 changed files with 24 additions and 6 deletions

View File

@ -10,6 +10,8 @@ class Account
const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html'; const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html';
const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html'; const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html';
const ADD_URL = 'https://www.leboncoin.fr/ai/form/0'; const ADD_URL = 'https://www.leboncoin.fr/ai/form/0';
const DELETE_URL = 'https://compteperso.leboncoin.fr/store/main?cmd=adservices';
const UPLOAD_URL = 'https://www.leboncoin.fr/ai/photo_upload_ajax/0';
/** @var Client */ /** @var Client */
protected $client; protected $client;
@ -102,11 +104,11 @@ class Account
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();
@ -133,20 +135,36 @@ 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' => 1, 'phone_hidden' => 1,
] ]
); );
$uri = self::UPLOAD_URL;
$this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
$uri = $form->getUri(); $uri = $form->getUri();
// It needs to be done twice // It needs to be done twice
$this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']); $this->client->request('POST', $uri, $fields, [], ['Content-Type => multipart/form-data']);
$crawler = $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 // This one doesn't wor either -> redirect to Home
file_put_contents(sprintf('%s/add-2.html', $log_folder), $crawler->html());
$form = $crawler->selectButton('Valider mon annonce')->form(); $form = $crawler->selectButton('Valider mon annonce')->form();
$crawler = $this->client->submit($form, ['accept_rule' => 1]); $crawler = $this->client->submit($form, ['accept_rule' => 1]);
} catch (\Exception $e) { } catch (\Exception $e) {
echo $e->getTraceAsString(); echo $e->getTraceAsString();
} }
} }
public function delete(Deal $deal)
{
$fields = [
sprintf('selected_ads[%s]', $deal->getId()) => 'on',
'cmd' => 'delete',
'continue' => 'Continuer'
];
$crawler = $this->client->request('POST', self::DELETE_URL, $fields);
// confirmation
$form = $crawler->selectButton('Valider')->form();
$crawler = $this->client->submit($form, ['delete_reason' => '1']);
}
} }

View File

@ -105,7 +105,7 @@ class Deal
$crawler = $client->request('GET', $url); $crawler = $client->request('GET', $url);
$deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id')); $deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id'));
$deal->setAccount($account); $deal->setAccount($account);
$deal->setSubject($crawler->filter('.item_imageCaption')->first()->text()); $deal->setSubject($crawler->filter('h1')->first()->text());
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), self::$categories)); $deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), self::$categories));
$deal->setType(self::TYPE_OFFER); $deal->setType(self::TYPE_OFFER);
$deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()); $deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text());