skip posts that were posted already
This commit is contained in:
68
main.py
68
main.py
@@ -5,6 +5,7 @@ import asyncio
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
# this is a bot which posts the latest image post from ich_iel
|
# 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
|
# 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)))
|
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=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)"}
|
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.1)"}
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
@@ -32,21 +33,34 @@ async def get_latest_post(subreddit):
|
|||||||
try:
|
try:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
print(f"Fetched data from Reddit: {data}")
|
print(f"Fetched data from Reddit: {data}")
|
||||||
|
posts = []
|
||||||
if data["data"]["children"]:
|
if data["data"]["children"]:
|
||||||
for child in data["data"]["children"]:
|
for child in data["data"]["children"]:
|
||||||
post = child["data"]
|
post = child["data"]
|
||||||
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
|
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
|
||||||
print(f"Found image post: {post['title']} - {post['url']}")
|
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):
|
except (KeyError, json.JSONDecodeError):
|
||||||
return None, None
|
return []
|
||||||
else:
|
else:
|
||||||
print(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
|
print(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
|
||||||
print(f"Response content: {response.text}")
|
print(f"Response content: {response.text}")
|
||||||
return None, None
|
return []
|
||||||
else:
|
else:
|
||||||
print(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")
|
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()
|
@bot.command()
|
||||||
async def setChannel(message):
|
async def setChannel(message):
|
||||||
@@ -60,7 +74,6 @@ async def setChannel(message):
|
|||||||
try:
|
try:
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db')
|
con = sqlite3.connect('data/ich_iel-bot.db')
|
||||||
con = con.cursor()
|
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.execute("INSERT OR REPLACE INTO channels (guild_id, channel_id) VALUES (?, ?)", (guild_id, channel_id))
|
||||||
con.connection.commit()
|
con.connection.commit()
|
||||||
await message.channel.send(f"Channel set to {channel_id}")
|
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")
|
await message.channel.send("Version 0.2 is running")
|
||||||
|
|
||||||
async def post_reddit():
|
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
|
subreddit = "ich_iel"
|
||||||
title, image_url = await get_latest_post(subreddit)
|
posts = await get_latest_post(subreddit)
|
||||||
if title and image_url:
|
if not posts:
|
||||||
try:
|
print("No image posts found.")
|
||||||
con = sqlite3.connect('data/ich_iel-bot.db') # i like sqlite
|
return
|
||||||
con = con.cursor()
|
try:
|
||||||
con.execute("SELECT guild_id, channel_id FROM channels")
|
con = sqlite3.connect('data/ich_iel-bot.db')
|
||||||
rows = con.fetchall()
|
con = con.cursor()
|
||||||
if not rows:
|
con.execute("SELECT guild_id, channel_id FROM channels")
|
||||||
print("No channels set")
|
rows = con.fetchall()
|
||||||
return
|
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:
|
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:
|
try:
|
||||||
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))
|
||||||
|
con.connection.commit()
|
||||||
|
print(f"Posted to channel {channel_id} in guild {guild_id}")
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
print(f"Channel {channel_id} not found for guild {guild_id}")
|
print(f"Channel {channel_id} not found for guild {guild_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error sending to channel {channel_id}: {e}")
|
print(f"Error sending to channel {channel_id}: {e}")
|
||||||
except sqlite3.Error as e:
|
print("Alle gefundenen Posts wurden bereits gepostet.")
|
||||||
print(f"Database error: {e}")
|
except sqlite3.Error as e:
|
||||||
|
print(f"Database error: {e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Starting bot...")
|
print("Starting bot...")
|
||||||
|
asyncio.run(init_db())
|
||||||
TOKEN = os.getenv("FLUXER_TOKEN")
|
TOKEN = os.getenv("FLUXER_TOKEN")
|
||||||
if not TOKEN:
|
if not TOKEN:
|
||||||
print("Error: FLUXER_TOKEN not found in environment variables")
|
print("Error: FLUXER_TOKEN not found in environment variables")
|
||||||
|
|||||||
Reference in New Issue
Block a user