Skip to content

Commit

Permalink
reverted aiohttp fix kinda
Browse files Browse the repository at this point in the history
  • Loading branch information
oyisre committed Aug 31, 2016
1 parent d106378 commit ce52783
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async def on_ready():
os.mkdir('data')
bot.add_cog(general.General(bot))
bot.add_cog(voice.Voice(bot))
bot.add.cog(queries.Queries(bot))
bot.add_cog(queries.Queries(bot))
bot.add_cog(games.Games(bot, conn))
bot.run(str(os.environ['DISCORD_TOKEN']))
queries.close_aiohttp()
36 changes: 19 additions & 17 deletions cogs/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# global vars
js_driver = None
session = aiohttp.ClientSession()


class Queries:
Expand Down Expand Up @@ -64,24 +65,22 @@ async def _image_not_safe(self, ctx):
await self.bot.say(image_url)

@commands.command()
async def copypasta(self, search=""):
async def copypasta(self, search=""):
"""Pastes a random copypasta (copypasterino.me)
add a query after to search"""
pasta = ""
if search != "":
search_urlsafe = urllib.parse.quote_plus(search)
url = 'http://copypasterino.me/search/' + search_urlsafe
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
json = await r.json()
pasta = json[random.randrange(0, len(json))]['pasta']
async with session.get(url) as r:
json = await r.json()
pasta = json[random.randrange(0, len(json))]['pasta']
else:
url = 'http://copypasterino.me/static/all/hot/' + \
str(random.randint(1, 7))
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
json = await r.json()
pasta = json[random.randrange(0, len(json))]['pasta']
async with session.get(url) as r:
json = await r.json()
pasta = json[random.randrange(0, len(json))]['pasta']
"""
url = 'http://copypasterino.me/general/hot/' + str(random.randint(1, 7))
# print(url)
Expand Down Expand Up @@ -185,11 +184,11 @@ async def bing_img_search(query, safe=True, offset=0):
search_q += '&Adult=%27Off%27'
url = base_url + '&Query=' + search_q + \
'&$top=1&$format=JSON&$skip=' + str(offset)
async with aiohttp.ClientSession() as session:
async with session.get(
url, auth=("", os.environ['BING_API_KEY'])) as response:
results = await response.json()
response.close()

async with session.get(
url, auth=("", os.environ['BING_API_KEY'])) as response:
results = await response.json()
response.close()

image_url = results['d']['results'][0]['Image']
if len(image_url) > 0:
Expand All @@ -207,9 +206,8 @@ async def after_the_deadline(query, type=0):
url = 'http://service.afterthedeadline.com/checkGrammar'
else:
url = 'http://service.afterthedeadline.com/checkDocument'
async with aiohttp.ClientSession() as session:
async with session.post(url, data=data) as r:
response = await r.text()
async with session.post(url, data=data) as r:
response = await r.text()
soup = BeautifulSoup(response, 'html.parser')
output = ""
for error in soup.find_all('error'):
Expand All @@ -223,3 +221,7 @@ async def after_the_deadline(query, type=0):
output += error.find('description').text + \
": " + suggestions[1:] + "\n"
return output


def close_aiohttp():
session.close()

0 comments on commit ce52783

Please sign in to comment.