[General] URL encode user input for [p]urban and [p]lmgtfy (#4024)

* URL encode user input in the general cog

Adds URL encoding to `[p]lmgtfy` via `urllib2.parse.quote_plus()`
and to `[p]urban` via `aiohttp.ClientSession.get`'s `params` argument

* Black reformatting
This commit is contained in:
Nathaniel F 2020-09-02 18:35:27 -04:00 committed by GitHub
parent 0200c2cb3f
commit 59f69c7727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ import time
from enum import Enum from enum import Enum
from random import randint, choice from random import randint, choice
from typing import Final from typing import Final
import urllib.parse
import aiohttp import aiohttp
import discord import discord
from redbot.core import commands from redbot.core import commands
@ -214,9 +215,7 @@ class General(commands.Cog):
@commands.command() @commands.command()
async def lmgtfy(self, ctx, *, search_terms: str): async def lmgtfy(self, ctx, *, search_terms: str):
"""Create a lmgtfy link.""" """Create a lmgtfy link."""
search_terms = escape( search_terms = escape(urllib.parse.quote_plus(search_terms), mass_mentions=True)
search_terms.replace("+", "%2B").replace(" ", "+"), mass_mentions=True
)
await ctx.send("https://lmgtfy.com/?q={}".format(search_terms)) await ctx.send("https://lmgtfy.com/?q={}".format(search_terms))
@commands.command(hidden=True) @commands.command(hidden=True)
@ -482,12 +481,14 @@ class General(commands.Cog):
""" """
try: try:
url = "https://api.urbandictionary.com/v0/define?term=" + str(word).lower() url = "https://api.urbandictionary.com/v0/define"
params = {"term": str(word).lower()}
headers = {"content-type": "application/json"} headers = {"content-type": "application/json"}
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response: async with session.get(url, headers=headers, params=params) as response:
data = await response.json() data = await response.json()
except aiohttp.ClientError: except aiohttp.ClientError: