Fixes for time to next event / race.

This commit is contained in:
tamservo 2023-12-25 17:01:09 -05:00
parent e960966a47
commit c91c0e6e05
2 changed files with 48 additions and 38 deletions

8
load_schedule.py Normal file → Executable file
View File

@ -11,7 +11,7 @@ if __name__ == '__main__':
# Create the table # Create the table
cur.execute("""create table schedule( id integer primary key, """ + cur.execute("""create table schedule( id integer primary key, """ +
"""title, session_type, date_start INTEGER,""" + """title, session_type, date_start,""" +
"""location, round );""") """location, round );""")
con.commit() con.commit()
@ -24,12 +24,8 @@ if __name__ == '__main__':
"date_start, location, round) VALUES(" + \ "date_start, location, round) VALUES(" + \
f"?, ?, ?, ?, ?)" f"?, ?, ?, ?, ?)"
# Convert date into seconds
date_str = row[2]
date_obj = datetime.datetime.fromisoformat(date_str)
cur.execute(sql_line, cur.execute(sql_line,
(row[0], row[1], date_obj.timestamp(), row[3], row[4]) (row)
) )
con.commit() con.commit()

View File

@ -532,49 +532,63 @@ class Robottas(commands.Bot):
return 'Watched Already' return 'Watched Already'
async def report_next_event(self,ctx): async def report_next_event(self, ctx):
try: try:
tz = datetime.timezone.utc
con = sqlite3.connect('schedule.db') con = sqlite3.connect('schedule.db')
cur = con.cursor() cur = con.cursor()
now_secs = datetime.datetime.now().timestamp() now_str = datetime.datetime.now(tz=tz).isoformat(sep=' ')
for row in cur.execute('select * from schedule ' + \ now_str = now_str.split(".")[0]
'where date_start > ?' + \
'order by date_start ascending limit 1', query = 'select * from schedule where date_start > ? ' + \
(now_secs)): 'order by date_start asc limit 1'
next_secs = row[2]
delta_seconds = next_secs - now_secs cur.execute(query, (now_str,))
td = datetime.timedelta(seconds=delta_seconds) rows = cur.fetchall()
next_date = datetime.datetime.fromtimestamp(next_secs)
message = f"Next event is {row[0]} at {next_date}. " + \ for row in rows:
f"Time remaining {td}"
await ctx.send(message) t1 = datetime.datetime.fromisoformat(now_str)
break # There should only be one row anyway t2 = datetime.datetime.fromisoformat(row[3])
delta = t2 - t1
message = f"The next event is the {row[1]} which is {delta} from now."
await ctx.send(message)
break # There should only be one row anyway
except: except:
ctx.send("Sorry, hit the wall trying to find the answer...") await ctx.send("Sorry, hit the wall trying to find the answer...")
async def report_next_race(self,ctx): async def report_next_race(self, ctx):
try: try:
tz = datetime.timezone.utc
con = sqlite3.connect('schedule.db') con = sqlite3.connect('schedule.db')
cur = con.cursor() cur = con.cursor()
now_secs = datetime.datetime.now().timestamp() now_str = datetime.datetime.now(tz=tz).isoformat(sep=' ')
for row in cur.execute('select * from schedule ' + \ now_str = now_str.split(".")[0]
'where date_start > ? and ' + \
'session_type = "Race" ' + \ query = "SELECT * FROM schedule WHERE date_start > ? AND " + \
'order by date_start ascending limit 1', "session_type = 'Race' ORDER BY date_start ASC LIMIT 1"
(now_secs)):
next_secs = row[2] cur.execute(query, (now_str,))
delta_seconds = next_secs - now_secs rows = cur.fetchall()
td = datetime.timedelta(seconds=delta_seconds)
next_date = datetime.datetime.fromtimestamp(next_secs) for row in rows:
message = f"Next event is {row[0]} at {next_date}. " + \ t1 = datetime.datetime.fromisoformat(now_str)
f"Time remaining {td}" t2 = datetime.datetime.fromisoformat(row[3])
await ctx.send(message) delta = t2 - t1
break # There should only be one row anyway
message = f"The next race is the {row[1]} which is {delta} from now."
await ctx.send(message)
break
except: except:
ctx.send("Sorry, hit the wall trying to find the answer...") await ctx.send("Sorry, hit the wall tring to find the next race.")
def __init__(self): def __init__(self):
# Set debug or not # Set debug or not