🔊 Ajoute des logs
This commit is contained in:
parent
768af6e636
commit
c078fc1c68
@ -1,3 +1,7 @@
|
|||||||
|
[DEBUG]
|
||||||
|
log=true
|
||||||
|
verbose=1
|
||||||
|
|
||||||
[CREDENTIALS]
|
[CREDENTIALS]
|
||||||
login=xxxxx
|
login=xxxxx
|
||||||
password=xxxxx
|
password=xxxxx
|
||||||
|
@ -13,6 +13,14 @@ class Actions
|
|||||||
const DELETE_URL = 'https://compteperso.leboncoin.fr/store/main?cmd=adservices';
|
const DELETE_URL = 'https://compteperso.leboncoin.fr/store/main?cmd=adservices';
|
||||||
const UPLOAD_URL = 'https://www.leboncoin.fr/ai/photo_upload_ajax/0';
|
const UPLOAD_URL = 'https://www.leboncoin.fr/ai/photo_upload_ajax/0';
|
||||||
|
|
||||||
|
const VERBOSE_INFO = 1;
|
||||||
|
const VERBOSE_PAGE = 2;
|
||||||
|
const VERBOSE_REQUEST = 4;
|
||||||
|
const VERBOSE_ALL = 8;
|
||||||
|
|
||||||
|
protected $debug = false;
|
||||||
|
protected $verbose = self::VERBOSE_INFO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Account
|
* @var Account
|
||||||
*/
|
*/
|
||||||
@ -27,6 +35,39 @@ class Actions
|
|||||||
$this->account = $account;
|
$this->account = $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $debug
|
||||||
|
* @return Actions
|
||||||
|
*/
|
||||||
|
public function setDebug($debug)
|
||||||
|
{
|
||||||
|
$this->debug = $debug;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $verbose
|
||||||
|
* @return Actions
|
||||||
|
*/
|
||||||
|
public function setVerbose($verbose)
|
||||||
|
{
|
||||||
|
$this->verbose = $verbose;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $action
|
||||||
|
* @param Crawler $crawler
|
||||||
|
* @return bool|int
|
||||||
|
*/
|
||||||
|
private function addPageDebug($action, Crawler $crawler)
|
||||||
|
{
|
||||||
|
if ($this->debug !== false) {
|
||||||
|
return file_put_contents(sprintf('%s/%s-%s.html', $this->debug, date('YmdHi'), $action), $crawler->html());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if connected, if not, try to connect you
|
* Check if connected, if not, try to connect you
|
||||||
@ -40,8 +81,10 @@ class Actions
|
|||||||
if (!$this->account->isConnected()) {
|
if (!$this->account->isConnected()) {
|
||||||
// Let's connect to your account (or not)
|
// Let's connect to your account (or not)
|
||||||
$crawler = $this->account->getClient()->request('GET', self::LOGIN_URL);
|
$crawler = $this->account->getClient()->request('GET', self::LOGIN_URL);
|
||||||
|
$this->addPageDebug('connect', $crawler);
|
||||||
$form = $crawler->selectButton('Se connecter')->form();
|
$form = $crawler->selectButton('Se connecter')->form();
|
||||||
$crawler = $this->account->getClient()->submit($form, ['st_username' => $this->account->getConfig()->login, 'st_passwd' => $this->account->getConfig()->password]);
|
$crawler = $this->account->getClient()->submit($form, ['st_username' => $this->account->getConfig()->login, 'st_passwd' => $this->account->getConfig()->password]);
|
||||||
|
$this->addPageDebug('checkIsConnected', $crawler);
|
||||||
$this->account->setConnected($crawler->filter('.account_userinfo')->count() > 0);
|
$this->account->setConnected($crawler->filter('.account_userinfo')->count() > 0);
|
||||||
}
|
}
|
||||||
return $this->account->isConnected();
|
return $this->account->isConnected();
|
||||||
@ -57,6 +100,7 @@ class Actions
|
|||||||
if ($this->connect()) {
|
if ($this->connect()) {
|
||||||
// Let's go to our dashboard
|
// Let's go to our dashboard
|
||||||
$crawler = $this->account->getClient()->request('GET', self::ACCOUNT_URL);
|
$crawler = $this->account->getClient()->request('GET', self::ACCOUNT_URL);
|
||||||
|
$this->addPageDebug('retrieve', $crawler);
|
||||||
// Let's list our existing deals
|
// Let's list our existing deals
|
||||||
$deals = $crawler->filter('#dashboard .list .element')->each(
|
$deals = $crawler->filter('#dashboard .list .element')->each(
|
||||||
function (Crawler $node) {
|
function (Crawler $node) {
|
||||||
@ -87,7 +131,18 @@ class Actions
|
|||||||
try {
|
try {
|
||||||
$crawler = $this->account->getClient()->request('GET', self::ADD_URL);
|
$crawler = $this->account->getClient()->request('GET', self::ADD_URL);
|
||||||
$form = $crawler->selectButton('Valider')->form();
|
$form = $crawler->selectButton('Valider')->form();
|
||||||
$image0 = sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId());
|
$path = $deal->getPath();
|
||||||
|
if (empty($path)) {
|
||||||
|
$path = sprintf('%s/%s', DEALS_DIR, $deal->getId());
|
||||||
|
}
|
||||||
|
$images = [];
|
||||||
|
$i = 0;
|
||||||
|
foreach (new \DirectoryIterator($path) as $file) {
|
||||||
|
if ($file->isFile() && $file->getExtension() === 'jpg') {
|
||||||
|
$images['image'.$i] = $file->getRealPath();
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
$fields = $form->getPhpValues();
|
$fields = $form->getPhpValues();
|
||||||
$fields = array_merge(
|
$fields = array_merge(
|
||||||
$fields,
|
$fields,
|
||||||
@ -118,12 +173,15 @@ class Actions
|
|||||||
);
|
);
|
||||||
$uri = $form->getUri();
|
$uri = $form->getUri();
|
||||||
// It needs to be done twice !!
|
// It needs to be done twice !!
|
||||||
$this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
|
$crawler = $this->account->getClient()->request('POST', $uri, $fields, $images);
|
||||||
$crawler = $this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
|
$this->addPageDebug('add-1', $crawler);
|
||||||
|
$crawler = $this->account->getClient()->request('POST', $uri, $fields, $images);
|
||||||
|
$this->addPageDebug('add-2', $crawler);
|
||||||
// TODO need to check if we're in the good page
|
// TODO need to check if we're in the good page
|
||||||
// Let's validate
|
// Let's validate
|
||||||
$form = $crawler->selectButton('Valider mon annonce')->form();
|
$form = $crawler->selectButton('Valider mon annonce')->form();
|
||||||
$crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
|
$crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
|
||||||
|
$this->addPageDebug('add-validation', $crawler);
|
||||||
// TODO return if it's the validation page or not
|
// TODO return if it's the validation page or not
|
||||||
return true;
|
return true;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@ -148,9 +206,11 @@ class Actions
|
|||||||
'continue' => 'Continuer'
|
'continue' => 'Continuer'
|
||||||
];
|
];
|
||||||
$crawler = $this->account->getClient()->request('POST', self::DELETE_URL, $fields);
|
$crawler = $this->account->getClient()->request('POST', self::DELETE_URL, $fields);
|
||||||
|
$this->addPageDebug('delete', $crawler);
|
||||||
// confirmation
|
// confirmation
|
||||||
$form = $crawler->selectButton('Valider')->form();
|
$form = $crawler->selectButton('Valider')->form();
|
||||||
$crawler = $this->account->getClient()->submit($form, ['delete_reason' => '1']);
|
$crawler = $this->account->getClient()->submit($form, ['delete_reason' => '1']);
|
||||||
|
$this->addPageDebug('delete-validation', $crawler);
|
||||||
// TODO return if it's the validation page or not
|
// TODO return if it's the validation page or not
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,8 @@ class Deal
|
|||||||
protected $image2;
|
protected $image2;
|
||||||
/** @var \DateTime */
|
/** @var \DateTime */
|
||||||
protected $datecreation;
|
protected $datecreation;
|
||||||
|
/** @var string */
|
||||||
|
protected $path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deal constructor.
|
* Deal constructor.
|
||||||
@ -47,10 +49,10 @@ 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('h1')->first()->text());
|
$deal->setSubject(trim($crawler->filter('h1')->first()->text()));
|
||||||
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories));
|
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$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(trim($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()));
|
||||||
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
|
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
|
||||||
$date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
|
$date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
|
||||||
$date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
|
$date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
|
||||||
@ -254,15 +256,38 @@ class Deal
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \DateTime $price
|
* @param array|\DateTime|string $datecreation
|
||||||
* @return Deal
|
* @return Deal
|
||||||
*/
|
*/
|
||||||
public function setDateCreation($datecreation)
|
public function setDateCreation($datecreation)
|
||||||
{
|
{
|
||||||
|
if (is_array($datecreation)) {
|
||||||
|
$datecreation = new \DateTime($datecreation['date']);
|
||||||
|
} elseif (is_string($datecreation)) {
|
||||||
|
$datecreation = new \DateTime($datecreation);
|
||||||
|
}
|
||||||
$this->datecreation = $datecreation;
|
$this->datecreation = $datecreation;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPath()
|
||||||
|
{
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return Deal
|
||||||
|
*/
|
||||||
|
public function setPath($path)
|
||||||
|
{
|
||||||
|
$this->path = $path;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Account
|
* @return Account
|
||||||
*/
|
*/
|
||||||
@ -333,12 +358,14 @@ class Deal
|
|||||||
*/
|
*/
|
||||||
public static function fromJSON($json)
|
public static function fromJSON($json)
|
||||||
{
|
{
|
||||||
|
$path = dirname(realpath($json));
|
||||||
$json = \json_decode(file_get_contents($json), true);
|
$json = \json_decode(file_get_contents($json), true);
|
||||||
$deal = new self();
|
$deal = new self();
|
||||||
foreach ($json as $property => $value) {
|
foreach ($json as $property => $value) {
|
||||||
$method = sprintf('set%s', ucfirst($property));
|
$method = sprintf('set%s', ucfirst($property));
|
||||||
$deal->$method($value);
|
$deal->$method($value);
|
||||||
}
|
}
|
||||||
|
$deal->setPath($path);
|
||||||
return $deal;
|
return $deal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user