mirror of
https://github.com/insertapp/mmolbbot.git
synced 2025-07-01 22:37:04 +00:00
refactor: further optimizations for live games
This commit is contained in:
parent
14f12210e6
commit
a6e06e9e98
1 changed files with 182 additions and 170 deletions
|
@ -5,6 +5,7 @@ from datetime import datetime, timedelta, timezone
|
||||||
import requests
|
import requests
|
||||||
import asyncio
|
import asyncio
|
||||||
import nextcord
|
import nextcord
|
||||||
|
import aiohttp
|
||||||
import aiosqlite as sqlite3
|
import aiosqlite as sqlite3
|
||||||
from nextcord.ext import commands, application_checks, tasks
|
from nextcord.ext import commands, application_checks, tasks
|
||||||
from nextcord import TextInputStyle, IntegrationType
|
from nextcord import TextInputStyle, IntegrationType
|
||||||
|
@ -15,12 +16,16 @@ async def livegameworker(self,serverid,userid,channelid,messageid,gameid,offset)
|
||||||
begin = timeit.default_timer()
|
begin = timeit.default_timer()
|
||||||
lastserverid = serverid
|
lastserverid = serverid
|
||||||
try:
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
channel = self.bot.get_channel(channelid)
|
channel = self.bot.get_channel(channelid)
|
||||||
message = await channel.fetch_message(messageid)
|
message = await channel.fetch_message(messageid)
|
||||||
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json()
|
data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}")
|
||||||
|
data = await data.json()
|
||||||
if len(data["entries"]) > 0:
|
if len(data["entries"]) > 0:
|
||||||
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset-((7-len(data["entries"]) if offset > 7 else offset))}").json()
|
data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset-((7-len(data["entries"]) if offset > 7 else offset))}")
|
||||||
basedata = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
data = await data.json()
|
||||||
|
basedata = await session.get(f"https://mmolb.com/api/game/{gameid}")
|
||||||
|
basedata = await basedata.json()
|
||||||
finalstr = ""
|
finalstr = ""
|
||||||
offsetadd = 0
|
offsetadd = 0
|
||||||
for i in data["entries"]:
|
for i in data["entries"]:
|
||||||
|
@ -74,6 +79,7 @@ async def livegameworker(self,serverid,userid,channelid,messageid,gameid,offset)
|
||||||
DELETE from liveupdate WHERE messageid = {messageid}
|
DELETE from liveupdate WHERE messageid = {messageid}
|
||||||
""")
|
""")
|
||||||
#await self.bot.db.commit()
|
#await self.bot.db.commit()
|
||||||
|
if message:
|
||||||
await message.edit(f"An error occoured in this live update\n{e}")
|
await message.edit(f"An error occoured in this live update\n{e}")
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
await warning.send(e)
|
await warning.send(e)
|
||||||
|
@ -90,17 +96,19 @@ async def classiclivegameworker(self,serverid,userid,channelid,gameid,offset):
|
||||||
begin = timeit.default_timer()
|
begin = timeit.default_timer()
|
||||||
lastserverid = serverid
|
lastserverid = serverid
|
||||||
try:
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
channel = self.bot.get_channel(channelid)
|
channel = self.bot.get_channel(channelid)
|
||||||
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json()
|
data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}")
|
||||||
|
data = await data.json()
|
||||||
if len(data["entries"]) > 0:
|
if len(data["entries"]) > 0:
|
||||||
#data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset-((7-len(data["entries"]) if offset > 7 else offset))}").json()
|
basedata = await session.get(f"https://mmolb.com/api/game/{gameid}")
|
||||||
basedata = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
basedata = await basedata.json()
|
||||||
finalstr = ""
|
finalstr = "\n>>> "
|
||||||
offsetadd = 0
|
offsetadd = 0
|
||||||
maysend = False
|
maysend = False
|
||||||
for i in data["entries"]:
|
for i in data["entries"]:
|
||||||
if "scores" in i['message'] or "homers" in i['message']:
|
if "scores" in i['message'] or "homers" in i['message']:
|
||||||
finalstr += f"\n>>> {i['message'].replace(", ","\n").replace(". ","\n").replace("<strong>", "").replace("</strong>", "\n")}"
|
finalstr += f"{i['message'].replace(", ","\n").replace(". ","\n").replace("<strong>", "").replace("</strong>", "\n")}\n"
|
||||||
maysend = True
|
maysend = True
|
||||||
offsetadd += 1
|
offsetadd += 1
|
||||||
if i["event"] == "Recordkeeping":
|
if i["event"] == "Recordkeeping":
|
||||||
|
@ -108,8 +116,7 @@ async def classiclivegameworker(self,serverid,userid,channelid,gameid,offset):
|
||||||
DELETE from liveupdate WHERE channelid = {channelid} AND gameid = '{gameid}'
|
DELETE from liveupdate WHERE channelid = {channelid} AND gameid = '{gameid}'
|
||||||
""")
|
""")
|
||||||
maysend = True
|
maysend = True
|
||||||
finalstr += f"\n> {i['message'].replace("<strong>", "**").replace("</strong>", "**")}"
|
finalstr += f"{i['message'].replace("<strong>", "**").replace("</strong>", "**")}\n"
|
||||||
#await self.bot.db.commit()
|
|
||||||
if maysend:
|
if maysend:
|
||||||
if data["entries"][-1]["inning_side"] == 1:
|
if data["entries"][-1]["inning_side"] == 1:
|
||||||
await channel.send(f"Bottom of the {data["entries"][-1]["inning"]} | {basedata["AwayTeamName"]} {basedata["AwayTeamEmoji"]} {data["entries"][-1]["away_score"]} vs {basedata["HomeTeamName"]} {basedata["HomeTeamEmoji"]} **{data["entries"][-1]["home_score"]}**{finalstr}")
|
await channel.send(f"Bottom of the {data["entries"][-1]["inning"]} | {basedata["AwayTeamName"]} {basedata["AwayTeamEmoji"]} {data["entries"][-1]["away_score"]} vs {basedata["HomeTeamName"]} {basedata["HomeTeamEmoji"]} **{data["entries"][-1]["home_score"]}**{finalstr}")
|
||||||
|
@ -129,6 +136,85 @@ async def classiclivegameworker(self,serverid,userid,channelid,gameid,offset):
|
||||||
await self.bot.db.commit()
|
await self.bot.db.commit()
|
||||||
await warning.send(f"Deleted {lastserverid} from the database due to 403 error")
|
await warning.send(f"Deleted {lastserverid} from the database due to 403 error")
|
||||||
|
|
||||||
|
|
||||||
|
async def spotlightsubscriptionworker(self,serverid,channelid,classic,gameid,data):
|
||||||
|
try:
|
||||||
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
||||||
|
test = await check.fetchone()
|
||||||
|
if test is None:
|
||||||
|
channel = self.bot.get_channel(channelid)
|
||||||
|
if data["State"] == "Complete":
|
||||||
|
return
|
||||||
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
||||||
|
test = await check.fetchone()
|
||||||
|
if test is None: #no idea why it has to have two checks
|
||||||
|
if classic == 0:
|
||||||
|
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
|
||||||
|
await self.bot.db.execute(f"""
|
||||||
|
INSERT INTO liveupdate VALUES
|
||||||
|
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])}, 0)
|
||||||
|
""")
|
||||||
|
else:
|
||||||
|
await self.bot.db.execute(f"""
|
||||||
|
INSERT INTO liveupdate VALUES
|
||||||
|
({channel.guild.id}, {self.bot.application_id}, {channelid}, NULL, "{gameid}", {len(data["EventLog"])}, 1)
|
||||||
|
""")
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
await self.bot.db.close()
|
||||||
|
self.checkspotlightsubscriptions.cancel()
|
||||||
|
self.checkteamsubscriptions.cancel()
|
||||||
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
|
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
|
||||||
|
except Exception as e:
|
||||||
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
|
await warning.send(f"Ignoring exception in spotlight check: {e}")
|
||||||
|
print(e)
|
||||||
|
return
|
||||||
|
|
||||||
|
async def teamsubscriptionworker(self,serverid,channelid,teamid,classic):
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
game = await session.get(f"https://mmolb.com/api/game-by-team/{teamid}")
|
||||||
|
game = await game.json()
|
||||||
|
gameid = game["game_id"]
|
||||||
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
||||||
|
test = await check.fetchone()
|
||||||
|
if test is None:
|
||||||
|
data = await session.get(f"https://mmolb.com/api/game/{gameid}")
|
||||||
|
data = await data.json()
|
||||||
|
channel = self.bot.get_channel(channelid)
|
||||||
|
if data["State"] == "Complete":
|
||||||
|
return
|
||||||
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
||||||
|
test = await check.fetchone()
|
||||||
|
if test is None: #no idea why it has to have two checks
|
||||||
|
if classic == 0:
|
||||||
|
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
|
||||||
|
await self.bot.db.execute(f"""
|
||||||
|
INSERT INTO liveupdate VALUES
|
||||||
|
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])}, 0)
|
||||||
|
""")
|
||||||
|
else:
|
||||||
|
await self.bot.db.execute(f"""
|
||||||
|
INSERT INTO liveupdate VALUES
|
||||||
|
({channel.guild.id}, {self.bot.application_id}, {channelid}, NULL, "{gameid}", {len(data["EventLog"])}, 1)
|
||||||
|
""")
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
await self.bot.db.close()
|
||||||
|
self.checkspotlightsubscriptions.cancel()
|
||||||
|
self.checkteamsubscriptions.cancel()
|
||||||
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
|
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
|
||||||
|
except Exception as e:
|
||||||
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
|
await warning.send(f"Ignoring exception in {teamid}: {e}")
|
||||||
|
print(e)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class liveupdate(commands.Cog):
|
class liveupdate(commands.Cog):
|
||||||
|
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
|
@ -408,14 +494,13 @@ class liveupdate(commands.Cog):
|
||||||
try:
|
try:
|
||||||
begin = timeit.default_timer()
|
begin = timeit.default_timer()
|
||||||
await self.bot.wait_until_ready()
|
await self.bot.wait_until_ready()
|
||||||
print("updating live games")
|
|
||||||
res = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE classic = 0")
|
res = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE classic = 0")
|
||||||
res = await res.fetchall()
|
res = await res.fetchall()
|
||||||
worklist = [livegameworker(self,serverid,userid,channelid,messageid,gameid,offset) for [serverid,userid,channelid,messageid,gameid,offset] in res]
|
worklist = [livegameworker(self,serverid,userid,channelid,messageid,gameid,offset) for [serverid,userid,channelid,messageid,gameid,offset] in res]
|
||||||
res = await self.bot.db.execute("SELECT serverid,userid,channelid,gameid,offset FROM liveupdate WHERE classic = 1")
|
res = await self.bot.db.execute("SELECT serverid,userid,channelid,gameid,offset FROM liveupdate WHERE classic = 1")
|
||||||
res = await res.fetchall()
|
res = await res.fetchall()
|
||||||
worklist = worklist + [classiclivegameworker(self,serverid,userid,channelid,gameid,offset) for [serverid,userid,channelid,gameid,offset] in res]
|
worklist = worklist + [classiclivegameworker(self,serverid,userid,channelid,gameid,offset) for [serverid,userid,channelid,gameid,offset] in res]
|
||||||
await asyncio.gather(*worklist)
|
await asyncio.gather(*worklist,return_exceptions=True)
|
||||||
await self.bot.db.commit()
|
await self.bot.db.commit()
|
||||||
timings.append(timeit.default_timer()-begin)
|
timings.append(timeit.default_timer()-begin)
|
||||||
if len(timings) >= 12:
|
if len(timings) >= 12:
|
||||||
|
@ -436,51 +521,17 @@ class liveupdate(commands.Cog):
|
||||||
async def checkspotlightsubscriptions(self):
|
async def checkspotlightsubscriptions(self):
|
||||||
try:
|
try:
|
||||||
await self.bot.wait_until_ready()
|
await self.bot.wait_until_ready()
|
||||||
print("refreshing spotlight subscriptions")
|
async with aiohttp.ClientSession() as session:
|
||||||
game = requests.get("https://mmolb.com/api/spotlight").json()
|
game = await session.get("https://mmolb.com/api/spotlight")
|
||||||
|
game = await game.json()
|
||||||
gameid = game["game_id"]
|
gameid = game["game_id"]
|
||||||
|
data = await session.get(f"https://mmolb.com/api/game/{gameid}")
|
||||||
|
data = await data.json()
|
||||||
res = await self.bot.db.execute("SELECT serverid,channelid,classic FROM spotlightsubscriptions")
|
res = await self.bot.db.execute("SELECT serverid,channelid,classic FROM spotlightsubscriptions")
|
||||||
res = await res.fetchall()
|
res = await res.fetchall()
|
||||||
print(res)
|
worklist = [spotlightsubscriptionworker(self,serverid,channelid,classic,gameid,data) for [serverid,channelid,classic] in res]
|
||||||
for [serverid,channelid,classic] in res:
|
await asyncio.gather(*worklist,return_exceptions=True)
|
||||||
try:
|
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
|
||||||
test = await check.fetchone()
|
|
||||||
print(test)
|
|
||||||
if test is None:
|
|
||||||
print("True")
|
|
||||||
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
|
||||||
channel = self.bot.get_channel(channelid)
|
|
||||||
if data["State"] == "Complete":
|
|
||||||
continue
|
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
|
||||||
test = await check.fetchone()
|
|
||||||
if test is None: #no idea why it has to have two checks
|
|
||||||
if classic == 0:
|
|
||||||
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
|
|
||||||
await self.bot.db.execute(f"""
|
|
||||||
INSERT INTO liveupdate VALUES
|
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])}, 0)
|
|
||||||
""")
|
|
||||||
else:
|
|
||||||
await self.bot.db.execute(f"""
|
|
||||||
INSERT INTO liveupdate VALUES
|
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, NULL, "{gameid}", {len(data["EventLog"])}, 1)
|
|
||||||
""")
|
|
||||||
await self.bot.db.commit()
|
await self.bot.db.commit()
|
||||||
else:
|
|
||||||
print("false")
|
|
||||||
except sqlite3.OperationalError:
|
|
||||||
await self.bot.db.close()
|
|
||||||
self.checkspotlightsubscriptions.cancel()
|
|
||||||
self.checkteamsubscriptions.cancel()
|
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
|
||||||
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
|
|
||||||
except Exception as e:
|
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
|
||||||
await warning.send(f"Ignoring exception in spotlight check: {e}")
|
|
||||||
print(e)
|
|
||||||
continue
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -488,57 +539,18 @@ class liveupdate(commands.Cog):
|
||||||
print(e)
|
print(e)
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
await warning.send(e)
|
await warning.send(e)
|
||||||
print(e)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@tasks.loop(seconds=120.0)
|
@tasks.loop(seconds=120.0)
|
||||||
async def checkteamsubscriptions(self):
|
async def checkteamsubscriptions(self):
|
||||||
try:
|
try:
|
||||||
print("refreshing team subscriptions")
|
|
||||||
await self.bot.wait_until_ready()
|
await self.bot.wait_until_ready()
|
||||||
res = await self.bot.db.execute("SELECT serverid,channelid,teamid,classic FROM teamsubscriptions")
|
res = await self.bot.db.execute("SELECT serverid,channelid,teamid,classic FROM teamsubscriptions")
|
||||||
res = await res.fetchall()
|
res = await res.fetchall()
|
||||||
print(res)
|
worklist = [teamsubscriptionworker(self,serverid,channelid,teamid,classic) for [serverid,channelid,teamid,classic] in res]
|
||||||
for [serverid,channelid,teamid,classic] in res:
|
await asyncio.gather(*worklist,return_exceptions=True)
|
||||||
try:
|
|
||||||
game = requests.get(f"https://mmolb.com/api/game-by-team/{teamid}").json()
|
|
||||||
gameid = game["game_id"]
|
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
|
||||||
test = await check.fetchone()
|
|
||||||
if test is None:
|
|
||||||
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
|
||||||
channel = self.bot.get_channel(channelid)
|
|
||||||
if data["State"] == "Complete":
|
|
||||||
continue
|
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
|
|
||||||
test = await check.fetchone()
|
|
||||||
if test is None: #no idea why it has to have two checks
|
|
||||||
if classic == 0:
|
|
||||||
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
|
|
||||||
await self.bot.db.execute(f"""
|
|
||||||
INSERT INTO liveupdate VALUES
|
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])}, 0)
|
|
||||||
""")
|
|
||||||
else:
|
|
||||||
await self.bot.db.execute(f"""
|
|
||||||
INSERT INTO liveupdate VALUES
|
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, NULL, "{gameid}", {len(data["EventLog"])}, 1)
|
|
||||||
""")
|
|
||||||
await self.bot.db.commit()
|
await self.bot.db.commit()
|
||||||
except KeyError:
|
|
||||||
continue
|
|
||||||
except sqlite3.OperationalError:
|
|
||||||
await self.bot.db.close()
|
|
||||||
self.checkspotlightsubscriptions.cancel()
|
|
||||||
self.checkteamsubscriptions.cancel()
|
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
|
||||||
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
|
|
||||||
except Exception as e:
|
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
|
||||||
await warning.send(f"Ignoring exception in {teamid}: {e}")
|
|
||||||
print(e)
|
|
||||||
continue
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#I know this is bad practice but these loops must be running
|
#I know this is bad practice but these loops must be running
|
||||||
warning = self.bot.get_channel(1365478368555827270)
|
warning = self.bot.get_channel(1365478368555827270)
|
||||||
|
|
Loading…
Reference in a new issue