This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-feed-main.py
185 lines (162 loc) · 8.22 KB
/
rss-feed-main.py
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
#---------------------------------------------------------------------------------------------------
# Bibliotheken die Importiert wurden
#---------------------------------------------------------------------------------------------------
from bs4 import BeautifulSoup
import requests
from Discord_Verarbeitung import *
import discord
from discord.ext import commands
import asyncio
import xmltodict
aus = 0
wdhl_datum_liste = ['allgemein', 'datein', 'allgemein_info']
#---------------------------------------------------------------------------------------------------
# Abfrage des RSS-FEEDs
#--------------------------------------------------------------------------------------------------
def aktueller_rss_feed(url):
schleife = True
while schleife == True:
try:
feed_liste = []
url = requests.get(url)
soup = BeautifulSoup(url.content, 'xml')
feeds = soup.find_all('item')
if len(feeds) > 0:
for x in range(len(feeds)):
obj = xmltodict.parse(str((feeds[x])))
if 'title' in obj['item']:
title = feeds[x].title.text
else:
title = None
if 'link' in obj['item'] and len(str(feeds[x].link.text)) < 100:
try:
with requests.Session() as s:
s.get(str(feeds[x].link.t))
link = feeds[x].link.text
except:
link = 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400'
else:
link = 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400'
if 'description' in obj['item'] and len(str(feeds[x].description.text)) < 3200:
discription = feeds[x].description.text
else:
discription = None
if 'author' in obj['item']:
author = feeds[x].author.text
else:
author = None
if 'pubDate' in obj['item']:
datum = feeds[x].pubDate.text
else:
datum = None
feed = [title, link, discription, author, datum]
feed_liste.append(feed)
else:
feed_liste = [-2, 'leer']
return feed_liste
except Exception as fail:
return [-1, fail]
#---------------------------------------------------------------------------------------------------
# Abfrage der IDs der Nachrichten
#----------------------------------------------------------------------------------------
async def messages_rss(messageNummer, channel):
messageCounter = 0
async for o in channel.history():
if messageCounter == messageNummer:
return o.id
messageCounter += 1
intents = discord.Intents.default()
client = commands.Bot(
command_prefix="$",
help_command=None,
intents=intents
)
#---------------------------------------------------------------------------------------------------
# Start vom Bot
#--------------------------------------------------------------------------------------------------
@client.event
async def on_connect():
print("[Ich habe mich eingeloggt]...")
client.loop.create_task(change_status())
client.loop.create_task(rss_discord_senden())
#---------------------------------------------------------------------------------------------------
# Main, verarbeitung der RSS-FEEDs
#--------------------------------------------------------------------------------------------------
async def rss_discord_senden():
global aus
if (aus == 0):
aus = 1
await client.wait_until_ready()
channel_fail = client.get_channel(channel_number('fail_id'))
await channel_fail.purge()
global wdhl_datum_liste
url_rss_datein = #url
url_rss_allgemein = #url
url_rss_allgemein_info = #url
message_channels = [client.get_channel(channel_number('rss_allgemein_id')), client.get_channel(channel_number('rss_datein_id')), client.get_channel(channel_number('rss_allgemein_info_id'))]
while True:
try:
feed_liste = [aktueller_rss_feed(url_rss_allgemein), aktueller_rss_feed(url_rss_datein), aktueller_rss_feed(url_rss_allgemein_info)]
for n in range(len(feed_liste)):
if feed_liste[n][0] == -1:
channel_fail = client.get_channel(channel_number('fail_id'))
await channel_fail.send('Fail bei Abfrage von Rss-Feed:')
await channel_fail.send(feed_liste[n][1])
elif feed_liste[n][0] == -2:
continue
else:
for x in range(len(feed_liste[n])):
if x <= 50:
try:
embed_feed = discord.Embed(title=f"Title: {feed_liste[n][x][0]}",
url=f'{feed_liste[n][x][1]}',
description=f'Discription: {feed_liste[n][x][2]}',
author=f'author:{feed_liste[n][x][3]}',
color=0x11ff00)
embed_feed.set_footer(text=f"Datum: {feed_liste[n][x][4]}")
if x == 0:
if feed_liste[n][x][3] != wdhl_datum_liste[n]:
message_counter = 0
async for o in message_channels[n].history():
message_counter = message_counter + 1
if message_counter > 50:
await message_channels[n].purge(limit=message_counter - 50)
wdhl_datum_liste[n] = feed_liste[n][x][3]
await message_channels[n].send(embed=embed_feed)
else:
variable_eingabe = await message_channels[n].fetch_message(await messages_rss(x, message_channels[n]))
await variable_eingabe.edit(embed=embed_feed)
except Exception as fail_senden:
print(fail_senden)
print(feed_liste[n][x])
await asyncio.sleep(60 * 2)
except Exception as fail:
try:
channel_fail = client.get_channel(channel_number('fail_id'))
await channel_fail.send('Fail bei Abfrage:')
await channel_fail.send(fail)
await asyncio.sleep(60 * 2)
except:
pass
#---------------------------------------------------------------------------------------------------
# Veränderung des Status des Bots
#--------------------------------------------------------------------------------------------------
async def change_status():
while True:
try:
await client.change_presence(activity=discord.Activity(
type=discord.ActivityType.playing,
name="sende RSS Daten!",
status=discord.Status.online))
await asyncio.sleep(7.5)
await client.change_presence(activity=discord.Activity(
type=discord.ActivityType.listening,
name="RSS Daten!",
status=discord.Status.do_not_disturb))
await asyncio.sleep(7.5)
except:
pass
#---------------------------------------------------------------------------------------------------
# Token vom Bot der eingesetzt werden muss
#--------------------------------------------------------------------------------------------------
client.run("Bot-Token")