Replaced urllib with aiohttp
This commit is contained in:
parent
afe8efde54
commit
16f36ab933
1 changed files with 16 additions and 12 deletions
|
@ -1,8 +1,7 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from .utils import config
|
from .utils import config
|
||||||
from .utils import checks
|
from .utils import checks
|
||||||
import urllib.request
|
import aiohttp
|
||||||
import urllib.parse
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
import json
|
import json
|
||||||
|
@ -11,8 +10,10 @@ import re
|
||||||
|
|
||||||
def channelOnline(channel: str):
|
def channelOnline(channel: str):
|
||||||
url = "https://api.twitch.tv/kraken/streams/{}".format(channel)
|
url = "https://api.twitch.tv/kraken/streams/{}".format(channel)
|
||||||
response = urllib.request.urlopen(url)
|
with aiohttp.ClientSession() as s:
|
||||||
data = json.loads(response.read().decode('utf-8'))
|
async with s.get(url) as r:
|
||||||
|
response = r.text()
|
||||||
|
data = json.loads(response)
|
||||||
return data['stream'] is not None
|
return data['stream'] is not None
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,8 +60,11 @@ class Twitch:
|
||||||
if result is not None:
|
if result is not None:
|
||||||
url = result['twitch_url']
|
url = result['twitch_url']
|
||||||
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
|
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
|
||||||
result = urllib.request.urlopen("https://api.twitch.tv/kraken/channels/{}".format(user))
|
with aiohttp.ClientSession() as s:
|
||||||
data = json.loads(result.read().decode('utf-8'))
|
async with s.get("https://api.twitch.tv/kraken/channels/{}".format(user)) as r:
|
||||||
|
response = r.text()
|
||||||
|
data = json.loads(response)
|
||||||
|
|
||||||
fmt = "Username: {}".format(data['display_name'])
|
fmt = "Username: {}".format(data['display_name'])
|
||||||
fmt += "\nStatus: {}".format(data['status'])
|
fmt += "\nStatus: {}".format(data['status'])
|
||||||
fmt += "\nFollowers: {}".format(data['followers'])
|
fmt += "\nFollowers: {}".format(data['followers'])
|
||||||
|
@ -80,12 +84,12 @@ class Twitch:
|
||||||
else:
|
else:
|
||||||
url = "https://www.{}".format(url)
|
url = "https://www.{}".format(url)
|
||||||
|
|
||||||
try:
|
with aiohttp.ClientSession() as s:
|
||||||
urllib.request.urlopen(url)
|
async with s.get(url) as r:
|
||||||
except urllib.request.HTTPError:
|
if not r.status == 200:
|
||||||
await self.bot.say("That twitch user does not exist! "
|
await self.bot.say("That twitch user does not exist! "
|
||||||
"What would be the point of adding a nonexistant twitch user? Silly")
|
"What would be the point of adding a nonexistant twitch user? Silly")
|
||||||
return
|
return
|
||||||
|
|
||||||
twitch = config.getContent('twitch')
|
twitch = config.getContent('twitch')
|
||||||
result = twitch.get(ctx.message.author.id)
|
result = twitch.get(ctx.message.author.id)
|
||||||
|
|
Loading…
Reference in a new issue