fix: timeout issue

This commit is contained in:
2025-09-15 15:11:58 +02:00
parent ff171984f4
commit fad35d10d3
2 changed files with 64 additions and 52 deletions

View File

@@ -1,6 +1,6 @@
import os import os
import aiohttp
import requests import asyncio
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from bot.log import logger from bot.log import logger
@@ -12,9 +12,9 @@ API_USERNAME = os.getenv("DISCOURSE_API_USERNAME")
headers = {"Api-Key": API_KEY, "Api-Username": API_USERNAME} headers = {"Api-Key": API_KEY, "Api-Username": API_USERNAME}
def get_topics_by_id(topic_id): async def get_topics_by_id(topic_id):
""" """
Fetches a topic by its ID and returns the topic data. Async: Fetches a topic by its ID and returns the topic data.
Args: Args:
topic_id (int): The ID of the topic to fetch. topic_id (int): The ID of the topic to fetch.
@@ -22,33 +22,35 @@ def get_topics_by_id(topic_id):
Returns: Returns:
dict or None: The topic data if successful, otherwise None. dict or None: The topic data if successful, otherwise None.
""" """
url = f"{DISCOURSE_BASE_URL}/t/{topic_id}.json"
timeout = aiohttp.ClientTimeout(total=5)
try: try:
response = requests.get( async with aiohttp.ClientSession() as session:
f"{DISCOURSE_BASE_URL}/t/{topic_id}.json", async with session.get(url, headers=headers, timeout=timeout) as response:
headers=headers, if response.status == 200:
timeout=10, # prevent hanging forever return await response.json()
) elif response.status == 403:
if response.status_code == 200:
return response.json()
elif response.status_code == 403:
logger.error( logger.error(
f"Access forbidden for topic {topic_id}: {response.status_code}" f"Access forbidden for topic {topic_id}: {response.status}"
) )
return None return None
else: else:
text = await response.text()
logger.error( logger.error(
f"Error fetching topic {topic_id}: {response.status_code} - {response.text}" f"Error fetching topic {topic_id}: {response.status} - {text}"
) )
return None return None
except asyncio.TimeoutError:
except requests.exceptions.RequestException as e: logger.error(f"Timeout while fetching topic {topic_id}")
return None
except aiohttp.ClientError as e:
logger.error(f"Request failed for topic {topic_id}: {e}") logger.error(f"Request failed for topic {topic_id}: {e}")
return None return None
def get_topics_by_tag(tag_name): async def get_topics_by_tag(tag_name):
""" """
Fetches all topics with a specific tag and retrieves the cooked string from each post. Async: Fetches all topics with a specific tag and retrieves the cooked string from each post.
Args: Args:
tag_name (str): The name of the tag to filter topics. tag_name (str): The name of the tag to filter topics.
@@ -56,35 +58,45 @@ def get_topics_by_tag(tag_name):
Returns: Returns:
list: A list of cooked strings from all posts in the topics. list: A list of cooked strings from all posts in the topics.
""" """
response = requests.get( url = f"{DISCOURSE_BASE_URL}/tag/{tag_name}.json"
f"{DISCOURSE_BASE_URL}/tag/{tag_name}.json", headers=headers timeout = aiohttp.ClientTimeout(total=5)
) try:
if response.status_code == 200: async with aiohttp.ClientSession() as session:
data = response.json() async with session.get(url, headers=headers, timeout=timeout) as response:
if response.status == 200:
data = await response.json()
topics = data.get("topic_list", {}).get("topics", []) topics = data.get("topic_list", {}).get("topics", [])
cooked_strings = [] cooked_strings = []
for topic in topics: for topic in topics:
topic_id = topic["id"] topic_id = topic["id"]
topic_data = get_topics_by_id(topic_id) topic_data = await get_topics_by_id(topic_id)
if topic_data: if topic_data:
posts = topic_data.get("post_stream", {}).get("posts", []) posts = topic_data.get("post_stream", {}).get("posts", [])
for post in posts: for post in posts:
cooked_strings.append(post.get("cooked", "")) cooked_strings.append(post.get("cooked", ""))
return cooked_strings return cooked_strings
elif response.status_code == 403: elif response.status == 403:
logger.error(f"Access forbidden for topic {topic_id}: {response.status_code}") logger.error(
f"Access forbidden for tag '{tag_name}': {response.status}"
)
return None return None
else: else:
text = await response.text()
logger.error( logger.error(
f"Error fetching topics with tag '{tag_name}': {response.status_code} - {response.text}" f"Error fetching topics with tag '{tag_name}': {response.status} - {text}"
) )
return [] return []
except asyncio.TimeoutError:
logger.error(f"Timeout while fetching topics with tag '{tag_name}'")
return []
except aiohttp.ClientError as e:
logger.error(f"Request failed for topics with tag {tag_name}: {e}")
return []
def fetch_cooked_posts(tag_name): async def fetch_cooked_posts(tag_name):
""" """
Fetches cooked strings from posts with a specific tag. Async: Fetches cooked strings from posts with a specific tag.
Args: Args:
tag_name (str): The name of the tag to filter topics. tag_name (str): The name of the tag to filter topics.
@@ -92,7 +104,7 @@ def fetch_cooked_posts(tag_name):
Returns: Returns:
list: A list of cooked strings from posts with the specified tag. list: A list of cooked strings from posts with the specified tag.
""" """
return get_topics_by_tag(tag_name) return await get_topics_by_tag(tag_name)
def html_to_text(html_content): def html_to_text(html_content):

View File

@@ -140,7 +140,7 @@ class DiscourseUpdater(commands.Cog):
""" """
tag_name = "docs" tag_name = "docs"
logger.info("Fetching Discourse data...") logger.info("Fetching Discourse data...")
cooked_posts = fetch_cooked_posts(tag_name) cooked_posts = await fetch_cooked_posts(tag_name)
if cooked_posts: if cooked_posts:
combined_text = combine_posts_text( combined_text = combine_posts_text(
[{"cooked": post} for post in cooked_posts] [{"cooked": post} for post in cooked_posts]