🔨 Factorise le plus possible pour en faire une API

This commit is contained in:
Clement Desmidt 2017-09-27 21:42:22 +02:00
parent 3842852dfe
commit 348ebfb170
4 changed files with 43 additions and 183 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "shikiryu/lbcreposter", "name": "shikiryu/lbcreposter-api",
"description": "Repost deal in LBC", "description": "Manage deals in LBC",
"type": "project", "type": "project",
"require": { "require": {
"fabpot/goutte": "^3.1" "fabpot/goutte": "^3.1"

View File

@ -6,13 +6,6 @@ use Goutte\Client;
class Account class Account
{ {
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 Client */ /** @var Client */
protected $client; protected $client;
/** @var Config */ /** @var Config */
@ -39,6 +32,14 @@ class Account
return $this->client; return $this->client;
} }
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/** /**
* Check if it's connected * Check if it's connected
* (if there's a "logout" link) * (if there's a "logout" link)
@ -48,120 +49,17 @@ class Account
public function isConnected() public function isConnected()
{ {
if (!$this->is_connected) { if (!$this->is_connected) {
$crawler = $this->client->request('GET', self::HOME_URL); $crawler = $this->client->request('GET', Actions::HOME_URL);
$this->is_connected = $crawler->filter('#account_logout')->count() == 1; $this->is_connected = $crawler->filter('#account_logout')->count() == 1;
} }
return $this->is_connected; return $this->is_connected;
} }
/** /**
* Check if connected, if not, try to connect you * @param bool $connected
*
* @see this#isConnected
*
* @return bool
*/ */
public function connect() public function setConnected($connected = false)
{ {
if (!$this->isConnected()) { $this->is_connected = $connected;
// $t = date('YmdHis');
// $log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
// mkdir($log_folder);
$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();
$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;
}
return $this->is_connected;
}
/**
* Retrieve all deals from the current account
*
* @return Deals
*/
public function getDeals()
{
if ($this->connect()) {
$crawler = $this->client->request('GET', self::ACCOUNT_URL);
$deals = $crawler->filter('#dashboard .list .element')->each(
function ($node) {
return $node->filter('.detail .title')->each(
function ($n) {
return $n->filter('a')->first()->attr('href');
}
);
}
);
$deals = array_map('current', $deals);
return (new Deals())->setAccount($this)->setDeals($deals);
}
return (new Deals())->setAccount($this);
}
public function addDeal(Deal $deal)
{
try {
$t = date('YmdHis');
$log_folder = sprintf('%s/logs/%s', APP_DIR, $t);
mkdir($log_folder);
$crawler = $this->client->request('GET', self::ADD_URL);
file_put_contents(sprintf('%s/add.html', $log_folder), $crawler->html());
$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->config->city, $this->config->postal_code),
'zipcode' => $this->config->postal_code,
'city' => $this->config->city,
'region' => $this->config->region,
'dpt_code' => $this->config->department,
'address' => $this->config->address,
'name' => $this->config->name,
'email' => $this->config->login,
'phone' => $this->config->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
$crawler = $this->client->request('POST', $uri, $fields, ['image0' => $image0]);
file_put_contents(sprintf('%s/upload-3.html', $log_folder), $crawler->html());
$crawler = $this->client->request('POST', $uri, $fields, ['image0' => $image0]);
file_put_contents(sprintf('%s/add-2.html', $log_folder), $crawler->html());
$form = $crawler->selectButton('Valider mon annonce')->form();
$crawler = $this->client->submit($form, ['accept_rule' => 1]);
} catch (\Exception $e) {
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

@ -7,70 +7,6 @@ class Deal
const TYPE_OFFER = 's'; const TYPE_OFFER = 's';
const TYPE_ASK = 'k'; const TYPE_ASK = 'k';
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',
];
/** @var Account */ /** @var Account */
private $account; private $account;
/** @var int */ /** @var int */
@ -85,9 +21,11 @@ class Deal
protected $body; protected $body;
/** @var float */ /** @var float */
protected $price; protected $price;
/** @var string */
protected $image0; protected $image0;
/** @var string */
protected $image1; protected $image1;
/** @var string */
protected $image2; protected $image2;
/** /**
@ -106,7 +44,7 @@ class Deal
$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($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(), 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($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'));
@ -117,7 +55,7 @@ class Deal
); );
foreach ($images as $image) { foreach ($images as $image) {
foreach ($image as $method => $uri) { foreach ($image as $method => $uri) {
$deal->$method(sprintf('http:%s', $uri)); $deal->$method($uri);
} }
} }
return $deal; return $deal;

View File

@ -6,12 +6,22 @@ class Deals extends \ArrayObject
{ {
protected $account; protected $account;
/**
* @param Deal $deal
*
* @return $this
*/
public function addDeal(Deal $deal) public function addDeal(Deal $deal)
{ {
$this->append($deal); $this->append($deal);
return $this; return $this;
} }
/**
* @param string[]|Deal[] $deals
*
* @return $this
*/
public function addDeals(array $deals) public function addDeals(array $deals)
{ {
array_walk($deals, function(&$deal) { array_walk($deals, function(&$deal) {
@ -25,6 +35,11 @@ class Deals extends \ArrayObject
return $this; return $this;
} }
/**
* @param string[]|Deal[] $deals
*
* @return $this
*/
public function setDeals(array $deals) public function setDeals(array $deals)
{ {
array_walk($deals, function(&$deal) { array_walk($deals, function(&$deal) {
@ -38,6 +53,7 @@ class Deals extends \ArrayObject
/** /**
* @param Account $account * @param Account $account
*
* @return Deals * @return Deals
*/ */
public function setAccount($account) public function setAccount($account)
@ -46,6 +62,9 @@ class Deals extends \ArrayObject
return $this; return $this;
} }
/**
* @param mixed $value
*/
public function append($value) public function append($value)
{ {
if (is_array($value)) { if (is_array($value)) {
@ -56,6 +75,11 @@ class Deals extends \ArrayObject
} }
} }
/**
* @param array $array
*
* @return Deal
*/
private function parseArray(array $array = []) private function parseArray(array $array = [])
{ {
if (count($array) == 1) { if (count($array) == 1) {