1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zrashwani\NewsScrapper\Adapters; |
4
|
|
|
|
5
|
|
|
use \Symfony\Component\DomCrawler\Crawler; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Adapter to extract news base on microdata format base on schema.org specifications |
9
|
|
|
* @link http://schema.org/Article schema.org NewsArticle specification |
10
|
|
|
* @author Zeid Rashwani <zrashwani.com> |
11
|
|
|
*/ |
12
|
|
|
class MicrodataAdapter extends AbstractAdapter |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Crawler $crawler |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function extractTitle(Crawler $crawler) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$ret = null; |
22
|
|
|
|
23
|
|
|
$crawler->filterXPath('//*[@itemprop="headline"]') |
24
|
|
|
->each( |
25
|
|
|
function(Crawler $node) use (&$ret) { |
26
|
|
|
$ret = trim($node->text()); |
27
|
|
|
} |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
return $ret; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function extractImage(Crawler $crawler) |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
$ret = $this->getSrcByImgSelector($crawler, '//img[@itemprop="image"]'); |
38
|
|
|
return $ret; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function extractDescription(Crawler $crawler) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$ret = null; |
44
|
|
|
|
45
|
|
|
$crawler->filterXPath('//*[@itemprop="description"]') |
46
|
|
|
->each( |
47
|
|
|
function(Crawler $node) use (&$ret) { |
48
|
|
|
if ($node->nodeName() === 'meta') { |
49
|
|
|
$ret = trim($node->attr('content')); |
50
|
|
|
} else { |
51
|
|
|
$ret = trim($node->text()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return $ret; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* extract keywords out of crawler object |
61
|
|
|
* @param Crawler $crawler |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function extractKeywords(Crawler $crawler) |
65
|
|
|
{ |
66
|
|
|
$ret = array(); |
67
|
|
|
|
68
|
|
|
$crawler->filterXPath('//*[@itemprop="keywords"]') |
69
|
|
|
->each( |
70
|
|
View Code Duplication |
function(Crawler $node) use (&$ret) { |
|
|
|
|
71
|
|
|
if ($node->nodeName() === 'meta') { |
72
|
|
|
$keyword_txt = trim($node->attr('content')); |
73
|
|
|
} else { |
74
|
|
|
$keyword_txt = trim($node->text()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (empty($keyword_txt) !== true) { |
78
|
|
|
$ret = explode(',', $keyword_txt); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $ret; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function extractBody(Crawler $crawler) |
87
|
|
|
{ |
88
|
|
|
$ret = ''; |
89
|
|
|
|
90
|
|
|
$crawler->filterXPath('//*[@itemprop="articleBody"]') |
91
|
|
|
->each( |
92
|
|
|
function(Crawler $node) use (&$ret) { |
93
|
|
|
$ret .= $node->html(); |
94
|
|
|
} |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if (empty($ret) === true) { |
98
|
|
|
$article_types = ['Article', 'NewsArticle', 'Report', 'ScholarlyArticle', |
99
|
|
|
'MedicalScholarlyArticle', 'SocialMediaPosting', |
100
|
|
|
'BlogPosting', 'LiveBlogPosting', |
101
|
|
|
'DiscussionForumPosting', 'TechArticle', |
102
|
|
|
'APIReference']; |
103
|
|
|
|
104
|
|
|
foreach ($article_types as $article_type) { |
105
|
|
|
$crawler->filterXPath( |
106
|
|
|
"//*[@itemtype='http://schema.org/$article_type']" |
107
|
|
|
) |
108
|
|
|
->each( |
109
|
|
|
function(Crawler $node) use (&$ret) { |
110
|
|
|
$ret .= $node->html(); |
111
|
|
|
} |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
if (empty($ret) === false) { //if content found, exit loop |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$ret = $this->normalizeHtml($ret); |
121
|
|
|
|
122
|
|
|
return $ret; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function extractPublishDate(Crawler $crawler) |
126
|
|
|
{ |
127
|
|
|
$date_str = null; |
128
|
|
|
|
129
|
|
|
$crawler->filterXPath('//*[@itemprop="datePublished"]') |
130
|
|
|
->each( |
131
|
|
|
function(Crawler $node) use (&$date_str) { |
132
|
|
|
if ($node->nodeName() === 'meta') { |
133
|
|
|
$date_str = $node->attr('content'); |
134
|
|
|
} elseif ($node->attr('datetime')) { |
|
|
|
|
135
|
|
|
$date_str = $node->attr('datetime'); |
136
|
|
|
} else { |
137
|
|
|
$date_str = $node->text(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
if (!is_null($date_str)) { |
143
|
|
|
//remove extra unneeded suffix from date |
144
|
|
|
$date_str = str_replace('ET', '', $date_str); |
145
|
|
|
$ret = new \DateTime($date_str); |
146
|
|
|
return $ret->format(\DateTime::ISO8601); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $date_str; //null |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function extractAuthor(Crawler $crawler) |
153
|
|
|
{ |
154
|
|
|
$ret = null; |
155
|
|
|
$crawler->filterXPath( |
156
|
|
|
'//*[@itemprop="author" '. |
157
|
|
|
'and @itemtype="http://schema.org/Person"]//*[@itemprop="name"]' |
158
|
|
|
) |
159
|
|
|
->each( |
160
|
|
|
function(Crawler $node) use (&$ret) { |
161
|
|
|
$ret = $node->text(); |
162
|
|
|
} |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
if (is_null($ret)) { |
166
|
|
|
$crawler->filterXPath('//*[@itemprop="author"]') |
167
|
|
|
->each( |
168
|
|
|
function(Crawler $node) use (&$ret) { |
169
|
|
|
if ($node->nodeName() === 'meta') { |
170
|
|
|
$ret = $node->attr('content'); |
171
|
|
|
} else { |
172
|
|
|
$ret = $node->text(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
$ret = preg_replace('@\s{2,}@', ' ', $ret); |
178
|
|
|
|
179
|
|
|
return $ret; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.