diff --git a/plugins/admidio_events/admidio_events.py b/plugins/admidio_events/admidio_events.py index 44c601a..7e71987 100644 --- a/plugins/admidio_events/admidio_events.py +++ b/plugins/admidio_events/admidio_events.py @@ -14,7 +14,6 @@ import threading import importlib import sys import datetime as dt -import MySQLdb as mysql from matrix_bot_api.mregex_handler import MRegexHandler @@ -70,13 +69,9 @@ class Plugin: self.event_thread = threading.Thread(target=self.check_new_event_thread) self.event_thread.start() - def connect_database(self): - # Connect to database - return mysql.connect( - user = self.config['database_credentials']['username'], - password = self.config['database_credentials']['password'], - database = self.config['database_credentials']['database'], - host = self.config['database_credentials']['host']) + def ready_block(self): + while (not self.adm.ready): + time.sleep(1) def check_new_event_thread(self): while not self.bot.cancel: @@ -92,24 +87,11 @@ class Plugin: saved in an SQLite database. """ - mysql_db = self.connect_database() - mysql_cursor = mysql_db.cursor() - - # Grab all events from database that start in the future - select_sql = """SELECT dat_id, dat_cat_id, - dat_headline, dat_begin, dat_end - FROM adm_dates - WHERE dat_begin >'{}'""".format( - dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) - - mysql_cursor.execute(select_sql) - - # Fetch events - results = mysql_cursor.fetchall() + # Refresh everything + self.adm.refresh() # Open SQLite database sqlite_db = sqlite3.connect(DATA_LOCATION) - sqlite_cursor = sqlite_db.cursor() # Define query to SELECT event from SQLite DB @@ -121,9 +103,12 @@ class Plugin: VALUES(:dat_id, :datetime_posted)""" # Loop through event - for row in results: + now = dt.datetime.now() + events = (x for (k,x) in self.adm.events.items() if x.start_time > now) + + for (i, event) in enumerate(events): # First, check if a message on this event was already sent - sqlite_cursor.execute(select_sql.format(row[0])) + sqlite_cursor.execute(select_sql.format(event.id)) if sqlite_cursor.fetchall(): # Do nothing. This event was already processed @@ -132,56 +117,64 @@ class Plugin: # This appears to be a new event. Process it! # Generate links - base_date_url = "{}{}".format( - self.config['base_url_adm'], - "/adm_program/modules/dates/") - - view_link = "{}dates.php?id={}&view_mode=html&" - view_link += "view=detail&headline={}" - view_link = view_link.format(base_date_url, row[0], row[2]) - - function_link = "{}dates_function.php?mode={}&dat_id={}&" - - attend_link = function_link.format(base_date_url, '3', row[0]) - maybe_link = function_link.format(base_date_url, '7', row[0]) - cancel_link = function_link.format(base_date_url, '4', row[0]) + links = self.generate_links(event.id, event.name) # First, write to SQLite database that it is processed sqlite_cursor.execute( insert_sql, { - 'dat_id':row[0], + 'dat_id':event.id, 'datetime_posted': dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}) # Check role ID of event and check if config defines # where it should be shared. for mapping in self.config['cat_id_room_mapping']: - if row[1] in mapping[0]: + if event.cat_id in mapping[0]: # We got a winner! for i, room in self.bot.client.rooms.items(): if room.display_name in mapping[1]: room.send_html( self.messages["new_event"].format( - row[3].strftime("%d-%m-%Y"), - row[3].strftime("%H:%M"), - row[2], - view_link, - attend_link, - maybe_link, - cancel_link)) + event.start_time.strftime("%d-%m-%Y"), + event.start_time.strftime("%H:%M"), + event.name, + links['view'], + links['attend'], + links['maybe'], + links['cancel'])) # Commit and close queries sqlite_db.commit() sqlite_db.close() - # Close MySQL - mysql_db.close() - # Sleep time.sleep(self.config['update_time_span'] * 60) + def generate_links(self, event_id, event_name): + links = dict() + + # Define the base URL for users to attend + base_dat_url = "{}{}".format( + self.config['base_url_adm'], + "/adm_program/modules/dates/") + + links['view'] = "{}dates.php?id={}&view_mode=html&" + links['view'] += "view=detail&headline={}" + links['view'] = links['view'].format(base_dat_url, event_id, event_name) + + function_link = "{}dates_function.php?mode={}&dat_id={}&" + + links['attend'] = function_link.format(base_dat_url, '3', event_id) + links['maybe'] = function_link.format(base_dat_url, '7', event_id) + links['cancel'] = function_link.format(base_dat_url, '4', event_id) + + return links + + def list_callback(self, room, event): + self.ready_block() + number_events = False for word in event['content']['body'].split(): @@ -216,15 +209,16 @@ class Plugin: if (i >= number_events): break; - attend_link = function_link.format(base_date_url, '3', event.id) + # Generate links + links = self.generate_links(event.id, event.name) html_message += "
{}+ + Je kunt gebruik maken van onderstaande links als je aangemeld bent op + de database in je browser. Klik hier + ''', "unknown_event":