1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zrashwani\NewsScrapper\Adapters; |
4
|
|
|
|
5
|
|
|
use \Symfony\Component\DomCrawler\Crawler; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Adapter to extract news base on parse.ly metadata specification specifications |
9
|
|
|
* @link https://www.parsely.com/docs/integration/metadata/jsonld.html parse.ly json-ld documentation |
10
|
|
|
* @link https://www.parsely.com/docs/integration/metadata/metatags.html parse.ly repeated meta tags documentation |
11
|
|
|
* @author Zeid Rashwani <zrashwani.com> |
12
|
|
|
*/ |
13
|
|
|
class ParselyAdapter extends AbstractAdapter |
14
|
|
|
{ |
15
|
|
View Code Duplication |
public function extractTitle(Crawler $crawler) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$ret = null; |
18
|
|
|
|
19
|
|
|
$crawler->filterXPath('//meta[@name="parsely-title"]') |
20
|
|
|
->each( |
21
|
|
|
function(Crawler $node) use (&$ret) { |
22
|
|
|
$ret = $node->attr('content'); |
23
|
|
|
} |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
return $ret; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function extractImage(Crawler $crawler) |
31
|
|
|
{ |
32
|
|
|
$ret = null; |
33
|
|
|
|
34
|
|
|
$crawler->filterXPath('//meta[@name="parsely-image-url"]') |
35
|
|
|
->each( |
36
|
|
|
function(Crawler $node) use (&$ret) { |
37
|
|
|
$ret = $node->attr('content'); |
38
|
|
|
} |
39
|
|
|
); |
40
|
|
|
if (empty($ret) === false) { |
41
|
|
|
$ret = $this->normalizeLink($ret); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $ret; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function extractDescription(Crawler $crawler) |
48
|
|
|
{ |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function extractKeywords(Crawler $crawler) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$ret = array(); |
55
|
|
|
|
56
|
|
|
$crawler->filterXPath('//meta[@name="parsely-tags"]') |
57
|
|
|
->each( |
58
|
|
|
function(Crawler $node) use (&$ret) { |
59
|
|
|
$ret = explode(',', $node->attr('content')); |
60
|
|
|
} |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return $ret; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function extractBody(Crawler $crawler) |
67
|
|
|
{ |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function extractPublishDate(Crawler $crawler) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$date_str = null; |
74
|
|
|
|
75
|
|
|
$crawler->filterXPath('//meta[@name="parsely-pub-date"]') |
76
|
|
|
->each( |
77
|
|
|
function(Crawler $node) use (&$date_str) { |
78
|
|
|
$date_str = $node->attr('content'); |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
if (!is_null($date_str)) { |
83
|
|
|
$ret = new \DateTime($date_str); |
84
|
|
|
return $ret->format(\DateTime::ISO8601); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function extractAuthor(Crawler $crawler) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$ret = null; |
91
|
|
|
$crawler->filterXPath('//meta[@name="parsely-author"]') |
92
|
|
|
->each( |
93
|
|
|
function(Crawler $node) use (&$ret) { |
94
|
|
|
$ret = $node->attr('content'); |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $ret; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.