add env variables for subreddit and post_limit

This commit is contained in:
2026-02-20 20:38:51 +01:00
parent 07b6abcdca
commit 3a3c251fcc
2 changed files with 15 additions and 12 deletions

View File

@@ -1,4 +1,6 @@
# Generate Token in Fluxer Settings under "Developer" # Generate Token in Fluxer Settings under "Developer"
FLUXER_TOKEN= FLUXER_TOKEN=
INTERVAL=3600 # 1 hour INTERVAL=3600 # 1 hour
SUBREDDIT=ich_iel
POST_LIMIT=20 # How many posts to fetch from reddit each time

23
main.py
View File

@@ -25,8 +25,9 @@ async def post_reddit_periodically():
await asyncio.sleep(int(os.getenv("INTERVAL", 3600))) await asyncio.sleep(int(os.getenv("INTERVAL", 3600)))
async def get_latest_post(subreddit): async def get_latest_post(subreddit):
url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit=20" post_limit = os.getenv("POST_LIMIT", 20)
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.1)"} url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit={post_limit}"
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.3)"}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code == 200: if response.status_code == 200:
if response.headers.get("Content-Type", "").startswith("application/json"): if response.headers.get("Content-Type", "").startswith("application/json"):
@@ -80,7 +81,7 @@ async def setChannel(message):
con = sqlite3.connect('data/ich_iel-bot.db') con = sqlite3.connect('data/ich_iel-bot.db')
cur = con.cursor() cur = con.cursor()
cur.execute("INSERT OR REPLACE INTO channels (guild_id, channel_id) VALUES (?, ?)", (guild_id, channel_id)) cur.execute("INSERT OR REPLACE INTO channels (guild_id, channel_id) VALUES (?, ?)", (guild_id, channel_id))
con.connection.commit() con.commit()
await message.channel.send(f"Channel set to {channel_id}") await message.channel.send(f"Channel set to {channel_id}")
except sqlite3.Error as e: except sqlite3.Error as e:
await message.channel.send(f"Database error: {e}") await message.channel.send(f"Database error: {e}")
@@ -91,19 +92,19 @@ async def setChannel(message):
@bot.command() @bot.command()
async def version(message): async def version(message):
await message.channel.send("Version 0.2 is running") await message.channel.send("Version 0.3 is running")
async def post_reddit(): async def post_reddit():
subreddit = "ich_iel" subreddit = os.getenv("SUBREDDIT", "ich_iel")
posts = await get_latest_post(subreddit) posts = await get_latest_post(subreddit)
if not posts: if not posts:
print("No image posts found.") print("No image posts found.")
return return
try: try:
con = sqlite3.connect('data/ich_iel-bot.db') con = sqlite3.connect('data/ich_iel-bot.db')
con = con.cursor() cur = con.cursor()
con.execute("SELECT guild_id, channel_id FROM channels") cur.execute("SELECT guild_id, channel_id FROM channels")
rows = con.fetchall() rows = cur.fetchall()
if not rows: if not rows:
print("No channels set") print("No channels set")
return return
@@ -114,7 +115,7 @@ async def post_reddit():
if not post_id_match: if not post_id_match:
continue continue
post_id = post_id_match.group(1) post_id = post_id_match.group(1)
is_posted = con.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone() is_posted = cur.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone()
if is_posted: if is_posted:
print(f"Post {post_id} already posted in guild {guild_id}, skipping.") print(f"Post {post_id} already posted in guild {guild_id}, skipping.")
continue continue
@@ -122,8 +123,8 @@ async def post_reddit():
channel = await bot.fetch_channel(int(channel_id)) channel = await bot.fetch_channel(int(channel_id))
if channel: if channel:
await channel.send(f"{title}\nOriginal post: {image_url}") await channel.send(f"{title}\nOriginal post: {image_url}")
con.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id)) cur.execute("INSERT OR REPLACE INTO posted (guild_id, post_id) VALUES (?, ?)", (guild_id, post_id))
con.connection.commit() con.commit()
print(f"Posted to channel {channel_id} in guild {guild_id}") print(f"Posted to channel {channel_id} in guild {guild_id}")
break break
else: else: