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 hAtom microformats specifications |
9
|
|
|
* @link http://microformats.org/wiki/hatom draft microformat specification |
10
|
|
|
* @author Zeid Rashwani <zrashwani.com> |
11
|
|
|
*/ |
12
|
|
|
class HAtomAdapter extends AbstractAdapter |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function extractTitle(Crawler $crawler) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$ret = null; |
17
|
|
|
|
18
|
|
|
$crawler->filter('.hentry .entry-title') |
19
|
|
|
->each( |
20
|
|
|
function(Crawler $node) use (&$ret) { |
21
|
|
|
$ret = $node->text(); |
22
|
|
|
} |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
return $ret; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function extractImage(Crawler $crawler) |
30
|
|
|
{ |
31
|
|
|
$ret = $this->getSrcByImgSelector($crawler, '.entry-thumbnail img'); |
32
|
|
|
return $ret; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function extractDescription(Crawler $crawler) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$ret = null; |
38
|
|
|
|
39
|
|
|
$crawler->filter('.hentry .entry-summary') |
40
|
|
|
->each( |
41
|
|
|
function(Crawler $node) use (&$ret) { |
42
|
|
|
$ret = $node->text(); |
43
|
|
|
} |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
return $ret; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function extractKeywords(Crawler $crawler) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$ret = array(); |
52
|
|
|
|
53
|
|
|
$crawler->filter('.hentry a[rel="tag"]') |
54
|
|
|
->each( |
55
|
|
|
function(Crawler $node) use (&$ret) { |
56
|
|
|
$ret[] = $node->text(); |
57
|
|
|
} |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $ret; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function extractBody(Crawler $crawler) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$ret = null; |
66
|
|
|
$crawler->filter(".hentry .entry-content") |
67
|
|
|
->each( |
68
|
|
|
function(Crawler $node) use (&$ret) { |
69
|
|
|
$ret = $this->normalizeHtml($node->html()); |
70
|
|
|
} |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $ret; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function extractPublishDate(Crawler $crawler) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$date_str = null; |
79
|
|
|
|
80
|
|
|
$crawler->filter('time.published, .hentry .entry-date') |
81
|
|
|
->each( |
82
|
|
|
function(Crawler $node) use (&$date_str) { |
83
|
|
|
$date_str = $node->attr('datetime'); |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if (!is_null($date_str)) { |
88
|
|
|
$ret = new \DateTime($date_str); |
89
|
|
|
return $ret->format(\DateTime::ISO8601); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function extractAuthor(Crawler $crawler) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$ret = null; |
96
|
|
|
$crawler->filter('.hentry .author.vcard') |
97
|
|
|
->each( |
98
|
|
|
function(Crawler $node) use (&$ret) { |
99
|
|
|
$ret = $node->text(); |
100
|
|
|
} |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return $ret; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.