This repository was archived by the owner on Mar 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrss.php
More file actions
95 lines (81 loc) · 2.27 KB
/
rss.php
File metadata and controls
95 lines (81 loc) · 2.27 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
<?php
/**
* Created by PhpStorm.
* User: fengchang
* Date: 2017/6/7
* Time: 下午10:49
*/
require 'config.php';
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use FeedWriter\ATOM;
date_default_timezone_set('Asia/Hong_Kong');
$log = new Logger('readhub-rss');
$log->pushHandler(new RotatingFileHandler(LOG_PATH, LOG_KEEP_DAYS, Logger::INFO));
$redis = null;
if (ENABLE_CACHE) {
$redis = new Redis();
$redis->pconnect(REDIS_HOST, REDIS_PORT);
}
$feedItems = [];
if (isset($_GET['channel'])) {
if ($_GET['channel'] === 'all')
$channels = array_keys(CHANNEL_CONFIG);
else
$channels = explode(",", $_GET['channel']);
} else {
$channels = ['topics'];
}
foreach ($channels as $channel) {
if (!array_key_exists($channel, CHANNEL_CONFIG))
continue;
$channelItems = [];
if (ENABLE_CACHE) {
$channelJson = $redis->get($channel);
if ($channelJson !== FALSE) {
$channelItems = unserialize($channelJson);
$log->addInfo("CACHE: GET channel $channel from Redis");
}
}
if (empty($channelItems)) {
$className = CHANNEL_CONFIG[$channel]['className'];
$api = new $className;
$channelItems = $api->getData();
if (ENABLE_CACHE) {
$redis->set($channel, serialize($channelItems), CACHE_EXPIRE);
$log->addInfo("CACHE: SET channel $channel to Redis");
}
}
$feedItems = array_merge($feedItems, $channelItems);
}
usort($feedItems, "compareItem");
$feed = initFeed();
foreach ($feedItems as $item)
$feed->addItem($item);
$feed->printFeed();
/**
* @return ATOM
*/
function initFeed()
{
$feed = new ATOM();
$feed->setTitle('Readbub');
$feed->setDescription('A RSS feed for Readhub');
$feed->setLink('https://readhub.me/');
$feed->setDate(new DateTime());
$feed->setImage('https://cdn.readhub.me/static/assets/png/readhub_logo.png');
$feed->setChannelElement('author', 'fengchang@bayescafe.com');
$feed->setSelfLink(RSS_URL);
return $feed;
}
function compareItem($a, $b)
{
$aDate = $a->getElements()['updated'];
$bDate = $b->getElements()['updated'];
if ($aDate == $bDate) {
return 0;
} else {
return $aDate < $bDate ? 1 : 0;
}
}