API/library/Deal.php

423 lines
9.9 KiB
PHP

<?php
namespace Shikiryu\LBCReposter;
use Symfony\Component\DomCrawler\Crawler;
class Deal
{
const TYPE_OFFER = 's';
const TYPE_ASK = 'k';
/** @var Account */
private $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;
/** @var string */
protected $image0;
/** @var string */
protected $image1;
/** @var string */
protected $image2;
/** @var \DateTime */
protected $datecreation;
/** @var string */
protected $path;
/**
* Deal constructor.
* @param int|null $id
*/
public function __construct($id = null)
{
$this->id = $id;
}
/**
* @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;
}
/**
* @return \DateTime
*/
public function getDateCreation()
{
return $this->datecreation;
}
/**
* @param array|\DateTime|string $datecreation
* @return Deal
*/
public function setDateCreation($datecreation)
{
if (is_array($datecreation)) {
$datecreation = new \DateTime($datecreation['date']);
} elseif (is_string($datecreation)) {
$datecreation = new \DateTime($datecreation);
}
$this->datecreation = $datecreation;
return $this;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param string $path
* @return Deal
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* @return Account
*/
public function getAccount()
{
return $this->account;
}
/**
* @param Account $account
* @return Deal
*/
public function setAccount($account)
{
$this->account = $account;
return $this;
}
private function toJSON()
{
$reflection = new \ReflectionClass($this);
$props = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
$json = [];
foreach ($props as $prop) {
$method = sprintf('get%s', ucfirst($prop->getName()));
$json[$prop->getName()] = $this->$method();
}
return \json_encode($json, JSON_UNESCAPED_UNICODE);
}
/**
* @param string $dir
*
* @return bool
*/
public function save($dir)
{
try {
$save_dir = sprintf('%s/%s', $dir, $this->id);
if (!is_dir($save_dir)) {
mkdir($save_dir);
}
file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON());
foreach (range(0, 2) as $i) {
$image = sprintf('image%u', $i);
if (empty($this->$image)) {
break;
}
$ch = curl_init($this->$image);
$fp = fopen(sprintf('%s/%s.jpg', $save_dir, $image), 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
} catch (\Exception $e) {
return false;
}
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
*
* @return Deal
*/
public static function fromJSON($json)
{
$path = dirname(realpath($json));
$json = \json_decode(file_get_contents($json), true);
$deal = new self();
foreach ($json as $property => $value) {
$method = sprintf('set%s', ucfirst($property));
$deal->$method($value);
}
$deal->setPath($path);
return $deal;
}
}