Récupère une annonce existante

This commit is contained in:
Shikiryu 2016-08-20 23:02:05 +02:00
parent 581488037a
commit 7bbf5a8dff
5 changed files with 340 additions and 78 deletions

8
.gitignore vendored
View File

@ -1,5 +1,5 @@
/composer.phar /composer.phar
/index.php /index.php
/composer.lock /composer.lock
/vendor/ /vendor/
/.idea/ /.idea/

View File

@ -10,5 +10,8 @@
"name": "Shikiryu", "name": "Shikiryu",
"email": "clement@desmidt.fr" "email": "clement@desmidt.fr"
} }
] ],
"autoload": {
"psr-4": {"Shikiryu\\LBCReposter\\": "library/"}
}
} }

View File

@ -1,60 +1,80 @@
<?php <?php
namespace Shikiryu\LBCReposter; namespace Shikiryu\LBCReposter;
use Goutte\Client; use Goutte\Client;
class Account class Account
{ {
const HOME_URL = 'https://www.leboncoin.fr/'; const HOME_URL = 'https://www.leboncoin.fr/';
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';
/** @var Client */ /** @var Client */
protected $client; protected $client;
/** @var string */ /** @var string */
protected $login; protected $login;
/** @var string */ /** @var string */
protected $password; protected $password;
/** @var bool */ /** @var bool */
protected $is_connected = false; protected $is_connected = false;
/** /**
* Account constructor. * Account constructor.
* @param Client $client * @param Client $client
* @param string $login * @param string $login
* @param string $password * @param string $password
*/ */
public function __construct(Client $client, $login, $password) public function __construct(Client $client, $login, $password)
{ {
$this->client = $client; $this->client = $client;
$this->login = $login; $this->login = $login;
$this->password = $password; $this->password = $password;
} }
public function isConnected() /**
{ * @return Client
if (!$this->is_connected) { */
$crawler = $this->client->request('GET', self::HOME_URL); public function getClient()
$this->is_connected = $crawler->filter('.loggedOut')->count() == 0; {
} return $this->client;
return $this->is_connected; }
}
public function isConnected()
public function connect() {
{ if (!$this->is_connected) {
if (!$this->isConnected()) { $crawler = $this->client->request('GET', self::HOME_URL);
$crawler = $this->client->request('GET', self::LOGIN_URL); $this->is_connected = $crawler->filter('.loggedOut')->count() == 0;
$form = $crawler->selectButton('Se connecter')->form(); }
$crawler = $this->client->submit($form, ['st_username' => $this->login, 'st_passwd' => $this->password]); return $this->is_connected;
$this->is_connected = $crawler->filter('.account_userinfo')->count() > 0; }
}
return $this->is_connected; public function connect()
} {
if (!$this->isConnected()) {
public function getDeals() $crawler = $this->client->request('GET', self::LOGIN_URL);
{ $form = $crawler->selectButton('Se connecter')->form();
$crawler = $this->client->request('GET', self::ACCOUNT_URL); $crawler = $this->client->submit($form, ['st_username' => $this->login, 'st_passwd' => $this->password]);
// $deals = $crawler->filter('#dashboard .list .element')->each() $this->is_connected = $crawler->filter('.account_userinfo')->count() > 0;
} }
return $this->is_connected;
}
public function getDeals()
{
if ($this->isConnected()) {
$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');
}
);
}
);
return new Deals($this, $deals);
}
return new Deals($this, []);
}
} }

View File

@ -1,8 +1,210 @@
<?php <?php
namespace Shikiryu\LBCReposter; namespace Shikiryu\LBCReposter;
class Deal class Deal
{ {
const TYPE_OFFER = 's';
const TYPE_ASK = 'k';
/** @var Account */
protected $account;
/** @var int */
protected $id;
/** @var int */
protected $category;
/** @var string type */
protected $type = self::TYPE_OFFER;
/** @var string title */
protected $subject;
/** @var string texte */
protected $body;
/** @var float */
protected $price;
protected $image0;
protected $image1;
protected $image2;
/**
* Deal constructor.
* @param int|null $id
*/
public function __construct($id = null)
{
$this->id = $id;
}
public static function fromURL(Account $account, $url)
{
$client = $account->getClient();
$crawler = $client->request('GET', $url);
$deal = new self();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
* @return Deal
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return int
*/
public function getCategory()
{
return $this->category;
}
/**
* @param int $category
* @return Deal
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
* @return Deal
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* @param string $subject
* @return Deal
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* @param string $body
* @return Deal
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @param float $price
* @return Deal
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* @return mixed
*/
public function getImage0()
{
return $this->image0;
}
/**
* @param mixed $image0
* @return Deal
*/
public function setImage0($image0)
{
$this->image0 = $image0;
return $this;
}
/**
* @return mixed
*/
public function getImage1()
{
return $this->image1;
}
/**
* @param mixed $image1
* @return Deal
*/
public function setImage1($image1)
{
$this->image1 = $image1;
return $this;
}
/**
* @return mixed
*/
public function getImage2()
{
return $this->image2;
}
/**
* @param mixed $image2
* @return Deal
*/
public function setImage2($image2)
{
$this->image2 = $image2;
return $this;
}
} }

View File

@ -1,8 +1,45 @@
<?php <?php
namespace Shikiryu\LBCReposter; namespace Shikiryu\LBCReposter;
class Deals class Deals extends \ArrayObject
{ {
protected $account;
private function parseArray(array $array = [])
{
if (count($array) == 1) {
return Deal::fromURL($this->account, current(array_values($array)));
}
$deal = new Deal();
foreach ($array as $name => $value) {
$method = sprintf('set%s', ucfirst($name));
$deal->$method($value);
}
return $deal;
}
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);
}
}
} }