3 Commits
0.1.1 ... react

Author SHA1 Message Date
Shikiryu
3a5008cc95 🚧 Démarre le développement suite au passage à React 2018-06-29 00:04:32 +02:00
7cada3dd07 🔧 Change le nom du bouton 2018-02-18 23:11:40 +01:00
bb5721f911 ⬆️ Met à jour le script suite au passage à React 2018-02-05 22:29:17 +01:00
9 changed files with 377 additions and 72 deletions

5
.gitignore vendored
View File

@@ -3,5 +3,6 @@
/composer.lock
/vendor/
/.idea/
/deals/lbcreposter.ini
/deals/*/
/data/config/lbcreposter.ini
/data/deals/*/
data/logs/*.html

0
data/logs/.gitkeep Normal file
View File

View File

@@ -2,7 +2,7 @@
namespace Shikiryu\LBCReposter;
use Goutte\Client;
use GuzzleHttp\Client;
class Account
{
@@ -20,22 +20,19 @@ class Account
*/
public function __construct(Config $config)
{
$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->client = new Client([
'timeout' => 90,
'verify' => false,
'curl' => [
CURLOPT_TIMEOUT => 60,
CURLOPT_TIMEOUT_MS => 60,
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_COOKIEJAR => tmpfile(),
],
'headers' => [
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
],
]);
$this->config = $config;
}
@@ -64,8 +61,8 @@ class Account
public function isConnected()
{
if (!$this->is_connected) {
$crawler = $this->client->request('GET', Actions::HOME_URL);
$this->is_connected = $crawler->filter('#account_logout')->count() == 1;
// $crawler = $this->client->request('GET', Actions::HOME_URL);
// $this->is_connected = $crawler->filter('#account_logout')->count() == 1;
}
return $this->is_connected;
}

View File

@@ -2,17 +2,22 @@
namespace Shikiryu\LBCReposter;
use GuzzleHttp\Exception\GuzzleException;
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';
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';
const VERBOSE_INFO = 1;
const VERBOSE_PAGE = 2;
const VERBOSE_REQUEST = 4;
@@ -21,6 +26,8 @@ class Actions
protected $debug = false;
protected $verbose = self::VERBOSE_INFO;
protected $access_token;
/**
* @var Account
*/
@@ -80,12 +87,34 @@ class Actions
{
if (!$this->account->isConnected()) {
// Let's connect to your account (or not)
$crawler = $this->account->getClient()->request('GET', self::LOGIN_URL);
$this->addPageDebug('connect', $crawler);
$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->addPageDebug('checkIsConnected', $crawler);
$this->account->setConnected($crawler->filter('.account_userinfo')->count() > 0);
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();
}
@@ -99,7 +128,40 @@ class Actions
{
if ($this->connect()) {
// Let's go to our dashboard
$crawler = $this->account->getClient()->request('GET', self::ACCOUNT_URL);
$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);
}
$this->addPageDebug('retrieve', $crawler);
// Let's list our existing deals
$deals = $crawler->filter('#dashboard .list .element')->each(
@@ -180,7 +242,7 @@ class Actions
$this->addPageDebug('add-2', $crawler);
// TODO need to check if we're in the good page
// Let's validate
$form = $crawler->selectButton('Valider mon annonce')->form();
$form = $crawler->selectButton('Valider')->form();
$crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
$this->addPageDebug('add-validation', $crawler);
// TODO return if it's the validation page or not

View File

@@ -43,48 +43,6 @@ class Deal
$this->id = $id;
}
public static function fromURL(Account $account, $url)
{
$client = $account->getClient();
$crawler = $client->request('GET', $url);
$deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id'));
$deal->setAccount($account);
$deal->setSubject(trim($crawler->filter('h1')->first()->text()));
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories));
$deal->setType(self::TYPE_OFFER);
$deal->setBody(trim($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()));
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
$date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
$date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
$hours = current($date_node->extract(['_text']));
$hours = substr($hours, strpos($hours, 'à')+2);
list($hour, $min) = explode(':', $hours);
$date->setTime((int) $hour, (int) $min);
$deal->setDateCreation($date);
if ($crawler->filter('.item_photo')->count() > 0) {
$script = $crawler ->filter('.item_photo')->first()->nextAll()
->filter('script')->first()->html();
preg_match_all("/\"(http.*ad-thumb.*)\"/m", $script, $urls);
$urls = $urls[1];
$images = [];
foreach ($urls as $i => $url) {
$images[] = [sprintf('setImage%s', $i) => str_replace('thumb', 'large', $url)];
}
} else {
$images = $crawler->filter('[data-popin-content]')->each(
function (Crawler $node, $i) {
return [ sprintf('setImage%s', $i) => $node->attr('data-popin-content')];
}
);
}
foreach ($images as $image) {
foreach ($image as $method => $uri) {
$deal->$method($uri);
}
}
return $deal;
}
/**
* @return int
*/
@@ -351,6 +309,99 @@ class Deal
return true;
}
/**
* @param Account $account
* @param string $url
*
* @return Deal
*/
public static function fromURL(Account $account, $url)
{
$client = $account->getClient();
$crawler = $client->request('GET', $url);
preg_match('/<script>window\.FLUX_STATE = (.*)<\/script>/im', $crawler->html(), $js_object);
if (isset($js_object[1])) {
return static::parseReactDeal($js_object[1], $account);
}
return static::parseHtmlDeal($crawler, $account);
}
/**
* @param $js_object
* @return null
*/
protected static function parseReactDeal($js_object, Account $account)
{
try {
$data = [];
JsParser::parse_jsobj($js_object, $data);
$deal = new self($data['adview']['list_id']);
$deal->setAccount($account);
$deal->setSubject($data['adview']['subject']);
$deal->setCategory($data['adview']['category_id']);
$deal->setType(self::TYPE_OFFER);
$deal->setBody($data['adview']['body']);
$deal->setPrice($data['adview']['price'][0]);
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $data['adview']['first_publication_date']);
$deal->setDateCreation($date);
foreach ($data['adview']['images']['urls_large'] as $i => $url) {
$deal->{'setImage'.$i}($url);
}
return $deal;
} catch (JsParserException $e) {
return null;
}
}
/**
* @param Crawler $crawler
* @param Account $account
*
* @return Deal
*/
protected static function parseHtmlDeal(Crawler $crawler, Account $account)
{
$deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id'));
$deal->setAccount($account);
$deal->setSubject(trim($crawler->filter('h1')->first()->text()));
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories));
$deal->setType(self::TYPE_OFFER);
$deal->setBody(trim($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()));
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
$date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
$date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
$hours = current($date_node->extract(['_text']));
$hours = substr($hours, strpos($hours, 'à')+2);
list($hour, $min) = explode(':', $hours);
$date->setTime((int) $hour, (int) $min);
$deal->setDateCreation($date);
if ($crawler->filter('.item_photo')->count() > 0) {
$script = $crawler ->filter('.item_photo')->first()->nextAll()
->filter('script')->first()->html();
preg_match_all("/\"(http.*ad-thumb.*)\"/m", $script, $urls);
$urls = $urls[1];
$images = [];
foreach ($urls as $i => $url) {
$images[] = [sprintf('setImage%s', $i) => str_replace('thumb', 'large', $url)];
}
} else {
$images = $crawler->filter('[data-popin-content]')->each(
function (Crawler $node, $i) {
return [ sprintf('setImage%s', $i) => $node->attr('data-popin-content')];
}
);
}
foreach ($images as $image) {
foreach ($image as $method => $uri) {
$deal->$method($uri);
}
}
return $deal;
}
/**
* @param string $json path to json
*

147
library/JsParser.php Normal file
View File

@@ -0,0 +1,147 @@
<?php
namespace Shikiryu\LBCReposter;
class JsParser
{
/**
* @param $str
* @param $data
*
* @return bool|string
*
* @throws JsParserException
*/
public static function parse_jsobj($str, &$data)
{
$str = trim($str);
if (strlen($str) < 1) return null;
if ($str{0} != '{') {
throw new JsParserException('The given string is not a JS object');
}
$str = substr($str, 1);
/* While we have data, and it's not the end of this dict (the comma is needed for nested dicts) */
while (strlen($str) && $str{0} != '}' && $str{0} != ',') {
/* find the key */
if ($str{0} == "'" || $str{0} == '"') {
/* quoted key */
list($str, $key) = self::parse_jsdata($str, ':');
} else {
$match = null;
/* unquoted key */
if (!preg_match('/^\s*[a-zA-z_][a-zA-Z_\d]*\s*:/', $str, $match)) {
throw new JsParserException('Invalid key ("' . $str . '")');
}
$key = $match[0];
$str = substr($str, strlen($key));
$key = trim(substr($key, 0, -1)); /* discard the ':' */
}
list($str, $data[$key]) = self::parse_jsdata($str, '}');
}
return substr($str, 1);
}
/**
* @param $str
* @param $term
* @return bool|int|mixed
* @throws JsParserException
*/
public static function comma_or_term_pos($str, $term)
{
$cpos = strpos($str, ',');
$tpos = strpos($str, $term);
if ($cpos === false && $tpos === false) {
throw new JsParserException('unterminated dict or array');
} else if ($cpos === false) {
return $tpos;
} else if ($tpos === false) {
return $cpos;
}
return min($tpos, $cpos);
}
/**
* @param $str
* @param string $term
* @return array
* @throws JsParserException
*/
public static function parse_jsdata($str, $term = "}")
{
$str = trim($str);
if (is_numeric($str{0} . "0")) {
/* a number (int or float) */
$newpos = self::comma_or_term_pos($str, $term);
$num = trim(substr($str, 0, $newpos));
$str = substr($str, $newpos + 1); /* discard num and comma */
if (!is_numeric($num)) {
throw new JsParserException('OOPSIE while parsing number: "' . $num . '"');
}
return array(trim($str), $num + 0);
} else if ($str{0} == '"' || $str{0} == "'") {
/* string */
$q = $str{0};
$offset = 1;
do {
$pos = strpos($str, $q, $offset);
$offset = $pos;
} while ($str{$pos - 1} == '\\'); /* find un-escaped quote */
$data = substr($str, 1, $pos - 1);
$str = substr($str, $pos);
$pos = self::comma_or_term_pos($str, $term);
$str = substr($str, $pos + 1);
return array(trim($str), $data);
} else if ($str{0} == '{') {
/* dict */
$data = array();
$str = self::parse_jsobj($str, $data);
return array($str, $data);
} else if ($str{0} == '[') {
/* array */
$arr = array();
$str = substr($str, 1);
if (']' === $str[0]) {
return ['', $arr];
}
while (strlen($str) && $str{0} != $term && $str{0} != ',') {
$val = null;
list($str, $val) = self::parse_jsdata($str, ']');
$arr[] = $val;
$str = trim($str);
}
$str = trim(substr($str, 1));
return array($str, $arr);
} else if (stripos($str, 'true') === 0) {
/* true */
$pos = self::comma_or_term_pos($str, $term);
$str = substr($str, $pos + 1); /* discard terminator */
return array(trim($str), true);
} else if (stripos($str, 'false') === 0) {
/* false */
$pos = self::comma_or_term_pos($str, $term);
$str = substr($str, $pos + 1); /* discard terminator */
return array(trim($str), false);
} else if (stripos($str, 'null') === 0) {
/* null */
$pos = self::comma_or_term_pos($str, $term);
$str = substr($str, $pos + 1); /* discard terminator */
return array(trim($str), null);
} else if (strpos($str, 'undefined') === 0) {
/* null */
$pos = self::comma_or_term_pos($str, $term);
$str = substr($str, $pos + 1); /* discard terminator */
return array(trim($str), null);
} else {
throw new JsParserException('Cannot figure out how to parse "' . $str . '" (term is ' . $term . ')');
}
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Shikiryu\LBCReposter;
class JsParserException extends \Exception
{
}

38
library/User.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
namespace Shikiryu\LBCReposter;
class User
{
protected $storeId;
protected $userId;
/**
* User constructor.
* @param $storeId
* @param $userId
*/
public function __construct($storeId, $userId)
{
$this->storeId = $storeId;
$this->userId = $userId;
}
/**
* @return mixed
*/
public function getStoreId()
{
return $this->storeId;
}
/**
* @return mixed
*/
public function getUserId()
{
return $this->userId;
}
}