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

@ -29,6 +29,8 @@ class Deal
protected $image1; protected $image1;
/** @var string */ /** @var string */
protected $image2; protected $image2;
/** @var \DateTime */
protected $datecreation;
/** /**
* Deal constructor. * Deal constructor.
@ -50,6 +52,13 @@ class Deal
$deal->setType(self::TYPE_OFFER); $deal->setType(self::TYPE_OFFER);
$deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()); $deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text());
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content')); $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) { if ($crawler->filter('.item_photo')->count() > 0) {
$script = $crawler ->filter('.item_photo')->first()->nextAll() $script = $crawler ->filter('.item_photo')->first()->nextAll()
->filter('script')->first()->html(); ->filter('script')->first()->html();
@ -57,7 +66,7 @@ class Deal
$urls = $urls[1]; $urls = $urls[1];
$images = []; $images = [];
foreach ($urls as $i => $url) { foreach ($urls as $i => $url) {
$images[] = [sprintf('setImage%s', $i) => str_replace('thumb','large', $url)]; $images[] = [sprintf('setImage%s', $i) => str_replace('thumb', 'large', $url)];
} }
} else { } else {
$images = $crawler->filter('[data-popin-content]')->each( $images = $crawler->filter('[data-popin-content]')->each(
@ -236,6 +245,24 @@ class Deal
return $this; return $this;
} }
/**
* @return \DateTime
*/
public function getDateCreation()
{
return $this->datecreation;
}
/**
* @param \DateTime $price
* @return Deal
*/
public function setDateCreation($datecreation)
{
$this->datecreation = $datecreation;
return $this;
}
/** /**
* @return Account * @return Account
*/ */
@ -279,8 +306,8 @@ class Deal
mkdir($save_dir); mkdir($save_dir);
} }
file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON()); file_put_contents(sprintf('%s/%s', $save_dir, 'data.json'), $this->toJSON());
foreach (range(0,2) as $i) { foreach (range(0, 2) as $i) {
$image = sprintf('image%u',$i); $image = sprintf('image%u', $i);
if (empty($this->$image)) { if (empty($this->$image)) {
break; break;
} }
@ -293,7 +320,7 @@ class Deal
curl_close($ch); curl_close($ch);
fclose($fp); fclose($fp);
} }
} catch(\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
return true; return true;