-
Notifications
You must be signed in to change notification settings - Fork 4
/
PatreonRSS.php
216 lines (196 loc) · 5.9 KB
/
PatreonRSS.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// set your creator ID here - you have to figure it out from the patreon HTML source code
$CREATOR_ID = '3764669';
/**
* Class PatreonRSS
*
* Very simple class to fetch posts from Patreon and create an RSS feed from it
*
* A bit hacky and cuold be improved a lot, but works
*/
class PatreonRSS
{
/** @var array which fields to include in the response, for now we don't need much */
protected $fields = array(
'post' =>
array(
'post_type',
'title',
'content',
//'comment_count',
//'min_cents_pledged_to_view',
//'like_count',
//'post_file',
//'image',
//'thumbnail_url',
//'embed',
//'is_paid',
'published_at',
'url',
//'pledge_url',
//'patreon_url',
//'current_user_has_liked',
//'patron_count',
//'current_user_can_view',
//'current_user_can_delete',
//'upgrade_url',
),
'user' =>
array(
'image_url',
'full_name',
'url',
)
);
/** @var array haven't really played with those, except the creator id */
protected $filter = array(
'is_by_creator' => true,
'is_following' => false,
'creator_id' => 'set by constructor',
'contains_exclusive_posts' => true
);
/**
* PatreonRSS constructor.
* @param string $id
*/
public function __construct($id)
{
$this->filter['creator_id'] = $id;
}
/**
* Output the RSS directly to the browser
*/
public function rss()
{
$data = $this->getData();
echo '<?xml version="1.0"?>';
echo '<rss version="2.0">';
echo '<channel>';
$this->printRssChannelInfo($data['campaign'], $data['user']);
foreach ($data['posts'] as $item) {
$this->printRssItem($item);
}
echo '</channel>';
echo '</rss>';
}
/**
* Output the RSS but use a cache
*
* Note: this does absolutely no error checking and will just ignore errors. You have
* to make sure the given $dir exists and is writable. Otherwise there will be no caching
*
* @param string $dir directory in which to store cache files - has to be writable
* @param int $maxage maximum age for the cache in seconds
*/
public function cachedRSS($dir, $maxage)
{
$cachefile = $dir.'/'.$this->filter['creator_id'].'.xml';
$lastmod = @filemtime($cachefile);
if(time() - $lastmod < $maxage) {
echo file_get_contents($cachefile);
return;
}
ob_start();
$this->rss();
$rss = ob_get_clean();
@file_put_contents($cachefile, $rss); // we just ignore any errors
echo $rss;
}
/**
* Constructs the URL based on the fields and filter config at the top
*
* @return string
*/
protected function getURL()
{
$url = 'https://api.patreon.com/stream?json-api-version=1.0';
foreach ($this->fields as $type => $set) {
$url .= '&fields[' . $type . ']=' . rawurlencode(join(',', $set));
}
foreach ($this->filter as $key => $val) {
if ($val === true) $val = 'true';
if ($val === false) $val = 'false';
$url .= '&filter[' . $key . ']=' . $val;
}
$url .= '&page[cursor]=null';
return $url;
}
/**
* Fetches the data from Patreon and cleans it up for our usecase
*
* @return array
*/
protected function getData()
{
$url = $this->getURL();
$json = file_get_contents($url);
$data = json_decode($json, true);
$clean = array(
'posts' => array(),
'user' => array(),
'campaign' => array()
);
foreach ($data['data'] as $item) {
$clean['posts'][] = $item['attributes'];
}
foreach ($data['included'] as $item) {
if ($item['type'] == 'user') {
$clean['user'] = $item['attributes'];
$clean['user']['id'] = $item['id'];
continue;
}
if ($item['type'] == 'campaign') {
$clean['campaign'] = $item['attributes'];
$clean['campaign']['id'] = $item['id'];
}
}
return $clean;
}
/**
* Print a single post as RSS item
*
* @param array $item
*/
protected function printRssItem($item)
{
echo '<item>';
echo '<title>';
echo htmlspecialchars($item['title']);
echo '</title>';
echo '<description>';
echo htmlspecialchars($item['content']);
echo '</description>';
echo '<link>';
echo htmlspecialchars($item['url']);
echo '</link>';
echo '<guid>';
echo htmlspecialchars($item['url']);
echo '</guid>';
echo '<pubDate>';
echo date('r', strtotime($item['published_at']));
echo '</pubDate>';
echo '</item>';
}
/**
* Print the channel info from our campaign and user data
*
* @param array $campaign
* @param array $user
*/
protected function printRssChannelInfo($campaign, $user)
{
echo '<title>';
echo htmlspecialchars($campaign['creation_name'] . ' Patreon Posts');
echo '</title>';
echo '<description>';
echo htmlspecialchars(strip_tags($campaign['summary']));
echo '</description>';
echo '<link>';
echo htmlspecialchars($user['url']);
echo '</link>';
}
}
// main
header('Content-Type: application/rss+xml');
$patreon = new PatreonRSS($CREATOR_ID);
$patreon->cachedRSS(__DIR__, 60*60); // cache for an hour