API/library/Deal.php
2016-08-24 22:30:13 +02:00

350 lines
8.4 KiB
PHP

<?php
namespace Shikiryu\LBCReposter;
class Deal
{
const TYPE_OFFER = 's';
const TYPE_ASK = 'k';
public static $categories = [
'33' => 'Offres d\'emploi',
'2' => 'Voitures',
'3' => 'Motos',
'4' => 'Caravaning',
'5' => 'Utilitaires',
'6' => 'Equipement Auto',
'44' => 'Equipement Moto',
'50' => 'Equipement Caravaning',
'7' => 'Nautisme',
'51' => 'Equipement Nautisme',
'9' => 'Ventes immobilières',
'10' => 'Locations',
'11' => 'Colocations',
'13' => 'Bureaux & Commerces',
'12' => 'Locations & Gîtes',
'67' => 'Chambres d\'hôtes',
'68' => 'Campings',
'69' => 'Hôtels',
'70' => 'Hébergements insolites',
'15' => 'Informatique',
'43' => 'Consoles & Jeux vidéo',
'16' => 'Image & Son',
'17' => 'Téléphonie',
'19' => 'Ameublement',
'20' => 'Electroménager',
'45' => 'Arts de la table',
'39' => 'Décoration',
'46' => 'Linge de maison',
'21' => 'Bricolage',
'52' => 'Jardinage',
'22' => 'Vêtements',
'53' => 'Chaussures',
'47' => 'Accessoires & Bagagerie',
'42' => 'Montres & Bijoux',
'23' => 'Equipement bébé',
'54' => 'Vêtements bébé',
'25' => 'DVD / Films',
'26' => 'CD / Musique',
'27' => 'Livres',
'28' => 'Animaux',
'55' => 'Vélos',
'29' => 'Sports & Hobbies',
'30' => 'Instruments de musique',
'40' => 'Collection',
'41' => 'Jeux & Jouets',
'48' => 'Vins & Gastronomie',
'57' => 'Matériel Agricole',
'58' => 'Transport - Manutention',
'59' => 'BTP - Chantier Gros-oeuvre',
'60' => 'Outillage - Matériaux 2nd-oeuvre',
'32' => 'Équipements Industriels',
'61' => 'Restauration - Hôtellerie',
'62' => 'Fournitures de Bureau',
'63' => 'Commerces & Marchés',
'64' => 'Matériel Médical',
'34' => 'Prestations de services',
'35' => 'Billetterie',
'49' => 'Evénements',
'36' => 'Cours particuliers',
'65' => 'Covoiturage',
'38' => 'Autres',
];
/** @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;
protected $image0;
protected $image1;
protected $image2;
/**
* Deal constructor.
* @param int|null $id
*/
public function __construct($id = null)
{
$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($crawler->filter('.item_imageCaption')->first()->text());
$deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), self::$categories));
$deal->setType(self::TYPE_OFFER);
$deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text());
$deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
$images = $crawler->filter('[data-popin-content]')->each(
function ($node, $i) {
return [ sprintf('setImage%s', $i) => $node->attr('data-popin-content')];
}
);
foreach ($images as $image) {
foreach ($image as $method => $uri) {
$deal->$method(sprintf('http:%s', $uri));
}
}
return $deal;
}
/**
* @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 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);
}
/**
* @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;
}
}