-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_process.php
More file actions
executable file
·266 lines (226 loc) · 5.81 KB
/
rss_process.php
File metadata and controls
executable file
·266 lines (226 loc) · 5.81 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/php
<?php
/*
* rss_process.php -
*
* Process (fetch, parse, and store) a list of RSS feeds.
* The list can come from a file or from an SQS queue.
*
* Copyright 2009-2010 Amazon.com, Inc. or its affiliates. All Rights
* Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You
* may not use this file except in compliance with the License. A copy
* of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license.txt" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the
* License.
*
* Modified by Jeffrey S. Haemer <[email protected]>
*/
//error_reporting(E_ALL);
require_once('AWSSDKforPHP/sdk.class.php');
require_once('include/book.inc.php');
require_once ('include/magpierss/rss_fetch.inc');
// Tell the RSS parser to disable caching of feed data
define('MAGPIE_CACHE_ON', 0);
// Create the feed queue, if necessary, and grab its URL
$sqs = new AmazonSQS();
$res = $sqs->create_queue(FEED_QUEUE);
if ($res->isOK())
{
$feedQueueUrl = urlFromQueueObject($res);
}
$doFile = false;
$doQueue = false;
// Check arguments and determine mode
if (($argc != 2) ||
(($argv[1] != '-f') && ($argv[1] != '-q')))
{
exit("Usage:\n".
$argv[0] . " -f\n" .
$argv[0] . " -q\n");
}
switch ($argv[1])
{
case '-f':
$doFile = true;
break;
case '-q':
$doQueue = true;
break;
}
// Create the SimpleDB and access objects
$sdb = new AmazonSDB();
$sqs = new AmazonSQS();
// Create array of interesting fields at feed level
$feedFields = array('link',
'title',
'pubdate',
'tagline',
'language',
'generator',
'description');
// Create array of interesting fields at item level
$itemFields = array('guid',
'link',
'title',
'description');
// Create array of possible fields for item key, in descending order of preference
$itemKeyFields = array('guid',
'link',
'title');
// Process file argument
if ($doFile)
{
// Get list of feeds to process
$urls = file(FEEDS);
print("Begin processing " . count($urls) . " feeds\n");
foreach ($urls as $url)
{
$url = trim($url);
if (updateFeed($sdb, $url))
{
print($url . " - updated.\n");
}
else
{
print($url . " - not updated.\n");
}
}
}
// Process queue argument
if ($doQueue)
{
while (true)
{
$message = pullMessage($sqs, $feedQueueUrl);
if ($message != null)
{
$messageDetail = $message['MessageDetail'];
$receiptHandle = (string)$message['ReceiptHandle'];
$url = $messageDetail['FeedURL'];
if (updateFeed($sdb, $url))
{
print($url . " - updated.\n");
}
else
{
print($url . " - not updated.\n");
}
// Delete the message
$sqs->delete_message($feedQueueUrl, $receiptHandle);
}
}
}
/*
* updateFeed -
*
* Fetch the latest version of the RSS from the given URL.
* Return true on success, false on error.
*
* If the feed cannot be fetched, update the BOOK_FEED_DOMAIN
* with the current date and time and a status of FEED_NO_FETCH.
*
* If the feed can be fetched, update the BOOK_FEED_DOMAIN
* with the current date and time, a status of FEED_YES_FETCH,
* and values for any fields set in FeedFields. Also update
* the BOOK_FIELD_ITEM_DOMAIN, with one item for each item
* in the feed, using any values in the fields named in
* ItemFields.
*/
function updateFeed($sdb, $url)
{
global $stats;
global $feedFields;
global $itemFields;
global $itemKeyFields;
// Fetch the RSS
$rss = fetch_rss($url);
// Handle success or failure
if ($rss !== false)
{
// Prepare to update BOOK_FEED_DOMAIN
$key = $url;
$attrs = array('feed_url' => $url,
'fetch_date' => date('c'),
'status' => FEED_YES_FETCH);
foreach ($feedFields as $field)
{
if (isset($rss->channel[$field]) && ($rss->channel[$field] != ''))
{
$attrs[$field] = $rss->channel[$field];
}
}
// Update BOOK_FEED_DOMAIN
$res = $sdb->put_attributes(BOOK_FEED_DOMAIN, $key, $attrs, true);
// Check status
if (!$res->isOK())
{
return false;
}
// Update BOOK_FEED_ITEM_DOMAIN for each item in the feed
foreach ($rss->items as $item)
{
$attrs = array();
foreach ($itemFields as $field)
{
if (isset($item[$field]) && ($item[$field] != ''))
{
$attrs[$field] = $item[$field];
}
}
// Find a field to use as a key
$itemKey = null;
foreach ($itemKeyFields as $field)
{
if (isset($item[$field]) && ($item[$field] != ''))
{
$itemKey = $item[$field];
break;
}
}
// Use the md5 hash of all fields as a last resort
if ($itemKey == null)
{
$all = '';
foreach ($attrs as $key => $value)
{
$all .= $key . '_' . $value . '__';
}
$key = md5($all);
}
// Update BOOK_FEED_ITEM_DOMAIN
$res = $sdb->put_attributes(BOOK_FEED_ITEM_DOMAIN, $itemKey, $attrs, true);
// Check status
if (!$res->isOK())
{
return false;
}
}
return true;
}
else
{
// Prepare to update BOOK_FEED_DOMAIN
$key = $url;
$attrs = array('feed_url' => $url,
'fetch_date' => date('c'),
'status' => FEED_NO_FETCH);
// Update BOOK_FEED_DOMAIN
$res = $sdb->put_attributes(BOOK_FEED_DOMAIN, $key, $attrs, true);
// Check status
if (!$res->isOK())
{
return false; // We failed at failing
}
// Signify fetch failure
return false;
}
}
?>