[V3 Streams] Replace instances of ClientSession assignment with async context manager (#1238)

This commit is contained in:
Tobotimus 2018-01-15 14:43:06 +11:00 committed by palmtree5
parent 05c5c58eaf
commit 68800d28fc
2 changed files with 26 additions and 34 deletions

View File

@ -25,7 +25,6 @@ class TwitchCommunity:
self.type = self.__class__.__name__ self.type = self.__class__.__name__
async def get_community_id(self): async def get_community_id(self):
session = aiohttp.ClientSession()
headers = { headers = {
"Accept": "application/vnd.twitchtv.v5+json", "Accept": "application/vnd.twitchtv.v5+json",
"Client-ID": str(self._token) "Client-ID": str(self._token)
@ -33,9 +32,9 @@ class TwitchCommunity:
params = { params = {
"name": self.name "name": self.name
} }
async with aiohttp.ClientSession() as session:
async with session.get(TWITCH_COMMUNITIES_ENDPOINT, headers=headers, params=params) as r: async with session.get(TWITCH_COMMUNITIES_ENDPOINT, headers=headers, params=params) as r:
data = await r.json() data = await r.json()
await session.close()
if r.status == 200: if r.status == 200:
return data["_id"] return data["_id"]
elif r.status == 400: elif r.status == 400:
@ -51,7 +50,6 @@ class TwitchCommunity:
self.id = await self.get_community_id() self.id = await self.get_community_id()
except CommunityNotFound: except CommunityNotFound:
raise raise
session = aiohttp.ClientSession()
headers = { headers = {
"Accept": "application/vnd.twitchtv.v5+json", "Accept": "application/vnd.twitchtv.v5+json",
"Client-ID": str(self._token) "Client-ID": str(self._token)
@ -60,6 +58,7 @@ class TwitchCommunity:
"community_id": self.id "community_id": self.id
} }
url = TWITCH_BASE_URL + "/kraken/streams" url = TWITCH_BASE_URL + "/kraken/streams"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, params=params) as r: async with session.get(url, headers=headers, params=params) as r:
data = await r.json() data = await r.json()
if r.status == 200: if r.status == 200:
@ -120,16 +119,15 @@ class TwitchStream(Stream):
if not self.id: if not self.id:
self.id = await self.fetch_id() self.id = await self.fetch_id()
session = aiohttp.ClientSession()
url = TWITCH_STREAMS_ENDPOINT + self.id url = TWITCH_STREAMS_ENDPOINT + self.id
header = { header = {
'Client-ID': str(self._token), 'Client-ID': str(self._token),
'Accept': 'application/vnd.twitchtv.v5+json' 'Accept': 'application/vnd.twitchtv.v5+json'
} }
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=header) as r: async with session.get(url, headers=header) as r:
data = await r.json(encoding='utf-8') data = await r.json(encoding='utf-8')
await session.close()
if r.status == 200: if r.status == 200:
if data["stream"] is None: if data["stream"] is None:
#self.already_online = False #self.already_online = False
@ -151,11 +149,10 @@ class TwitchStream(Stream):
'Accept': 'application/vnd.twitchtv.v5+json' 'Accept': 'application/vnd.twitchtv.v5+json'
} }
url = TWITCH_ID_ENDPOINT + self.name url = TWITCH_ID_ENDPOINT + self.name
session = aiohttp.ClientSession()
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=header) as r: async with session.get(url, headers=header) as r:
data = await r.json() data = await r.json()
await session.close()
if r.status == 200: if r.status == 200:
if not data["users"]: if not data["users"]:
@ -195,13 +192,12 @@ class TwitchStream(Stream):
class HitboxStream(Stream): class HitboxStream(Stream):
async def is_online(self): async def is_online(self):
session = aiohttp.ClientSession()
url = "https://api.hitbox.tv/media/live/" + self.name url = "https://api.hitbox.tv/media/live/" + self.name
async with aiohttp.ClientSession() as session:
async with session.get(url) as r: async with session.get(url) as r:
#data = await r.json(encoding='utf-8') #data = await r.json(encoding='utf-8')
data = await r.text() data = await r.text()
await session.close()
data = json.loads(data, strict=False) data = json.loads(data, strict=False)
if "livestream" not in data: if "livestream" not in data:
raise StreamNotFound() raise StreamNotFound()
@ -235,11 +231,10 @@ class MixerStream(Stream):
async def is_online(self): async def is_online(self):
url = "https://mixer.com/api/v1/channels/" + self.name url = "https://mixer.com/api/v1/channels/" + self.name
session = aiohttp.ClientSession() async with aiohttp.ClientSession() as session:
async with session.get(url) as r: async with session.get(url) as r:
#data = await r.json(encoding='utf-8') #data = await r.json(encoding='utf-8')
data = await r.text(encoding='utf-8') data = await r.text(encoding='utf-8')
await session.close()
if r.status == 200: if r.status == 200:
data = json.loads(data, strict=False) data = json.loads(data, strict=False)
if data["online"] is True: if data["online"] is True:
@ -278,11 +273,9 @@ class PicartoStream(Stream):
async def is_online(self): async def is_online(self):
url = "https://api.picarto.tv/v1/channel/name/" + self.name url = "https://api.picarto.tv/v1/channel/name/" + self.name
session = aiohttp.ClientSession() async with aiohttp.ClientSession() as session:
async with session.get(url) as r: async with session.get(url) as r:
data = await r.text(encoding='utf-8') data = await r.text(encoding='utf-8')
await session.close()
if r.status == 200: if r.status == 200:
data = json.loads(data) data = json.loads(data)
if data["online"] is True: if data["online"] is True:

View File

@ -293,10 +293,9 @@ class Core:
@checks.is_owner() @checks.is_owner()
async def avatar(self, ctx, url: str): async def avatar(self, ctx, url: str):
"""Sets Red's avatar""" """Sets Red's avatar"""
session = aiohttp.ClientSession() async with aiohttp.ClientSession() as session:
async with session.get(url) as r: async with session.get(url) as r:
data = await r.read() data = await r.read()
await session.close()
try: try:
await ctx.bot.user.edit(avatar=data) await ctx.bot.user.edit(avatar=data)