def IsFreeCommand(self, chat_id): try: start_date_query, end_date_query = self.str_text.split(';') except: raise err.ParseError # Call the GoogleAPI class and check isFree try: query = gc.GoogleAPI().FreeBusyQuery(start_date_query, end_date_query) isFree = gc.GoogleAPI().isFree(query) except: raise err.QueryError self.bot.sendMessage(chat_id, isFree) # Get the query's busy info if not isFree: info_busy = gc.GoogleAPI().BusyInfo(query) self.bot.sendMessage(chat_id, 'You are busy on this time interval!') self.bot.sendMessage(chat_id, "You have %d event(s) occuring between %s and %s" %(len(info_busy), start_date_query, end_date_query)) for i in range(len(info_busy)): # Ignoring timezones ignored_tz_start_busy = hc.StringParseGoogleAPI(info_busy[i]['start']).IgnoreTimeZone() ignored_tz_end_busy = hc.StringParseGoogleAPI(info_busy[i]['end']).IgnoreTimeZone() # Make these pretty start_busy_pretty = ignored_tz_start_busy.strftime('%Y-%m-%d %H:%M') end_busy_pretty = ignored_tz_end_busy.strftime('%Y-%m-%d %H:%M') # Send message to user self.bot.sendMessage(chat_id, "%d. %s until %s" %(i + 1, start_busy_pretty, end_busy_pretty)) raise err.IsNotFreeError return isFree
def IsFreeCommand(self): str_input = hc.StringParseGoogleAPI(self.str_text) str_input.ParseDateRange() start_date_query = str_input.start_date end_date_query = str_input.end_date # Call the GoogleAPI class and check isFree query = gc.GoogleAPI().FreeBusyQuery(start_date_query, end_date_query) isFree = gc.GoogleAPI().isFree(query) # Get the query's busy info if not isFree: info_busy = gc.GoogleAPI().BusyInfo(query) self.start_busy = info_busy[0] self.end_busy = info_busy[1] return isFree
def PreCreateEventIndex(self, evt_list): """Description: preparation to add the event from evt_list to Google Calendar""" for i in range(len(evt_list)): event1 = evt_list[i] course_index = event1[0] course_type = event1[1] course_group = event1[2] day = event1[3] start_time = event1[4][0] end_time = event1[4][1] location = event1[5] recurrence = event1[6] first_week = db.DB().table_query(self.chat_id, first_week=True)[0] first_recess_week = db.DB().table_query(self.chat_id, first_recess_week=True)[1] # Concatenate together event_summary = " ".join([course_code, course_type]) event_desc = " ".join([course_index, course_group]) ignore_first_event = False if day != 'MO': if recurrence.count('1') != 0 or recurrence != '' or recurrence.count('1') == 0: ignore_first_event = True # Recurrence Parsing recurrenceObject = hc.StringParseGoogleAPI(recurrence) recurrence_string = recurrenceObject.ParseOccurIgnoreWeek(first_week, start_time) # CreateEventIndex gc.GoogleAPI().CreateEventIndex(self.chat_id, event_summary, location, event_desc, start_time, end_time, first_week, first_recess_week, recurrence_string, day, ignore_first_event)
def getUpcomingEvent(self, chat_id): num_event = int(self.str_text) self.bot.sendMessage(chat_id, 'Getting %s upcoming event(s) for you' %(num_event)) events = gc.GoogleAPI().getUpcomingEventList(num_event) event_detail_list = [] inlines_keyboard = [] for event in events: # Getting the start, end, and summary start = event['start']['dateTime'] end = event['end']['dateTime'] summary = event['summary'] # Ignoring their timezones ignore_tz_start = hc.StringParseGoogleAPI(start).IgnoreTimeZone() ignore_tz_end = hc.StringParseGoogleAPI(end).IgnoreTimeZone() # Making these pretty ignore_tz_start_pretty = ignore_tz_start.strftime('%Y-%m-%d %H:%M') ignore_tz_end_pretty = ignore_tz_end.strftime('%Y-%m-%d %H:%M') # Combining all complete_event = summary + ' (' + ignore_tz_start_pretty + ' until ' + ignore_tz_end_pretty + ')' event_detail_list.append(complete_event) # Preparing inline keyboard for event_detail in event_detail_list: inlines_keyboard.append([InlineKeyboardButton(text=event_detail, callback_data=event_detail)]) keyboard = InlineKeyboardMarkup(inline_keyboard=inlines_keyboard) # Send the message to user self.bot.sendMessage(chat_id, 'Here they are!', reply_markup=keyboard)
def CreateEventCommand(self): str_input = hc.StringParseGoogleAPI(self.str_text) str_input.ParseEvent() event_name = str_input.event_name location = str_input.location start_date = str_input.start_date end_date = str_input.end_date # Call the GoogleAPI class and create event gc.GoogleAPI().createEvent(event_name, location, start_date, end_date)
def RemoveEventCommand(self, chat_id): query_data = self.str_text evt_name, start, end = query_data.split(';') excel = db.DB() other_event_id_str = excel.table_query(chat_id, other_event_id=True)[4] other_event_id_dict = json.loads(other_event_id_str) for key in list(other_event_id_dict.keys()): if other_event_id_dict[key]['name'] == evt_name and other_event_id_dict[key]['start'] == start and other_event_id_dict[key]['end'] == end: evt_id = key del(other_event_id_dict[evt_id]) other_event_id_update_str = json.dumps(other_event_id_dict) excel.update(chat_id, other_event_id=other_event_id_update_str) gc.GoogleAPI().deleteEvent(evt_id)
def RemoveCourseCommand(self, chat_id): course_code = self.str_text print(course_code) check_db = db.DB() course_code_db_str = check_db.table_query(chat_id, course_code_event_id=course_code)[3] course_code_db_obj = json.loads(course_code_db_str) # Remove it from Google Calendar Google = gc.GoogleAPI() try: for evt_id in course_code_db_obj[course_code]['event_id']: Google.deleteEvent(evt_id) except: self.error = 1 raise ValueError # Remove it from the database if not self.error: del(course_code_db_obj[course_code]) updated_course_code_str = json.dumps(course_code_db_obj) check_db.update(chat_id, course_code_event_id=updated_course_code_str)
def AddEventCommand(self, chat_id): try: # Parsing str_input = hc.StringParseGoogleAPI(self.str_text) str_input.ParseEvent() event_name = str_input.event_name location = str_input.location start_date = str_input.start_date start_date_pretty = str_input.start_time_cantik end_date_pretty = str_input.end_time_cantik end_date = str_input.end_date except: raise err.ParseError try: # Performing queries Google = gc.GoogleAPI() query = Google.FreeBusyQuery(start_date_pretty, end_date_pretty) isFree = Google.isFree(query) info_busy = Google.BusyInfo(query) except: raise err.QueryError('Unable to perform query') # Get the query's busy info if not isFree: print("not free!") self.bot.sendMessage(chat_id, "Cannot add event!") self.bot.sendMessage(chat_id, "You have %d event(s) occuring between %s and %s" %(len(info_busy), start_date_pretty, end_date_pretty)) for i in range(len(info_busy)): # Ignoring timezones ignored_tz_start_busy = hc.StringParseGoogleAPI(info_busy[i]['start']).IgnoreTimeZone() ignored_tz_end_busy = hc.StringParseGoogleAPI(info_busy[i]['end']).IgnoreTimeZone() # Make these pretty start_busy_pretty = ignored_tz_start_busy.strftime('%Y-%m-%d %H:%M') end_busy_pretty = ignored_tz_end_busy.strftime('%Y-%m-%d %H:%M') # Send message to user self.bot.sendMessage(chat_id, "%d. %s until %s" %(i + 1, start_busy_pretty, end_busy_pretty)) # Raise the error raise err.IsNotFreeError else: # Call the GoogleAPI class and create event current_event_id = gc.GoogleAPI().createEvent(event_name, location, start_date, end_date) # load the event to the database other_event_id_dict = { current_event_id: { 'name': event_name, 'location': location, 'start': start_date_pretty, 'end': end_date_pretty } } excel = db.DB() existing_data_str = excel.table_query(chat_id, other_event_id=True)[4] if excel.isRecordExist(chat_id, other_event_id=True): existing_data_dict = json.loads(existing_data_str) other_event_id_dict.update(existing_data_dict) other_event_id_str = json.dumps(other_event_id_dict) excel.update(chat_id, other_event_id=other_event_id_str)