try using RSS feed instead of json, since RSS doesn't seem to block server ips
Build and publish ich_iel bot / build (push) Successful in 6m31s
Build and publish ich_iel bot / build (push) Successful in 6m31s
This commit is contained in:
@@ -11,6 +11,7 @@ import re
|
|||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import yt_player
|
import yt_player
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
# 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
|
||||||
@@ -53,32 +54,30 @@ async def post_reddit_periodically():
|
|||||||
|
|
||||||
async def get_latest_post(subreddit):
|
async def get_latest_post(subreddit):
|
||||||
post_limit = os.getenv("POST_LIMIT", 20)
|
post_limit = os.getenv("POST_LIMIT", 20)
|
||||||
url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit={post_limit}"
|
url = f"https://www.reddit.com/r/{subreddit}/hot.rss?limit={post_limit}"
|
||||||
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.3)"}
|
headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.5.1)"}
|
||||||
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"):
|
logging.error(f"Failed to fetch RSS feed: {response.status_code}")
|
||||||
try:
|
return []
|
||||||
data = response.json()
|
|
||||||
logging.debug(f"Fetched data from Reddit: {data}")
|
|
||||||
posts = []
|
posts = []
|
||||||
if data["data"]["children"]:
|
try:
|
||||||
for child in data["data"]["children"]:
|
root = ET.fromstring(response.text)
|
||||||
post = child["data"]
|
ns = {"atom": "http://www.w3.org/2005/Atom"}
|
||||||
if post.get("post_hint") == "image" and post.get("url", "").endswith((".jpg", ".png", ".jpeg", ".gif")):
|
for entry in root.findall("atom:entry", ns):
|
||||||
logging.debug(f"Found image post: {post['title']} - {post['url']}")
|
post_title = entry.find("atom:title", ns)
|
||||||
posts.append((post["title"], post["url"]))
|
content = entry.find("atom:content", ns)
|
||||||
|
if post_title is None or content is None or content.text is None:
|
||||||
|
continue
|
||||||
|
link_match = re.search(r'<a href="([^"]+)">\[link\]</a>', content.text)
|
||||||
|
if link_match and link_match.group(1).lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
|
||||||
|
logging.debug(f"Found image post: {post_title.text} - {link_match.group(1)}")
|
||||||
|
posts.append((post_title.text, link_match.group(1)))
|
||||||
|
except ET.ParseError as e:
|
||||||
|
logging.error(f"Error parsing RSS feed: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
return posts
|
return posts
|
||||||
except (KeyError, json.JSONDecodeError):
|
|
||||||
logging.error(f"Error parsing Reddit response: {response.text}")
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
logging.warning(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}")
|
|
||||||
logging.warning(f"Response content: {response.text}")
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
logging.error(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def init_db():
|
async def init_db():
|
||||||
try:
|
try:
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ def setup(bot):
|
|||||||
bot.command()(play)
|
bot.command()(play)
|
||||||
|
|
||||||
async def play(ctx, *, url: str):
|
async def play(ctx, *, url: str):
|
||||||
guild_id = ctx._guild.id # Guild aus der Message
|
guild_id = ctx._guild.id
|
||||||
voice_state = _bot.get_voice_state(guild_id, ctx.author.id)
|
voice_state = _bot.get_voice_state(guild_id, ctx.author.id)
|
||||||
|
|
||||||
if voice_state is None or voice_state.channel_id is None:
|
if voice_state is None or voice_state.channel_id is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user