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

View File

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