API/library/Actions.php

284 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace Shikiryu\LBCReposter;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;
class Actions
{
const HOME_URL = 'https://www.leboncoin.fr/';
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';
const API_LOGIN_URL = 'https://api.leboncoin.fr/api/oauth/v1/token';
const API_DEAL_RETRIEVE_URL = 'https://api.leboncoin.fr/api/dashboard/v1/search';
const API_PROFILE_URL = 'https://api.leboncoin.fr/api/accounts/v1/accounts/me/personaldata';
const API_STATUS_URL = 'https://api.leboncoin.fr/api/dashboard/v1/kyc_status/me';
2017-10-01 22:40:56 +02:00
const VERBOSE_INFO = 1;
const VERBOSE_PAGE = 2;
const VERBOSE_REQUEST = 4;
const VERBOSE_ALL = 8;
protected $debug = false;
protected $verbose = self::VERBOSE_INFO;
protected $access_token;
/**
* @var Account
*/
protected $account;
/**
* Actions constructor.
* @param Account $account
*/
public function __construct(Account $account)
{
$this->account = $account;
}
2017-10-01 22:40:56 +02:00
/**
* @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
*
* @see Account#isConnected
*
* @return bool
*/
public function connect()
{
if (!$this->account->isConnected()) {
// Let's connect to your account (or not)
try {
$crawler = $this->account->getClient()->request('POST', self::API_LOGIN_URL, [
'form_params' => [
'client_id' => 'frontweb',
'grant_type' => 'password',
'password' => $this->account->getConfig()->password,
'username' => $this->account->getConfig()->login
]
]);
$response = json_decode($crawler->getBody()->getContents());
$this->access_token = $response->access_token;
} catch (GuzzleException $e) {
$this->account->setConnected(false);
}
try {
$crawler = $this->account->getClient()->request('GET', self::API_STATUS_URL,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->access_token)
]
]
);
$response = json_decode($crawler->getBody()->getContents());
$this->account->setConnected($response->status === 1);
} catch (GuzzleException $e) {
$this->account->setConnected(false);
}
}
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
$search_params = [
'context' => 'default',
'filters' => [
'owner' => [
'store_id' => ''
]
],
'limit' => '5',
'offset' => '0',
'sort_by' => 'date',
'sort_order' => 'desc',
];
var_dump($search_params);
var_dump( $this->access_token );
try {
$crawler = $this->account->getClient()->request('POST', self::API_DEAL_RETRIEVE_URL,
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
// 'api_key' => self::API_KEY,
'Authorization' => sprintf('Bearer %s', $this->access_token),
],
'body' => json_encode($search_params),
'debug' => true,
]
);
$response = json_decode($crawler->getBody()->getContents());
var_dump($response); exit;
$this->account->setConnected($response->status === 1);
} catch (GuzzleException $e) {
echo $e->getMessage() . ' - '.$e->getTraceAsString(); exit;
$this->account->setConnected(false);
}
2017-10-01 22:40:56 +02:00
$this->addPageDebug('retrieve', $crawler);
// 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);
$this->addPageDebug('add-form', $crawler);
$form = $crawler->selectButton('Valider')->form();
2017-10-01 22:40:56 +02:00
$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 = 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 !!
2017-10-01 22:40:56 +02:00
$crawler = $this->account->getClient()->request('POST', $uri, $fields, $images);
$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
// Let's validate
2018-02-18 23:11:40 +01:00
$form = $crawler->selectButton('Valider')->form();
$crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
2017-10-01 22:40:56 +02:00
$this->addPageDebug('add-validation', $crawler);
// 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);
2017-10-01 22:40:56 +02:00
$this->addPageDebug('delete', $crawler);
// check s'il y a un bouton "Valider", s'il y en a pas c'est que la demande a déjà été faite
$node = $crawler->filterXPath('//input[@value="Valider"]');
if ($node->count() == 1) {
// 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
$this->addPageDebug('delete-validation', $crawler);
}
return true;
}
}