From 542ae9a35a65e6ba841fde6f0ac971cd3790e665 Mon Sep 17 00:00:00 2001 From: Michelle Date: Thu, 19 Feb 2026 15:54:20 +0100 Subject: [PATCH] more prints --- main.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 99359b8..6fb7547 100644 --- a/main.py +++ b/main.py @@ -28,16 +28,21 @@ async def get_latest_post(subreddit): headers = {"User-Agent": "Mozilla/5.0 (compatible; ich_iel-Bot/0.1)"} response = requests.get(url, headers=headers) if response.status_code == 200: - try: - data = response.json() - print(f"Fetched data from Reddit: {data}") - 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"] - except (KeyError, json.JSONDecodeError): + if response.headers.get("Content-Type", "").startswith("application/json"): + try: + data = response.json() + print(f"Fetched data from Reddit: {data}") + 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"] + except (KeyError, json.JSONDecodeError): + return None, None + else: + print(f"Unexpected content type from Reddit: {response.headers.get('Content-Type')}") + print(f"Response content: {response.text}") return None, None else: print(f"Failed to fetch Reddit data (maybe a block?): {response.status_code}")