Skip to content

Commit

Permalink
Minor Imgur Fix
Browse files Browse the repository at this point in the history
Fixes template command not working for imgur links. Additionally, this patch should work on most links too as it has a valid user-agent (Windows 10 Google Chrome).
  • Loading branch information
neurofumo authored Jul 15, 2024
1 parent 3828bed commit 19663e9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ async def get_content(url: str, content_type, **kwargs):
Raise BadResponseError or ValueError."""
# check if the URL is a data URL
data = check_data_url(url)
# set headers for user-agent
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}
if content_type == "image":
if url.startswith('https://imgur.com/'):
reurl = re.search(r'https://imgur\.com/([^?#]*)', url)
image_hash = reurl.group(1)
url = f"https://i.imgur.com/{image_hash}.png"
if data:
return data
timeout = aiohttp.ClientTimeout(
sock_connect=10.0, sock_read=10.0
) # set a timeout of 10 seconds
async with aiohttp.ClientSession(timeout=timeout, **kwargs) as session:
try:
async with session.get(url) as r:
async with session.get(url, headers=headers) as r:
if r.status == 200:
if content_type == "json":
return await r.json()
Expand Down

0 comments on commit 19663e9

Please sign in to comment.