-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
85 lines (72 loc) · 3.01 KB
/
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
import discord
import feedparser
import os
from dotenv import load_dotenv
import yaml
import asyncio
load_dotenv()
intents = discord.Intents.all()
client = discord.Client(intents=intents)
# Information needed for discord authentication
DISCORD_CHANNEL_IDS = list(map(int, os.getenv('DISCORD_CHANNEL_IDS').split(',')))
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
RSS_FEED_URLS = os.getenv('RSS_FEED_URLS').split(",")
EMOJI = "\U0001F4F0" # Newspaper emoji
sent_articles_file = "sent_articles.yaml"
async def fetch_feed(channel):
# Load the seen IDs from the file, or create an empty dictionary
if os.path.exists(sent_articles_file):
with open(sent_articles_file, "r") as f:
sent_articles = yaml.safe_load(f)
print("Loaded YAML object")
else:
sent_articles = {}
print("Created new empty dictionary")
for rss_feed_url in RSS_FEED_URLS:
# Parse the RSS feed
print("Parsing RSS feed...")
feed = feedparser.parse(rss_feed_url)
# Check if the feed was parsed successfully
if feed.bozo:
print(f"Error parsing RSS feed: {feed.bozo_exception}")
continue
last_entry = feed.entries[0]
if channel.id not in sent_articles:
sent_articles[channel.id] = []
if last_entry.link not in sent_articles[channel.id]:
article_title = last_entry.title
article_link = last_entry.link
sent_articles[channel.id].append(last_entry.link)
print(f"New article: {article_title}")
print(f"Link: {article_link}")
try:
# Send the article link to the channel
await channel.send(f"{EMOJI} | {article_title}\n\n{article_link}")
print("Article sent to channel successfully")
except discord.Forbidden:
print("Error: Insufficient permissions to send messages to the channel")
except discord.HTTPException as e:
print(f"Error sending message to the channel: {e}")
print(f"Parsing complete for {rss_feed_url}")
while True:
try:
with open(sent_articles_file, "w") as f:
yaml.dump(sent_articles, f, default_flow_style=False, sort_keys=False)
break # Exit the loop if the file was written successfully
except Exception as e:
print(f"Error writing seen IDs to file: {e}")
await asyncio.sleep(1) # Wait for 1 second before trying again
@client.event
async def on_ready():
print(f"Bot logged in as {client.user.name}")
while True:
for channel_id in DISCORD_CHANNEL_IDS:
# Get the desired channel object
channel = client.get_channel(channel_id)
print(f"Target channel: {channel.name} (ID: {channel.id})")
# Fetch the RSS feed
await fetch_feed(channel)
await asyncio.sleep(600) # Wait for 60 seconds before fetching again
# Start the bot
print("Starting the bot...")
client.run(DISCORD_BOT_TOKEN)