|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pilipinews\Website\Bulletin; |
|
4
|
|
|
|
|
5
|
|
|
use Pilipinews\Common\Article; |
|
6
|
|
|
use Pilipinews\Common\Client; |
|
7
|
|
|
use Pilipinews\Common\Crawler as DomCrawler; |
|
8
|
|
|
use Pilipinews\Common\Interfaces\ScraperInterface; |
|
9
|
|
|
use Pilipinews\Common\Scraper as AbstractScraper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Manila Bulletin Scraper |
|
13
|
|
|
* |
|
14
|
|
|
* @package Pilipinews |
|
15
|
|
|
* @author Rougin Gutib <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Scraper extends AbstractScraper implements ScraperInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $removables = array( |
|
23
|
|
|
'.uk-article-title', |
|
24
|
|
|
'.share-container', |
|
25
|
|
|
'.uk-grid.uk-grid-large.uk-margin-bottom', |
|
26
|
|
|
'.uk-visible-small.uk-margin-top.uk-margin-bottom', |
|
27
|
|
|
'#related_post', |
|
28
|
|
|
'#disqus_thread', |
|
29
|
|
|
'script', |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the contents of an article. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $link |
|
36
|
|
|
* @return \Pilipinews\Common\Article |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function scrape($link) |
|
39
|
|
|
{ |
|
40
|
6 |
|
$this->prepare((string) $link); |
|
41
|
|
|
|
|
42
|
6 |
|
$title = $this->title('h2.title'); |
|
43
|
|
|
|
|
44
|
6 |
|
$this->remove($this->removables); |
|
45
|
|
|
|
|
46
|
6 |
|
$body = $this->body('.article-content'); |
|
47
|
|
|
|
|
48
|
6 |
|
$body = $this->image($body); |
|
49
|
|
|
|
|
50
|
6 |
|
$body = $this->slidenav($body); |
|
51
|
|
|
|
|
52
|
6 |
|
$html = $this->html($body); |
|
53
|
|
|
|
|
54
|
6 |
|
return new Article($title, $html, $link); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Converts image elements to readable string. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Pilipinews\Common\Crawler $crawler |
|
61
|
|
|
* @return \Pilipinews\Common\Crawler |
|
62
|
|
|
*/ |
|
63
|
2 |
|
protected function image(DomCrawler $crawler) |
|
64
|
|
|
{ |
|
65
|
4 |
|
$callback = function (DomCrawler $crawler) |
|
66
|
|
|
{ |
|
67
|
6 |
|
$result = $crawler->filter('img')->first(); |
|
68
|
|
|
|
|
69
|
6 |
|
$image = (string) $result->attr('src'); |
|
70
|
|
|
|
|
71
|
6 |
|
$text = $crawler->filter('figcaption')->first(); |
|
72
|
|
|
|
|
73
|
6 |
|
$message = $image . ' - ' . $text->html(); |
|
74
|
|
|
|
|
75
|
6 |
|
$message = str_replace('<br>', ' ', $message); |
|
76
|
|
|
|
|
77
|
6 |
|
return '<p>PHOTO: ' . $message . '</p>'; |
|
78
|
6 |
|
}; |
|
79
|
|
|
|
|
80
|
6 |
|
return $this->replace($crawler, '.wp-block-image', $callback); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Initializes the crawler instance. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $link |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
6 |
|
protected function prepare($link) |
|
90
|
|
|
{ |
|
91
|
6 |
|
$response = Client::request((string) $link); |
|
92
|
|
|
|
|
93
|
6 |
|
$regex = '/<p>Tags:(.*?)<\/p>/i'; |
|
94
|
|
|
|
|
95
|
6 |
|
$html = preg_replace($regex, '', $response); |
|
96
|
|
|
|
|
97
|
6 |
|
$this->crawler = new DomCrawler((string) $html); |
|
98
|
6 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Converts an slidenav element into a readable string. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Pilipinews\Common\Crawler $crawler |
|
104
|
|
|
* @return \Pilipinews\Common\Crawler |
|
105
|
|
|
*/ |
|
106
|
2 |
|
protected function slidenav(DomCrawler $crawler) |
|
107
|
|
|
{ |
|
108
|
|
|
$callback = function (DomCrawler $crawler) |
|
109
|
|
|
{ |
|
110
|
|
|
$items = $crawler->filter('img'); |
|
111
|
|
|
|
|
112
|
|
|
$items = $items->each(function ($crawler) |
|
113
|
|
|
{ |
|
114
|
|
|
$link = 'https://mb.com.ph'; |
|
115
|
|
|
|
|
116
|
|
|
$image = $link . $crawler->attr('src'); |
|
117
|
|
|
|
|
118
|
|
|
return '<p>PHOTO: ' . $image . '</p>'; |
|
119
|
|
|
}); |
|
120
|
|
|
|
|
121
|
|
|
return implode("\n\n", (array) $items); |
|
122
|
2 |
|
}; |
|
123
|
|
|
|
|
124
|
2 |
|
$class = '.uk-slidenav-position'; |
|
125
|
|
|
|
|
126
|
2 |
|
return $this->replace($crawler, $class, $callback); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|