skip posts that were posted already

This commit is contained in:
2026-02-19 17:40:02 +01:00
parent 542ae9a35a
commit 53fcd0a12b

50
main.py
View File

@@ -5,6 +5,7 @@ import asyncio
from dotenv import load_dotenv
import sqlite3
import os
import re
# this is a bot which posts the latest image post from ich_iel
# the code probably sucks, but it works, so I don't care
@@ -24,7 +25,7 @@ async def post_reddit_periodically():
await asyncio.sleep(int(os.getenv("INTERVAL", 3600)))
async def get_latest_post(subreddit):
url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit=5"
url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit=20"
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.1)"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
@@ -32,21 +33,34 @@ async def get_latest_post(subreddit):
try:
data = response.json()
print(f"Fetched data from Reddit: {data}")
posts = []
if data["data"]["children"]:
for child in data["data"]["children"]:
post = child["data"]
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
print(f"Found image post: {post['title']} - {post['url']}")
return post["title"], post["url"]
posts.append((post["title"], post["url"]))
return posts
except (KeyError, json.JSONDecodeError):
return None, None
return []
else:
print(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
print(f"Response content: {response.text}")
return None, None
return []
else:
print(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")
return None, None
return []
async def init_db():
try:
con = sqlite3.connect('data/ich_iel-bot.db')
con = con.cursor()
con.execute("CREATE TABLE IF NOT EXISTS channels (guild_id INTEGER PRIMARY KEY, channel_id INTEGER)")
con.execute("CREATE TABLE IF NOT EXISTS posted (guild_id INTEGER, post_id VARCHAR(255) PRIMARY KEY)")
con.connection.commit()
print("Database initialized successfully")
except sqlite3.Error as e:
print(f"Database initialization error: {e}")
@bot.command()
async def setChannel(message):
@@ -60,7 +74,6 @@ async def setChannel(message):
try:
con = sqlite3.connect('data/ich_iel-bot.db')
con = con.cursor()
con.execute("CREATE TABLE IF NOT EXISTS channels (guild_id INTEGER PRIMARY KEY, channel_id INTEGER)")
con.execute("INSERT OR REPLACE INTO channels (guild_id, channel_id) VALUES (?, ?)", (guild_id, channel_id))
con.connection.commit()
await message.channel.send(f"Channel set to {channel_id}")
@@ -76,31 +89,48 @@ async def version(message):
await message.channel.send("Version 0.2 is running")
async def post_reddit():
subreddit = "ich_iel" # I guess I could make this editable through the env file, so it's not just a bot for ich_iel lol
title, image_url = await get_latest_post(subreddit)
if title and image_url:
subreddit = "ich_iel"
posts = await get_latest_post(subreddit)
if not posts:
print("No image posts found.")
return
try:
con = sqlite3.connect('data/ich_iel-bot.db') # i like sqlite
con = sqlite3.connect('data/ich_iel-bot.db')
con = con.cursor()
con.execute("SELECT guild_id, channel_id FROM channels")
rows = con.fetchall()
if not rows:
print("No channels set")
return
for title, image_url in posts:
post_id_match = re.search(r"\/([^\/]+)\.(jpg|png|jpeg|gif)$", image_url)
if not post_id_match:
continue
post_id = post_id_match.group(1)
for guild_id, channel_id in rows:
is_posted = con.execute("SELECT post_id FROM posted WHERE guild_id = ? AND post_id = ?", (guild_id, post_id)).fetchone()
if is_posted:
print(f"Post {post_id} already posted in guild {guild_id}, skipping.")
continue
try:
channel = await bot.fetch_channel(int(channel_id))
if channel:
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))
con.connection.commit()
print(f"Posted to channel {channel_id} in guild {guild_id}")
return
else:
print(f"Channel {channel_id} not found for guild {guild_id}")
except Exception as e:
print(f"Error sending to channel {channel_id}: {e}")
print("Alle gefundenen Posts wurden bereits gepostet.")
except sqlite3.Error as e:
print(f"Database error: {e}")
if __name__ == "__main__":
print("Starting bot...")
asyncio.run(init_db())
TOKEN = os.getenv("FLUXER_TOKEN")
if not TOKEN:
print("Error: FLUXER_TOKEN not found in environment variables")