Ajoute la date de mise en ligne

Fix #1
This commit is contained in:
Clement Desmidt 2017-09-30 15:41:23 +02:00
parent 2750443612
commit 29ea8882fe

View File

@ -1,317 +1,344 @@
<?php <?php
namespace Shikiryu\LBCReposter; namespace Shikiryu\LBCReposter;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
class Deal class Deal
{ {
const TYPE_OFFER = 's'; const TYPE_OFFER = 's';
const TYPE_ASK = 'k'; const TYPE_ASK = 'k';
/** @var Account */ /** @var Account */
private $account; private $account;
/** @var int */ /** @var int */
protected $id; protected $id;
/** @var int */ /** @var int */
protected $category; protected $category;
/** @var string type */ /** @var string type */
protected $type = self::TYPE_OFFER; protected $type = self::TYPE_OFFER;
/** @var string title */ /** @var string title */
protected $subject; protected $subject;
/** @var string texte */ /** @var string texte */
protected $body; protected $body;
/** @var float */ /** @var float */
protected $price; protected $price;
/** @var string */ /** @var string */
protected $image0; protected $image0;
/** @var string */ /** @var string */
protected $image1; protected $image1;
/** @var string */ /** @var string */
protected $image2; protected $image2;
/** @var \DateTime */
/** protected $datecreation;
* Deal constructor.
* @param int|null $id /**
*/ * Deal constructor.
public function __construct($id = null) * @param int|null $id
{ */
$this->id = $id; public function __construct($id = null)
} {
$this->id = $id;
public static function fromURL(Account $account, $url) }
{
$client = $account->getClient(); public static function fromURL(Account $account, $url)
$crawler = $client->request('GET', $url); {
$deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id')); $client = $account->getClient();
$deal->setAccount($account); $crawler = $client->request('GET', $url);
$deal->setSubject($crawler->filter('h1')->first()->text()); $deal = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id'));
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories)); $deal->setAccount($account);
$deal->setType(self::TYPE_OFFER); $deal->setSubject($crawler->filter('h1')->first()->text());
$deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()); $deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories));
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content')); $deal->setType(self::TYPE_OFFER);
if ($crawler->filter('.item_photo')->count() > 0) { $deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text());
$script = $crawler ->filter('.item_photo')->first()->nextAll() $deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
->filter('script')->first()->html(); $date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
preg_match_all("/\"(http.*ad-thumb.*)\"/m", $script, $urls); $date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
$urls = $urls[1]; $hours = current($date_node->extract(['_text']));
$images = []; $hours = substr($hours, strpos($hours, 'à')+2);
foreach ($urls as $i => $url) { list($hour, $min) = explode(':', $hours);
$images[] = [sprintf('setImage%s', $i) => str_replace('thumb','large', $url)]; $date->setTime((int) $hour, (int) $min);
} $deal->setDateCreation($date);
} else { if ($crawler->filter('.item_photo')->count() > 0) {
$images = $crawler->filter('[data-popin-content]')->each( $script = $crawler ->filter('.item_photo')->first()->nextAll()
function (Crawler $node, $i) { ->filter('script')->first()->html();
return [ sprintf('setImage%s', $i) => $node->attr('data-popin-content')]; preg_match_all("/\"(http.*ad-thumb.*)\"/m", $script, $urls);
} $urls = $urls[1];
); $images = [];
} foreach ($urls as $i => $url) {
foreach ($images as $image) { $images[] = [sprintf('setImage%s', $i) => str_replace('thumb', 'large', $url)];
foreach ($image as $method => $uri) { }
$deal->$method($uri); } else {
} $images = $crawler->filter('[data-popin-content]')->each(
} function (Crawler $node, $i) {
return $deal; return [ sprintf('setImage%s', $i) => $node->attr('data-popin-content')];
} }
);
/** }
* @return int foreach ($images as $image) {
*/ foreach ($image as $method => $uri) {
public function getId() $deal->$method($uri);
{ }
return $this->id; }
} return $deal;
}
/**
* @param int $id /**
* @return Deal * @return int
*/ */
public function setId($id) public function getId()
{ {
$this->id = $id; return $this->id;
return $this; }
}
/**
/** * @param int $id
* @return int * @return Deal
*/ */
public function getCategory() public function setId($id)
{ {
return $this->category; $this->id = $id;
} return $this;
}
/**
* @param int $category /**
* @return Deal * @return int
*/ */
public function setCategory($category) public function getCategory()
{ {
$this->category = $category; return $this->category;
return $this; }
}
/**
/** * @param int $category
* @return string * @return Deal
*/ */
public function getType() public function setCategory($category)
{ {
return $this->type; $this->category = $category;
} return $this;
}
/**
* @param string $type /**
* @return Deal * @return string
*/ */
public function setType($type) public function getType()
{ {
$this->type = $type; return $this->type;
return $this; }
}
/**
/** * @param string $type
* @return string * @return Deal
*/ */
public function getSubject() public function setType($type)
{ {
return $this->subject; $this->type = $type;
} return $this;
}
/**
* @param string $subject /**
* @return Deal * @return string
*/ */
public function setSubject($subject) public function getSubject()
{ {
$this->subject = $subject; return $this->subject;
return $this; }
}
/**
/** * @param string $subject
* @return string * @return Deal
*/ */
public function getBody() public function setSubject($subject)
{ {
return $this->body; $this->subject = $subject;
} return $this;
}
/**
* @param string $body /**
* @return Deal * @return string
*/ */
public function setBody($body) public function getBody()
{ {
$this->body = $body; return $this->body;
return $this; }
}
/**
/** * @param string $body
* @return float * @return Deal
*/ */
public function getPrice() public function setBody($body)
{ {
return $this->price; $this->body = $body;
} return $this;
}
/**
* @param float $price /**
* @return Deal * @return float
*/ */
public function setPrice($price) public function getPrice()
{ {
$this->price = $price; return $this->price;
return $this; }
}
/**
/** * @param float $price
* @return mixed * @return Deal
*/ */
public function getImage0() public function setPrice($price)
{ {
return $this->image0; $this->price = $price;
} return $this;
}
/**
* @param mixed $image0 /**
* @return Deal * @return mixed
*/ */
public function setImage0($image0) public function getImage0()
{ {
$this->image0 = $image0; return $this->image0;
return $this; }
}
/**
/** * @param mixed $image0
* @return mixed * @return Deal
*/ */
public function getImage1() public function setImage0($image0)
{ {
return $this->image1; $this->image0 = $image0;
} return $this;
}
/**
* @param mixed $image1 /**
* @return Deal * @return mixed
*/ */
public function setImage1($image1) public function getImage1()
{ {
$this->image1 = $image1; return $this->image1;
return $this; }
}
/**
/** * @param mixed $image1
* @return mixed * @return Deal
*/ */
public function getImage2() public function setImage1($image1)
{ {
return $this->image2; $this->image1 = $image1;
} return $this;
}
/**
* @param mixed $image2 /**
* @return Deal * @return mixed
*/ */
public function setImage2($image2) public function getImage2()
{ {
$this->image2 = $image2; return $this->image2;
return $this; }
}
/**
/** * @param mixed $image2
* @return Account * @return Deal
*/ */
public function getAccount() public function setImage2($image2)
{ {
return $this->account; $this->image2 = $image2;
} return $this;
}
/**
* @param Account $account /**
* @return Deal * @return \DateTime
*/ */
public function setAccount($account) public function getDateCreation()
{ {
$this->account = $account; return $this->datecreation;
return $this; }
}
/**
private function toJSON() * @param \DateTime $price
{ * @return Deal
$reflection = new \ReflectionClass($this); */
$props = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED); public function setDateCreation($datecreation)
$json = []; {
foreach ($props as $prop) { $this->datecreation = $datecreation;
$method = sprintf('get%s', ucfirst($prop->getName())); return $this;
$json[$prop->getName()] = $this->$method(); }
}
return \json_encode($json); /**
} * @return Account
*/
/** public function getAccount()
* @param string $dir {
* return $this->account;
* @return bool }
*/
public function save($dir) /**
{ * @param Account $account
try { * @return Deal
$save_dir = sprintf('%s/%s', $dir, $this->id); */
if (!is_dir($save_dir)) { public function setAccount($account)
mkdir($save_dir); {
} $this->account = $account;
file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON()); return $this;
foreach (range(0,2) as $i) { }
$image = sprintf('image%u',$i);
if (empty($this->$image)) { private function toJSON()
break; {
} $reflection = new \ReflectionClass($this);
$ch = curl_init($this->$image); $props = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
$fp = fopen(sprintf('%s/%s.jpg', $save_dir, $image), 'wb'); $json = [];
curl_setopt($ch, CURLOPT_FILE, $fp); foreach ($props as $prop) {
curl_setopt($ch, CURLOPT_HEADER, 0); $method = sprintf('get%s', ucfirst($prop->getName()));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $json[$prop->getName()] = $this->$method();
curl_exec($ch); }
curl_close($ch); return \json_encode($json);
fclose($fp); }
}
} catch(\Exception $e) { /**
return false; * @param string $dir
} *
return true; * @return bool
} */
public function save($dir)
/** {
* @param string $json path to json try {
* $save_dir = sprintf('%s/%s', $dir, $this->id);
* @return Deal if (!is_dir($save_dir)) {
*/ mkdir($save_dir);
public static function fromJSON($json) }
{ file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON());
$json = \json_decode(file_get_contents($json), true); foreach (range(0, 2) as $i) {
$deal = new self(); $image = sprintf('image%u', $i);
foreach ($json as $property => $value) { if (empty($this->$image)) {
$method = sprintf('set%s', ucfirst($property)); break;
$deal->$method($value); }
} $ch = curl_init($this->$image);
return $deal; $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 string $json path to json
*
* @return Deal
*/
public static function fromJSON($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);
}
return $deal;
}
}