mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[V3 Streams] Replace instances of ClientSession assignment with async context manager (#1238)
This commit is contained in:
parent
05c5c58eaf
commit
68800d28fc
@ -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 session.get(TWITCH_COMMUNITIES_ENDPOINT, headers=headers, params=params) as r:
|
async with aiohttp.ClientSession() as session:
|
||||||
data = await r.json()
|
async with session.get(TWITCH_COMMUNITIES_ENDPOINT, headers=headers, params=params) as r:
|
||||||
await session.close()
|
data = await r.json()
|
||||||
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,8 +58,9 @@ class TwitchCommunity:
|
|||||||
"community_id": self.id
|
"community_id": self.id
|
||||||
}
|
}
|
||||||
url = TWITCH_BASE_URL + "/kraken/streams"
|
url = TWITCH_BASE_URL + "/kraken/streams"
|
||||||
async with session.get(url, headers=headers, params=params) as r:
|
async with aiohttp.ClientSession() as session:
|
||||||
data = await r.json()
|
async with session.get(url, headers=headers, params=params) as r:
|
||||||
|
data = await r.json()
|
||||||
if r.status == 200:
|
if r.status == 200:
|
||||||
if data["_total"] == 0:
|
if data["_total"] == 0:
|
||||||
raise OfflineCommunity()
|
raise OfflineCommunity()
|
||||||
@ -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 session.get(url, headers=header) as r:
|
async with aiohttp.ClientSession() as session:
|
||||||
data = await r.json(encoding='utf-8')
|
async with session.get(url, headers=header) as r:
|
||||||
await session.close()
|
data = await r.json(encoding='utf-8')
|
||||||
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 session.get(url, headers=header) as r:
|
async with aiohttp.ClientSession() as session:
|
||||||
data = await r.json()
|
async with session.get(url, headers=header) as r:
|
||||||
await session.close()
|
data = await r.json()
|
||||||
|
|
||||||
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 session.get(url) as r:
|
async with aiohttp.ClientSession() as session:
|
||||||
#data = await r.json(encoding='utf-8')
|
async with session.get(url) as r:
|
||||||
data = await r.text()
|
#data = await r.json(encoding='utf-8')
|
||||||
await session.close()
|
data = await r.text()
|
||||||
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:
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user