def add_reminder(self, r: Reminder, context: CallbackContext): # TODO: optimize cursor = self.db_connection().cursor() query = '''INSERT INTO Reminder VALUES({},"{}","","{}","{}",{},{},{},{},{},{})'''.format( r.near_ts, r.chat_id, r.time_form, r.name, r.hour, r.minute, r.second, r.day, r.month, r.year) cursor.execute(query) self.db_connection().commit() cursor.close() if datetime.datetime.now(tz=pytz.FixedOffset(r.utc * 60)).replace( hour=23, minute=59, second=59).timestamp() > r.near_ts: context.job_queue.run_once( remind, 1 + r.near_ts - datetime.datetime.now( tz=pytz.FixedOffset(r.utc * 60)).timestamp(), context=(self, r), name=r.id())
def daily_update(self, context: CallbackContext): # TODO: optimize cursor = self.db_connection().cursor() cursor.execute( '''SELECT chat_id, time_form FROM Reminder WHERE near_ts < {}'''. format((datetime.datetime.now() + datetime.timedelta(days=1)).timestamp())) for row in cursor.fetchall(): try: c = self.get_chat(row[0]) r = Reminder(chat_id=row[0], utc=c.utc, time_form=eval(row[1])) context.job_queue.run_once( remind, 1 + r.near_ts - datetime.datetime.now( tz=pytz.FixedOffset(c.utc * 60)).timestamp(), context=(self, r), name=r.id()) except RuntimeError as e: cursor.execute( '''DELETE FROM Reminder WHERE chat_id = "{}" and time_form = "{}"''' .format(row[0], row[1])) self.db_connection().commit() cursor.close()