1
0
Fork 0
mirror of synced 2024-09-30 09:17:13 +13: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:
result = await r.text()
data = json.loads(result)
fmt = "\n".join("{}: {}".format(i, r) for i, r in data['game_stats'].items() if i in check_g_stats) fmt = "\n".join("{}: {}".format(i, r) for i, r in data['game_stats'].items() if i in check_g_stats)
fmt += "\n" fmt += "\n"
fmt += "\n".join("{}: {}".format(i, r) for i, r in data['overall_stats'].items() if i in check_o_stats) fmt += "\n".join("{}: {}".format(i, r) for i, r in data['overall_stats'].items() if i in check_o_stats)
await self.bot.say( await self.bot.say(
"Overwatch stats for {}: ```py\n{}```".format(user.name, fmt.title().replace("_", " "))) "Overwatch stats for {}: ```py\n{}```".format(user.name, fmt.title().replace("_", " ")))
else: else:
result = urllib.request.urlopen(base_url + "{}/heroes/{}".format(bt, hero.lower().replace('-', ''))) url = base_url + "{}/heroes/{}".format(bt, hero.lower().replace('-', '')
data = json.loads(result.read().decode('utf-8')) await aiohttp.ClientSession() as s:
async with s.get(url) as r:
if r.status == 500
fmt = "{} has not used the hero {} before!".format(user.name, hero.title())
await self.bot.say(fmt)
return
elif r.status = 404
fmt = "{} 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".join("{}: {}".format(i, r) for i, r in data['general_stats'].items() if i in check_g_stats)
fmt += "\n" fmt += "\n"
fmt += "\n".join("{}: {}".format(i, r) for i, r in data['hero_stats'].items()) 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{}``` " await self.bot.say("Overwatch stats for {} using the hero {}: ```py\n{}``` "
.format(user.name, hero.title(), fmt.title().replace("_", " "))) .format(user.name, hero.title(), fmt.title().replace("_", " ")))
except urllib.error.HTTPError as error:
error_no = int(re.search("\d+", str(error)).group(0))
if error_no == 500:
await self.bot.say("{} has not used the hero {} before!".format(user.name, hero.title()))
elif error_no == 404:
await self.bot.say("{} is not an actual hero!".format(hero.title()))
@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:
if not r.status == 200:
await self.bot.say("Profile does not exist! Battletags are picky, " await self.bot.say("Profile does not exist! Battletags are picky, "
"format needs to be `user#xxxx`. Capitalization matters") "format needs to be `user#xxxx`. Capitalization matters")
return 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):