-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_feeds.php
143 lines (122 loc) · 4.95 KB
/
update_feeds.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
<?php
$config = include('config.php');
if (isset($config['readerSettings'])) {
$readerSettings = $config['readerSettings'];
$webUrl = $readerSettings['web_url'];
$entriesPerFeed = $readerSettings['how_many'];
$refreshTime = $readerSettings['how_often_to_run'];
$opmlPath = $readerSettings['opml_path'];
$defaultIcon = $readerSettings['default_icon'];
} else {
die('Error: Configuration is missing or invalid.');
}
$absoluteOpmlPath = $_SERVER['DOCUMENT_ROOT'] . $opmlPath;
function fetchFeedsFromOpml($absoluteOpmlPath) {
echo $absoluteOpmlPath;
$feeds = [];
if (file_exists($absoluteOpmlPath)) {
$opml = simplexml_load_file($absoluteOpmlPath);
foreach ($opml->body->outline as $outline) {
if (isset($outline->outline)) {
foreach ($outline->outline as $feed) {
$feeds[] = (string) $feed['xmlUrl'];
}
} else {
$feeds[] = (string) $outline['xmlUrl'];
}
}
} else {
echo "<br>OPML file not found.<br>";
}
return $feeds;
}
function fetchLatestEntries($feedUrls, $defaultIcon, $entriesPerFeed, $webUrl) {
$entries = [];
$agent = 'User-Agent: PixeeFeeds 1.0 via ' . $webUrl;
$options = [
'http' => [
'header' => $agent,
'ignore_errors' => true
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
];
$context = stream_context_create($options);
foreach ($feedUrls as $feedUrl) {
$feedContent = @file_get_contents($feedUrl, false, $context);
if ($feedContent !== false) {
$xml = simplexml_load_string($feedContent);
if ($xml === false) {
error_log("Failed to parse feed: $feedUrl");
continue;
}
if (isset($xml->channel->item)) {
// RSS feed
$websiteTitle = (string) $xml->channel->title;
$websiteUrl = (string) $xml->channel->link;
$icon = (string) $xml->channel->image->url;
if (empty($icon)) {
$icon = $defaultIcon;
}
$feedEntries = [];
foreach ($xml->channel->item as $item) {
$feedEntries[] = [
'title' => (string) $item->title,
'link' => (string) $item->link,
'description' => strip_tags((string) $item->description),
'pubDate' => strtotime((string) $item->pubDate),
'author' => isset($item->author) ? (string) $item->author : 'Unknown',
'websiteTitle' => $websiteTitle,
'websiteUrl' => $websiteUrl,
'icon' => $icon
];
}
usort($feedEntries, function($a, $b) {
return $b['pubDate'] - $a['pubDate'];
});
$entries = array_merge($entries, array_slice($feedEntries, 0, $entriesPerFeed));
} elseif (isset($xml->entry)) {
// Atom feed
$websiteTitle = (string) $xml->title;
$websiteUrl = (string) $xml->link['href'];
$icon = $defaultIcon; // Atom feeds do not have an image element in the same way RSS feeds do
$feedEntries = [];
foreach ($xml->entry as $entry) {
$feedEntries[] = [
'title' => (string) $entry->title,
'link' => (string) $entry->link['href'],
'description' => strip_tags((string) $entry->content),
'pubDate' => strtotime((string) $entry->updated),
'author' => isset($entry->author->name) ? (string) $entry->author->name : 'Unknown',
'websiteTitle' => $websiteTitle,
'websiteUrl' => $websiteUrl,
'icon' => $icon
];
}
usort($feedEntries, function($a, $b) {
return $b['pubDate'] - $a['pubDate'];
});
$entries = array_merge($entries, array_slice($feedEntries, 0, $entriesPerFeed));
} else {
error_log("Feed format not recognized: $feedUrl");
}
} else {
error_log("Failed to load feed: $feedUrl");
}
}
usort($entries, function($a, $b) {
return $b['pubDate'] - $a['pubDate'];
});
return $entries;
}
$feeds = fetchFeedsFromOpml($absoluteOpmlPath);
$latestEntries = fetchLatestEntries($feeds, $defaultIcon, $entriesPerFeed, $webUrl);
$feedDataFile = 'feed_data.json';
if (file_put_contents($feedDataFile, json_encode($latestEntries, JSON_PRETTY_PRINT))) {
echo "<br>Feed data updated successfully.";
} else {
echo "<br>Failed to update feed data.";
}
?>