the meme command in my discord bot used to work earlier but it suddenly just stopped working.... here is the code
@commands.command(name = 'meme')
@commands.cooldown(1, 2, BucketType.user)
async def meme(self,ctx):
'''get a random meme from reddit!'''
subreddits = ['dankmemes', 'memes','meme', 'wholesomememes', 'comedyheaven','pewdiepiesubmissions', 'KidsAreFuckingStupid','cursedcomments','HolUp','blursedimages','rareinsults']
subreddit = random.choice(subreddits)
async with aiohttp.ClientSession() as cs:
async with cs.get(f'https://www.reddit.com/r/{subreddit}/new.json?sort=hot') as r:
res = await r.json()
post=res['data']['children'][random.randint(0, 25)]
url = post['data']['url']
title= post['data']['title']
embed = discord.Embed(title = title,description=f"Meme for {ctx.author}")
embed.set_image(url=url)
embed.set_footer(text = f'Image from r/{subreddit}')
await ctx.send(embed=embed)
It stopped working some days ago.. I've been trying to find a solution for quite a while but couldn't get it to work I tried this: https://stackoverflow.com/a/48841071/16390831
Every solution I tried though, gave one of the two errors:
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://www.reddit.com/r/meme/new.json?sort=hot')
File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 167, in wrapped ret = await coro(*args, **kwargs) File "c:\Users\dhrav\Documents\Python Projects\SpaceBot\SpaceBot\commands.py", line 565, in meme res = json.loads(rs) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What is wrong? is it something to do with the Reddit API? I tried printing cs, it gave a weird output about servers and stuff
again, it did work a couple of days ago but stopped working suddenly, seemingly for no reason. any help would be appreciated. thanks in advance.
CodePudding user response:
The API is returning a Content-Type: text/html
header, you can pass content_type=None
into r.json
to ignore the header:
res = await r.json(content_type=None)
CodePudding user response:
Seems like Reddit is sending the wrong content type. You can pass the expected content to the json()
method
res = await r.json(content_type='text/html')