-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ModifyBridge.php
181 lines (168 loc) · 5.98 KB
/
ModifyBridge.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class ModifyBridge extends FeedExpander
{
const MAINTAINER = 'Mynacol';
const NAME = 'Modify Feed';
const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'Modifies a feed of your choice with regexes';
const URI = 'https://github.com/RSS-Bridge/rss-bridge';
const PARAMETERS = [[
'url' => [
'name' => 'Feed URL',
'type' => 'text',
'exampleValue' => 'https://lorem-rss.herokuapp.com/feed?unit=day',
'required' => true,
],
'title_pattern' => [
'name' => 'Title find pattern (regular expression!)',
'type' => 'text',
'exampleValue' => 'Unwanted part in title',
'required' => false,
],
'title_replacement' => [
'name' => 'Title replacement for the find pattern',
'type' => 'text',
'exampleValue' => '${0}',
'required' => false,
],
'author_pattern' => [
'name' => 'Author find pattern (regular expression!)',
'type' => 'text',
'exampleValue' => '^(author)\s*|\s*publisher$',
'required' => false,
],
'author_replacement' => [
'name' => 'Author replacement for the find pattern',
'type' => 'text',
'exampleValue' => '${1}',
'required' => false,
],
'content_pattern' => [
'name' => 'Content find pattern (regular expression!)',
'type' => 'text',
'exampleValue' => '(content)\s+advertisement\s+(content)',
'required' => false,
],
'content_replacement' => [
'name' => 'Content replacement for the find pattern',
'type' => 'text',
'exampleValue' => '${1} ${2}',
'required' => false,
],
'uri_pattern' => [
'name' => 'URI/URL find pattern (regular expression!)',
'type' => 'text',
'exampleValue' => '^https?://(.*)/(.*)$',
'required' => false,
],
'uri_replacement' => [
'name' => 'URI/URL replacement for the find pattern',
'type' => 'text',
'exampleValue' => 'https://${1}/foo/${2}',
'required' => false,
],
'enclosure_pattern' => [
'name' => 'Enclosure URI/URL find pattern (regular expression!)',
'type' => 'text',
'exampleValue' => '^https?://(.*)/(.*)$',
'required' => false,
],
'enclosure_replacement' => [
'name' => 'Enclosure URI/URL replacement for the find pattern',
'type' => 'text',
'exampleValue' => 'https://${1}/foo/${2}',
'required' => false,
],
'case_insensitive' => [
'name' => 'Case-insensitive find patterns',
'type' => 'checkbox',
'required' => false,
],
]];
public function collectData()
{
$url = $this->getInput('url');
if (!Url::validate($url)) {
throw new \Exception('The url parameter must either refer to http or https protocol.');
}
$this->collectExpandableDatas($this->getURI());
}
protected function parseItem(array $item)
{
// Title
$pattern = $this->buildPattern($this->getInput('title_pattern'));
$replacement = $this->getInput('title_replacement');
$res = preg_replace($pattern, $replacement, $item['title']);
if ($res !== null) {
$item['title'] = $res;
}
// Author
$pattern = $this->buildPattern($this->getInput('author_pattern'));
$replacement = $this->getInput('author_replacement');
$res = preg_replace($pattern, $replacement, $item['author']);
if ($res !== null) {
$item['author'] = $res;
}
// Content
$pattern = $this->buildPattern($this->getInput('content_pattern'));
$replacement = $this->getInput('content_replacement');
$res = preg_replace($pattern, $replacement, $item['content']);
if ($res !== null) {
$item['content'] = $res;
}
// URI
$pattern = $this->buildPattern($this->getInput('uri_pattern'));
$replacement = $this->getInput('uri_replacement');
$res = preg_replace($pattern, $replacement, $item['uri']);
if ($res !== null) {
$item['uri'] = $res;
}
// Enclosures
if (array_key_exists('enclosures', $item)) {
$pattern = $this->buildPattern($this->getInput('enclosure_pattern'));
$replacement = $this->getInput('enclosure_replacement');
foreach ($item['enclosures'] as $key => $val) {
$res = preg_replace($pattern, $replacement, $val);
if ($res !== null) {
$item['enclosures'][$key] = $res;
}
}
}
if (array_key_exists('enclosure', $item)) {
$pattern = $this->buildPattern($this->getInput('enclosure_pattern'));
$replacement = $this->getInput('enclosure_replacement');
$res = preg_replace($pattern, $replacement, $item['enclosure']['url']);
if ($res !== null) {
$item['enclosure']['url'] = $res;
}
}
return $item;
}
private function buildPattern($pattern)
{
if (! str_contains($pattern, '#')) {
$delimiter = '#';
} elseif (! str_contains($pattern, '/')) {
$delimiter = '/';
} else {
throw new \Exception('Cannot use both / and # inside filter');
}
$regex = $delimiter . $pattern . $delimiter;
if ($this->getInput('case_insensitive')) {
$regex .= 'i';
}
return $regex;
}
public function getURI()
{
$url = $this->getInput('url');
if ($url) {
return $url;
}
return parent::getURI();
}
public function getName()
{
return parent::getName();
}
}