/
Craigslist.php
136 lines (119 loc) · 3.98 KB
/
Craigslist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace app\models\glabs\objects;
use app\commands\GlabsController;
use PHPHtmlParser\Dom;
use PHPHtmlParser\Exceptions\CurlException;
/**
* Class of objects of craigslist.org.
*
* @package glabs
* @author Nikolaj Rudakov <nnrudakov@gmail.com>
* @copyright 2016
*/
class Craigslist extends BaseObject
{
/**
* @var bool
*/
public $parseDescription = true;
/**
* @inheritdoc
*/
protected function setTitle()
{
if (!$this->title || 'none' === $this->title) {
/* @var \PHPHtmlParser\Dom\AbstractNode $title */
if ($title = self::$dom->find('#titletextonly', 0)) {
$this->title = $title->text();
}
}
}
/**
* @inheritdoc
*/
protected function setDescription()
{
/* @var \PHPHtmlParser\Dom\AbstractNode $postingbody */
$postingbody = self::$dom->find('#postingbody');
/* @var \PHPHtmlParser\Dom\AbstractNode $contact */
// "click" to show contact
if ($this->parseDescription && $contact = $postingbody->find('.showcontact', 0)) {
try {
$description = GlabsController::$curl->get(
'http://' . parse_url($this->url, PHP_URL_HOST) . $contact->getAttribute('href')
);
if (false !== strpos($description, 'g-recaptcha')) {
throw new ObjectException('Showed Google captcha.');
}
$this->description = $description;
} catch (CurlException $e) {
throw new ObjectException('Could not get contacts (' . $e->getMessage() . ').');
}
} else {
$this->description = $postingbody->innerHtml() . ' ' . $this->data['description'];
}
return true;
}
/**
* @inheritdoc
*/
public function setPrice($node = null)
{
/* @var \PHPHtmlParser\Dom\AbstractNode $node */
/* @var \PHPHtmlParser\Dom\AbstractNode $price */
$price = $node !== null ? $node->find('.price', 0) : self::$dom->find('.price', 0);
if ($price) {
$this->price = $price->text();
} else {
if (preg_match('/\$(\d+)/', $this->title, $matches)) {
$this->price = $matches[1];
} else if (preg_match('/(\d+)\$/', $this->title, $matches)) {
$this->price = $matches[1];
}
}
$this->price = str_replace('$', '', $this->price);
if (!$this->price) {
throw new ObjectException('There is no price in object.');
}
}
/**
* @inheritdoc
*/
protected function setImages()
{
/* @var \PHPHtmlParser\Dom\AbstractNode $figure */
$figure = self::$dom->find('figure', 0);
if (!$figure) {
throw new ObjectException('Has no files.');
}
$is_multiimage = false !== strpos($figure->getAttribute('class'), 'multiimage');
if ($is_multiimage) {
/* @var \PHPHtmlParser\Dom\AbstractNode $link */
foreach ($figure->find('a') as $link) {
if (count($this->subimage) >= 4) {
break;
}
try {
$image = new Image(['url' => $link->getAttribute('href'), 'object_url' => $this->url]);
} catch (ImageException $e) {
continue;
}
if (!$this->thumbnail) {
$this->thumbnail = $image;
} else {
$this->subimage[] = $image;
}
}
if (!$this->thumbnail) {
throw new ObjectException('Has no files.');
}
} else {
try {
$this->thumbnail = new Image(['url' => $figure->find('img', 0)->getAttribute('src')]);
} catch (ImageException $e) {
throw new ObjectException('Has no files.');
}
}
return true;
}
}