|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use discord\discord; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class rssReader |
|
30
|
|
|
*/ |
|
31
|
|
|
class rssReader |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
public $config; |
|
34
|
|
|
public $db; |
|
35
|
|
|
public $discord; |
|
36
|
|
|
public $logger; |
|
37
|
|
|
public $guild; |
|
38
|
|
|
public $itemTitle; |
|
39
|
|
|
public $itemUrl; |
|
40
|
|
|
public $itemDate; |
|
41
|
|
|
public $strDate; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $config |
|
45
|
|
|
* @param $discord |
|
46
|
|
|
* @param $logger |
|
47
|
|
|
*/ |
|
48
|
|
|
public function init($config, $discord, $logger) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->config = $config; |
|
51
|
|
|
$this->discord = $discord; |
|
52
|
|
|
$this->logger = $logger; |
|
53
|
|
|
$this->guild = $config['bot']['guild']; |
|
54
|
|
|
$this->rssFeeds = $config['plugins']['rssReader']['rssFeeds']; |
|
|
|
|
|
|
55
|
|
|
$this->toDiscordChannel = $config['plugins']['rssReader']['channelID']; |
|
|
|
|
|
|
56
|
|
|
$lastCheck = getPermCache('rssLastChecked'); |
|
57
|
|
|
if (is_null($lastCheck)) { |
|
58
|
|
|
// Schedule it for right now if first run |
|
59
|
|
|
setPermCache('rssLastChecked', time() - 5); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
|
|
public function tick() |
|
67
|
|
|
{ |
|
68
|
|
|
$lastCheck = getPermCache('rssLastChecked'); |
|
69
|
|
|
$feeds = $this->rssFeeds; |
|
70
|
|
|
$toChannel = $this->toDiscordChannel; |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if ($lastCheck <= time()) { |
|
73
|
|
|
$this->logger->addInfo('rssReader: Checking RSS feeds for updated news..'); |
|
74
|
|
|
$this->getRss($feeds, $toChannel); |
|
75
|
|
|
setPermCache('rssLastChecked', time() + 900); |
|
76
|
|
|
$this->logger->addInfo('rssReader: All feeds checked..'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $feeds |
|
82
|
|
|
* @param $toChannel |
|
83
|
|
|
*/ |
|
84
|
|
|
|
|
85
|
|
|
private function getRss($feeds, $toChannel) |
|
86
|
|
|
{ |
|
87
|
|
|
foreach ($feeds as $rssUrl) { |
|
88
|
|
|
//Check that url is set |
|
89
|
|
|
if (empty($rssUrl) || is_null($rssUrl) || $rssUrl === 0 || $rssUrl === '0') { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$rss = new SimpleXMLElement($rssUrl, null, true); // XML parser |
|
94
|
|
|
if ($rss===null || !is_object($rss)) { |
|
95
|
|
|
$this->logger->addInfo("Failed to load xml file: {$rssUrl}"); |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
if (!is_object($rss->feed)) { |
|
99
|
|
|
$this->logger->addInfo("Feed is not an object at: {$rssUrl}"); |
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
$feedLink = $rss->feed->id; |
|
103
|
|
|
$latestTopicDate = getPermCache("rssFeed{$feedLink}"); |
|
104
|
|
|
|
|
105
|
|
|
//Check if feed has been checked before |
|
106
|
|
|
if ($latestTopicDate === NULL) { |
|
107
|
|
|
// Set date to now to avoid posting old articles |
|
108
|
|
|
setPermCache("rssFeed{$feedLink}", time()); |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
//Find item to check if feed is formatted |
|
113
|
|
|
$itemTitle = $rss->entry->title; |
|
114
|
|
|
$itemUrl = $rss->entry->id; |
|
115
|
|
|
$strDate = $rss->entry->published; |
|
116
|
|
|
|
|
117
|
|
|
$itemDate = strtotime($strDate); |
|
118
|
|
|
|
|
119
|
|
|
//Check to see if feed is formatted correctly |
|
120
|
|
|
if (is_null($itemTitle) || is_null($itemUrl) || is_null($itemDate)) { |
|
121
|
|
|
$this->logger->addInfo("rssReader: {$rssUrl} is not a properly formatted RSS feed. {$itemTitle} {$itemUrl} {$itemDate}"); |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
//Find item to post |
|
126
|
|
|
foreach ($rss->entry as $item) { |
|
127
|
|
|
//Get item details |
|
128
|
|
|
$itemTitle = $item->title; |
|
129
|
|
|
$itemUrl = $item->id; |
|
130
|
|
|
$itemPubbed = $item->published; |
|
131
|
|
|
$itemDate = strtotime($item->published); |
|
132
|
|
|
|
|
133
|
|
|
//Check if item is old |
|
134
|
|
|
if ($itemDate <= $latestTopicDate) { |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
//Set message |
|
139
|
|
|
$msg = "\n**{$itemTitle}**\n\n*{$itemPubbed}*\n{$itemUrl}"; |
|
140
|
|
|
|
|
141
|
|
|
//Send message |
|
142
|
|
|
$channelID = $toChannel; |
|
143
|
|
|
queueMessage($msg, $channelID, $this->guild); |
|
144
|
|
|
$this->logger->addInfo("rssReader: Item Queued {$itemTitle}"); |
|
145
|
|
|
if ($itemDate > getPermCache("rssFeed{$feedLink}")) { |
|
146
|
|
|
setPermCache("rssFeed{$feedLink}", $itemDate); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.