diff options
author | 2019-10-03 18:06:45 -0400 | |
---|---|---|
committer | 2019-10-03 18:06:45 -0400 | |
commit | 75c22d58f70732a2c457ad72453107d222ee5d94 (patch) | |
tree | bd930d95d8749dba705aeaeeebba235ce1be71e2 | |
parent | Merge pull request #486 from python-discord/mute-fix (diff) | |
parent | Merge branch 'master' into fetch_posts_retries (diff) |
Merge pull request #481 from bendiller/fetch_posts_retries
Add checks for valid response and retries to fetch_posts()
-rw-r--r-- | bot/cogs/reddit.py | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/bot/cogs/reddit.py b/bot/cogs/reddit.py index 63a57c5c6..6880aab85 100644 --- a/bot/cogs/reddit.py +++ b/bot/cogs/reddit.py @@ -21,6 +21,7 @@ class Reddit(Cog): HEADERS = {"User-Agent": "Discord Bot: PythonDiscord (https://pythondiscord.com/)"} URL = "https://www.reddit.com" + MAX_FETCH_RETRIES = 3 def __init__(self, bot: Bot): self.bot = bot @@ -42,16 +43,23 @@ class Reddit(Cog): if params is None: params = {} - response = await self.bot.http_session.get( - url=f"{self.URL}/{route}.json", - headers=self.HEADERS, - params=params - ) + url = f"{self.URL}/{route}.json" + for _ in range(self.MAX_FETCH_RETRIES): + response = await self.bot.http_session.get( + url=url, + headers=self.HEADERS, + params=params + ) + if response.status == 200 and response.content_type == 'application/json': + # Got appropriate response - process and return. + content = await response.json() + posts = content["data"]["children"] + return posts[:amount] - content = await response.json() - posts = content["data"]["children"] + await asyncio.sleep(3) - return posts[:amount] + log.debug(f"Invalid response from: {url} - status code {response.status}, mimetype {response.content_type}") + return list() # Failed to get appropriate response within allowed number of retries. async def send_top_posts( self, channel: TextChannel, subreddit: Subreddit, content: str = None, time: str = "all" @@ -62,13 +70,14 @@ class Reddit(Cog): embed.description = "" # Get the posts - posts = await self.fetch_posts( - route=f"{subreddit}/top", - amount=5, - params={ - "t": time - } - ) + async with channel.typing(): + posts = await self.fetch_posts( + route=f"{subreddit}/top", + amount=5, + params={ + "t": time + } + ) if not posts: embed.title = random.choice(ERROR_REPLIES) |