❇️ Inclut le client dans la librairie

This commit is contained in:
Clement Desmidt 2017-09-28 22:33:47 +02:00
parent 272ab042d7
commit f7b6573375
3 changed files with 246 additions and 2 deletions

View File

@ -18,9 +18,24 @@ class Account
* @param Client $client * @param Client $client
* @param Config $config * @param Config $config
*/ */
public function __construct(Client $client, Config $config) public function __construct(Config $config)
{ {
$this->client = $client; $this->client = new \Goutte\Client();
$this->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',
],
])
);
$this->client->setMaxRedirects(10);
$this->config = $config; $this->config = $config;
} }

157
library/Actions.php Normal file
View File

@ -0,0 +1,157 @@
<?php
namespace Shikiryu\LBCReposter;
use Symfony\Component\DomCrawler\Crawler;
class Actions
{
const HOME_URL = 'https://www.leboncoin.fr/';
const LOGIN_URL = 'https://www.leboncoin.fr/beta/ajax/popins/connexion.html';
const ACCOUNT_URL = 'https://compteperso.leboncoin.fr/account/index.html';
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 Account
*/
protected $account;
/**
* Actions constructor.
* @param Account $account
*/
public function __construct(Account $account)
{
$this->account = $account;
}
/**
* Check if connected, if not, try to connect you
*
* @see Account#isConnected
*
* @return bool
*/
public function connect()
{
if (!$this->account->isConnected()) {
// Let's connect to your account (or not)
$crawler = $this->account->getClient()->request('GET', self::LOGIN_URL);
$form = $crawler->selectButton('Se connecter')->form();
$crawler = $this->account->getClient()->submit($form, ['st_username' => $this->account->getConfig()->login, 'st_passwd' => $this->account->getConfig()->password]);
$this->account->setConnected($crawler->filter('.account_userinfo')->count() > 0);
}
return $this->account->isConnected();
}
/**
* Retrieve all deals from the current account
*
* @return Deals
*/
public function retrieve()
{
if ($this->connect()) {
// Let's go to our dashboard
$crawler = $this->account->getClient()->request('GET', self::ACCOUNT_URL);
// Let's list our existing deals
$deals = $crawler->filter('#dashboard .list .element')->each(
function (Crawler $node) {
return $node->filter('.detail .title')->each(
function (Crawler $n) {
return $n->filter('a')->first()->attr('href');
}
);
}
);
// Let's flatten that array
$deals = array_map('current', $deals);
return (new Deals())->setAccount($this->account)->setDeals($deals);
}
// Let's return empty deals container
return (new Deals())->setAccount($this->account);
}
/**
* Create the given deal in the account
*
* @param Deal $deal
*
* @return bool
*/
public function create(Deal $deal)
{
try {
$crawler = $this->account->getClient()->request('GET', self::ADD_URL);
$form = $crawler->selectButton('Valider')->form();
$image0 = sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId());
$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->account->getConfig()->city, $this->account->getConfig()->postal_code),
'zipcode' => $this->account->getConfig()->postal_code,
'city' => $this->account->getConfig()->city,
'region' => $this->account->getConfig()->region,
'dpt_code' => $this->account->getConfig()->department,
'address' => $this->account->getConfig()->address,
'name' => $this->account->getConfig()->name,
'email' => $this->account->getConfig()->login,
'phone' => $this->account->getConfig()->phone,
'category' => $deal->getCategory(),
'type' => $deal->getType(),
'subject' => $deal->getSubject(),
'body' => $deal->getBody(),
'price' => $deal->getPrice(),
'no_salesmen' => 1,
'phone_hidden' => 1,
]
);
$uri = $form->getUri();
// It needs to be done twice !!
$this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
$crawler = $this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
// TODO need to check if we're in the good page
// Let's validate
$form = $crawler->selectButton('Valider mon annonce')->form();
$crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
// TODO return if it's the validation page or not
return true;
} catch (\Exception $e) {
// TODO manage exceptions
echo $e->getTraceAsString();
return false;
}
}
/**
* Delete the deal from the account
*
* @param Deal $deal
*
* @return bool
*/
public function delete(Deal $deal)
{
$fields = [
sprintf('selected_ads[%s]', $deal->getId()) => 'on',
'cmd' => 'delete',
'continue' => 'Continuer'
];
$crawler = $this->account->getClient()->request('POST', self::DELETE_URL, $fields);
// confirmation
$form = $crawler->selectButton('Valider')->form();
$crawler = $this->account->getClient()->submit($form, ['delete_reason' => '1']);
// TODO return if it's the validation page or not
return true;
}
}

72
library/Categories.php Normal file
View File

@ -0,0 +1,72 @@
<?php
namespace Shikiryu\LBCReposter;
class Categories
{
public static $categories = [
'33' => 'Offres d\'emploi',
'2' => 'Voitures',
'3' => 'Motos',
'4' => 'Caravaning',
'5' => 'Utilitaires',
'6' => 'Equipement Auto',
'44' => 'Equipement Moto',
'50' => 'Equipement Caravaning',
'7' => 'Nautisme',
'51' => 'Equipement Nautisme',
'9' => 'Ventes immobilières',
'10' => 'Locations',
'11' => 'Colocations',
'13' => 'Bureaux & Commerces',
'12' => 'Locations & Gîtes',
'67' => 'Chambres d\'hôtes',
'68' => 'Campings',
'69' => 'Hôtels',
'70' => 'Hébergements insolites',
'15' => 'Informatique',
'43' => 'Consoles & Jeux vidéo',
'16' => 'Image & Son',
'17' => 'Téléphonie',
'19' => 'Ameublement',
'20' => 'Electroménager',
'45' => 'Arts de la table',
'39' => 'Décoration',
'46' => 'Linge de maison',
'21' => 'Bricolage',
'52' => 'Jardinage',
'22' => 'Vêtements',
'53' => 'Chaussures',
'47' => 'Accessoires & Bagagerie',
'42' => 'Montres & Bijoux',
'23' => 'Equipement bébé',
'54' => 'Vêtements bébé',
'25' => 'DVD / Films',
'26' => 'CD / Musique',
'27' => 'Livres',
'28' => 'Animaux',
'55' => 'Vélos',
'29' => 'Sports & Hobbies',
'30' => 'Instruments de musique',
'40' => 'Collection',
'41' => 'Jeux & Jouets',
'48' => 'Vins & Gastronomie',
'57' => 'Matériel Agricole',
'58' => 'Transport - Manutention',
'59' => 'BTP - Chantier Gros-oeuvre',
'60' => 'Outillage - Matériaux 2nd-oeuvre',
'32' => 'Équipements Industriels',
'61' => 'Restauration - Hôtellerie',
'62' => 'Fournitures de Bureau',
'63' => 'Commerces & Marchés',
'64' => 'Matériel Médical',
'34' => 'Prestations de services',
'35' => 'Billetterie',
'49' => 'Evénements',
'36' => 'Cours particuliers',
'65' => 'Covoiturage',
'38' => 'Autres',
];
}