Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions bot/cogs/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,16 +43,23 @@ async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None
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)
Comment thread
bendiller marked this conversation as resolved.
Resolved

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.
Comment thread
MarkKoz marked this conversation as resolved.
Resolved

async def send_top_posts(
self, channel: TextChannel, subreddit: Subreddit, content: str = None, time: str = "all"
Expand All @@ -62,13 +70,14 @@ async def send_top_posts(
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)
Expand Down