-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowForum.py
More file actions
271 lines (213 loc) · 13.3 KB
/
ShowForum.py
File metadata and controls
271 lines (213 loc) · 13.3 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
267
268
269
270
271
import discord
import asyncio
from discord.ext import commands
from io import StringIO
from html.parser import HTMLParser
from datetime import date
import rss
import Filtering
today = date.today()
#Key-Value Pairs for the Feed, so the user can set multiple Feeds at once
#Structure {Course.id : Forum.id}
rssdata = {}
def get_RSSData():
return rssdata
# strip html tags and only get the needed values inside
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs = True
self.text = StringIO()
def handle_data(self, d):
self.text.write(d)
def get_data(self):
return self.text.getvalue()
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
intervalTime = 10
class ShowForum(commands.Cog):
def __init__(self, client):
self.client = client
# listens to the rss-feed and refreshes it permanently
# posts new entries from the rss-feed after receiving them
# @initial startup wont post entries from the past
# posts one notification for each user in the Filtering-Lists
# checks the blacklist and keywords. If the post contains anything from the blacklist it will not be posted
# If the post contains anything from the keyword list the text will be bold and in a different colour
# If there a no Filterlists it will just post the Notifications without modification
# Author: Sven & Lennart
async def listen(ctx):
IDsUsed = []
listening = True
while (listening):
if not rssdata:
listening = False
else:
for entry in get_RSSData():
course_id = int(entry)
forum_id = int(rssdata.get(course_id))
feed = rss.refreshFeed(course_id, forum_id)
feed.reverse()
for entry_in_feed in feed:
if entry_in_feed.id in IDsUsed:
continue
else:
IDsUsed.append(entry_in_feed.id)
if entry_in_feed.published_parsed.tm_year < date.today().year or \
(
entry_in_feed.published_parsed.tm_year == date.today().year and entry_in_feed.published_parsed.tm_mon < date.today().month) or \
(
entry_in_feed.published_parsed.tm_mon == date.today().month and entry_in_feed.published_parsed.tm_mday < date.today().day):
continue
else:
author = strip_tags(entry_in_feed.summary).replace(u'\xa0', u'').split(".")[0]
message = strip_tags(entry_in_feed.summary).replace(u'\xa0', u'').split(".")[1]
users = Filtering.getUsers()
if not users:
noti = discord.Embed(color=0x990000)
noti.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
noti.add_field(name=entry_in_feed.title + " " + author,
value="```" + message + "```" + "\n" + entry_in_feed.published + "\n" + "[Direktlink]({})".format(
entry_in_feed.link), inline=False)
noti.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"),
icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=noti)
else:
for user in users:
pairs = Filtering.getPairs()
blist = Filtering.getBlacklist()
post = True
for x in blist[pairs.get(user)]:
if x in entry_in_feed.title or x in author or x in message:
post = False
if post:
klist = Filtering.getKeywordlist()
mark = False
for x in klist[pairs.get(user)]:
if x in entry_in_feed.title or x in author or x in message:
mark = True
if mark:
await ctx.send("Neue Benachrichtigung für <@{}>".format(user))
noti = discord.Embed(color=0x990000)
noti.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
noti.add_field(name=entry_in_feed.title + " " + author,
value="```yaml\n**" + message + "**```" + "\n" + entry_in_feed.published + "\n" + "[Direktlink]({})".format(
entry_in_feed.link), inline=False)
noti.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"),
icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=noti)
else:
await ctx.send("Neue Benachrichtigung für <@{}>".format(user))
noti = discord.Embed(color=0x990000)
noti.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
noti.add_field(name=entry_in_feed.title + " " + author,
value="```" + message + "```" + "\n" + entry_in_feed.published + "\n" + "[Direktlink]({})".format(
entry_in_feed.link), inline=False)
noti.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"),
icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=noti)
await asyncio.sleep(intervalTime)
# Command to add a new Feed to the bot it will listen to
# This also auto starts the listen fuction
# Author: Sven
@client.command()
async def new_feed(ctx, *args):
try:
if len(args) != 2:
raise ValueError(
"Please give me two numbers first the ID of the Course and then the ID of the Forum.\n"
"e.g. https://isis.tu-berlin.de/rss/file.php/<COURSE ID>/<YOUR SECURITYKEY>/mod_forum/<FORUM ID>/rss.xml")
course = args[0]
forum = args[1]
if not course.isnumeric() or not forum.isnumeric():
raise ValueError(
"You provided at least one wrong input. Please provide two Integer as an input.")
rssdata[course] = forum
success = discord.Embed(title="", color=0x990000)
success.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
success.add_field(name="New Feed", value="You did set up a new RSS Feed!", inline=False)
success.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=success, delete_after=10.0)
await listen(ctx)
except ValueError as e:
warn = discord.Embed(title="", color=0x990000)
warn.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
warn.add_field(name="Wrong Input", value=e, inline=False)
warn.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=warn, delete_after=20.0)
# Command to add a new Feed to the bot it will listen to
# This also auto starts the listen fuction
# Author: Sven
@client.command()
async def remove_feed(ctx, *args):
try:
if len(args) != 2:
raise ValueError(
"Please give me two numbers first the ID of the Course and then the ID of the Forum.\n"
"e.g. https://isis.tu-berlin.de/rss/file.php/<COURSE ID>/<YOUR SECURITYKEY>/mod_forum/<FORUM ID>/rss.xml")
course = args[0]
forum = args[1]
if not course.isnumeric() or not forum.isnumeric():
raise ValueError(
"You provided at least one wrong input. Please provide two Integer as an input.")
rssdata.pop(course, forum)
success = discord.Embed(title="", color=0x990000)
success.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
success.add_field(name="Removed Feed", value="You did remove a RSS Feed!", inline=False)
success.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=success, delete_after=10.0)
except ValueError as e:
warn = discord.Embed(title="", color=0x990000)
warn.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
warn.add_field(name="Wrong Input", value=e, inline=False)
warn.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=warn, delete_after=20.0)
# gives user the opportunity to set the refresh interval of Isi.
# reacts to !set_interval_to <Int> <[h, min, sec]>
# checks if enough and the right arguments are given
# if arguments are incorrect, it displays an error message which disappears after 10 seconds
# if arguments are correct, user will get a verification message
#
# CURRENTLY ONLY FOR OWNER
# Author: Lennart
@client.command()
@commands.is_owner()
async def set_interval_to(ctx, *args):
try:
if len(args) != 2:
raise ValueError(
"Please give a number and a unit of time [h, min or sec] for your interval (e.g. **!set_interval_to 60 min**)")
time = args[0]
unit = args[1]
if not time.isnumeric():
raise ValueError(
"You provided a " + str(type(time)) + " for the time value, please provide an Integer.")
if unit != "sec" and unit != "min" and unit != "h":
raise ValueError("Please provide the right unit (h, min, sec)")
global intervalTime
if (unit == "sec"):
intervalTime = int(time)
unit = "seconds"
elif (unit == "min"):
intervalTime = int(time) * 60
unit = "minutes"
elif (args[1] == "h"):
intervalTime = int(time) * 360
unit = "hours"
changeSuccess = discord.Embed(title="Isi will now check for new Forum entries every " + time + " " + unit, color=0xFFD300)
changeSuccess.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
changeSuccess.add_field(name="Warning", value="Due to restrictions by moodle and the RSS feed, Isi can only recieve the latest ten entries. \n If the forum Isi is listening to has a lot of traffic, you might miss out on some of the entries.", inline=False)
changeSuccess.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=changeSuccess)
except ValueError as e:
warn = discord.Embed(title="", color=0x990000)
warn.set_thumbnail(url="https://i.imgur.com/TBr8R7L.png")
warn.add_field(name="Wrong Input", value=e, inline=False)
warn.set_footer(text="ISIS Bot v0.1 • " + date.today().strftime("%d/%m/%y"), icon_url="https://i.imgur.com/s8Ni2X1.png")
await ctx.send(embed=warn, delete_after=20.0)
def setup(client):
client.add_cog(ShowForum(client))