forked from VerifiedJoseph/rss-bridge-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheDailyBeastBridge.php
134 lines (109 loc) · 3.7 KB
/
TheDailyBeastBridge.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
<?php
class TheDailyBeastBridge extends BridgeAbstract {
const NAME = 'The Daily Beast Bridge';
const URI = 'https://www.thedailybeast.com';
const DESCRIPTION = 'Returns newest articles (summary only) by category, keyword or author';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = array(
'By Category' => array(
'c' => array(
'name' => 'Category',
'type' => 'text',
'required' => true,
'exampleValue' => 'us-news',
),
),
'By Keyword' => array(
'k' => array(
'name' => 'Keyword',
'type' => 'text',
'required' => true,
'exampleValue' => 'impeachment',
),
),
'By Author' => array(
'a' => array(
'name' => 'Author',
'type' => 'text',
'required' => true,
'exampleValue' => 'donald-kirk',
),
),
);
const CACHE_TIMEOUT = 3600; // 1 hour
private $categoryUrlRegex = '/thedailybeast\.com\/category\/([\w-]+)/';
private $keywordUrlRegex = '/thedailybeast\.com\/keyword\/([\w-]+)/';
private $authorUrlRegex = '/thedailybeast\.com\/author\/([\w-]+)/';
private $feedName = '';
public function detectParameters($url) {
$params = array();
if(preg_match($this->categoryUrlRegex, $url, $matches)) {
$params['context'] = 'By Category';
$params['c'] = $matches[1];
return $params;
}
if(preg_match($this->keywordUrlRegex, $url, $matches)) {
$params['context'] = 'By Keyword';
$params['k'] = $matches[1];
return $params;
}
if(preg_match($this->authorUrlRegex, $url, $matches)) {
$params['context'] = 'By Author';
$params['a'] = $matches[1];
return $params;
}
return null;
}
public function collectData() {
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request: ' . $this->getURI());
if ($html->find('h2.WrapHeader__title', 0)) {
$this->feedName = $html->find('h2.WrapHeader__title', 0)->plaintext;
}
if ($html->find('h4.Byline__name', 0)) { // By Author
$this->feedName = $html->find('h4.Byline__name', 0)->plaintext;
}
foreach ($html->find('article') as $article) {
$item = array();
$item['uri'] = $article->find('a', 0)->href;
$articleHtml = getSimpleHTMLDOMCached($item['uri'], 7200)
or returnServerError('Could not request: ' . $item['uri']);
$item['title'] = $articleHtml->find('meta[property="og:title"]', 0)->content;
if ($articleHtml->find('script[type="application/ld+json"]', 0)) {
$schemaData = json_decode($articleHtml->find('script[type="application/ld+json"]', 0)->innertext);
if (isset($schemaData->isAccessibleForFree)) {
$item['title'] .= ' [Paywall]';
}
}
$item['author'] = $articleHtml->find('meta[name="authors"]', 0)->content;
$item['timestamp'] = $articleHtml->find('meta[property="article:published_time"]', 0)->content;
$description = $articleHtml->find('meta[property="og:description"]', 0)->content;
$image = $articleHtml->find('meta[property="og:image"]', 0)->content;
$item['content'] = <<<EOD
<p><img src="{$image}"></p>
<p>{$description}<p><a href="{$item['uri']}">Read on thedailybeast.com</a>
EOD;
$categories = explode(',', $articleHtml->find('meta[name="keywords"]', 0)->content);
$item['categories'] = array_map('trim', $categories);
$item['enclosures'][] = $image;
$this->items[] = $item;
}
}
public function getURI() {
switch($this->queriedContext) {
case 'By Category':
return self::URI . '/category/' . $this->getInput('c');
case 'By Keyword':
return self::URI . '/keyword/' . $this->getInput('k');
case 'By Author':
return self::URI . '/author/' . $this->getInput('a');
default: return parent::getURI();
}
}
public function getName() {
if (!empty($this->feedName)) {
return $this->feedName . ' - The Daily Beast';
}
return parent::getName();
}
}