1
0
Fork 0
mirror of synced 2024-06-29 03:30:57 +12:00

Replaced urllib with aiohttp

This commit is contained in:
Phxntxm 2016-07-24 09:02:50 -05:00
parent 4792225061
commit 1f7da0a4f5
2 changed files with 42 additions and 33 deletions

View file

@ -16,13 +16,13 @@ class Links:
@checks.customPermsOrRole("send_messages") @checks.customPermsOrRole("send_messages")
async def urban(self, *msg: str): async def urban(self, *msg: str):
"""Pulls the top urbandictionary.com definition for a term""" """Pulls the top urbandictionary.com definition for a term"""
try:
url = "http://api.urbandictionary.com/v0/define?term={}".format('+'.join(msg)) url = "http://api.urbandictionary.com/v0/define?term={}".format('+'.join(msg))
with aiohttp.ClientSession() as s: with aiohttp.ClientSession() as s:
async with s.get(url) as r: async with s.get(url) as r:
response = await r.text() response = await r.text()
data = json.loads(response) data = json.loads(response)
try:
if len(data['list']) == 0: if len(data['list']) == 0:
await self.bot.say("No result with that term!") await self.bot.say("No result with that term!")
else: else:

View file

@ -3,9 +3,7 @@ from .utils import checks
from discord.ext import commands from discord.ext import commands
import discord import discord
import urllib.parse import aiohttp
import urllib.request
import urllib.error
import json import json
import re import re
@ -41,29 +39,38 @@ class Overwatch:
return return
await self.bot.say("Searching profile information....") await self.bot.say("Searching profile information....")
try:
if hero == "": if hero == "":
result = urllib.request.urlopen(base_url + "{}/stats/general".format(bt)) await aiohttp.ClientSession() as s:
data = json.loads(result.read().decode('utf-8')) async with s.get(base_url + "{}/stats/general".format(bt)) as r:
fmt = "\n".join("{}: {}".format(i, r) for i, r in data['game_stats'].items() if i in check_g_stats) result = await r.text()
fmt += "\n"
fmt += "\n".join("{}: {}".format(i, r) for i, r in data['overall_stats'].items() if i in check_o_stats) data = json.loads(result)
await self.bot.say( fmt = "\n".join("{}: {}".format(i, r) for i, r in data['game_stats'].items() if i in check_g_stats)
"Overwatch stats for {}: ```py\n{}```".format(user.name, fmt.title().replace("_", " "))) fmt += "\n"
else: fmt += "\n".join("{}: {}".format(i, r) for i, r in data['overall_stats'].items() if i in check_o_stats)
result = urllib.request.urlopen(base_url + "{}/heroes/{}".format(bt, hero.lower().replace('-', ''))) await self.bot.say(
data = json.loads(result.read().decode('utf-8')) "Overwatch stats for {}: ```py\n{}```".format(user.name, fmt.title().replace("_", " ")))
fmt = "\n".join("{}: {}".format(i, r) for i, r in data['general_stats'].items() if i in check_g_stats) else:
fmt += "\n" url = base_url + "{}/heroes/{}".format(bt, hero.lower().replace('-', '')
fmt += "\n".join("{}: {}".format(i, r) for i, r in data['hero_stats'].items()) await aiohttp.ClientSession() as s:
await self.bot.say("Overwatch stats for {} using the hero {}: ```py\n{}``` " async with s.get(url) as r:
.format(user.name, hero.title(), fmt.title().replace("_", " "))) if r.status == 500
except urllib.error.HTTPError as error: fmt = "{} has not used the hero {} before!".format(user.name, hero.title())
error_no = int(re.search("\d+", str(error)).group(0)) await self.bot.say(fmt)
if error_no == 500: return
await self.bot.say("{} has not used the hero {} before!".format(user.name, hero.title())) elif r.status = 404
elif error_no == 404: fmt = "{} is not an actual hero!".format(hero.title())
await self.bot.say("{} is not an actual hero!".format(hero.title())) await self.bot.say(fmt)
return
result = await r.text()
data = json.loads(result)
fmt = "\n".join("{}: {}".format(i, r) for i, r in data['general_stats'].items() if i in check_g_stats)
fmt += "\n"
fmt += "\n".join("{}: {}".format(i, r) for i, r in data['hero_stats'].items())
await self.bot.say("Overwatch stats for {} using the hero {}: ```py\n{}``` "
.format(user.name, hero.title(), fmt.title().replace("_", " ")))
@ow.command(pass_context=True, name="add", no_pm=True) @ow.command(pass_context=True, name="add", no_pm=True)
@checks.customPermsOrRole("none") @checks.customPermsOrRole("none")
@ -72,12 +79,14 @@ class Overwatch:
bt = bt.replace("#", "-") bt = bt.replace("#", "-")
await self.bot.say("Looking up your profile information....") await self.bot.say("Looking up your profile information....")
url = base_url + "{}/stats/general".format(bt) url = base_url + "{}/stats/general".format(bt)
try:
urllib.request.urlopen(url) await aiohttp.ClientSession() as s:
except urllib.error.HTTPError: async with s.get(url) as r:
await self.bot.say("Profile does not exist! Battletags are picky, " if not r.status == 200:
"format needs to be `user#xxxx`. Capitalization matters") await self.bot.say("Profile does not exist! Battletags are picky, "
return "format needs to be `user#xxxx`. Capitalization matters")
return
ow = config.getContent('overwatch') ow = config.getContent('overwatch')
ow[ctx.message.author.id] = bt ow[ctx.message.author.id] = bt
if config.saveContent('overwatch', ow): if config.saveContent('overwatch', ow):