def scrape_and_store_menu(): food_list = set() menus = {} for hall, url in dining_list.items(): result = parse_page(url) menus[hall] = result for _, items in result.items(): food_list = food_list.union(items) items = ', '.join('(%s)' for _ in food_list) final_all_item_query = insert_menu_query.format(items) db.execute_query(final_all_item_query, values=list(food_list), results=False) # Add the daily menu into the database item_query = "('{hall}', '{meal}', (SELECT food_id FROM dining.food WHERE name = %s LIMIT 1))" item_list = [] query_list = [] for hall, menu in menus.items(): for meal, items in menu.items(): for item in items: item_list.append(item) query_list.append(item_query.format(hall=hall, meal=meal)) final_query = insert_today.format(', '.join(query_list)) db.execute_query(final_query, values=item_list, results=False)
def _generate_temporary_modify_urls(items, group_id, i_or_d): response = "I couldn't find that exact item, did you mean\n" for item in items[:5]: token = _generate_random_string(8) db.execute_query(generate_saved_query, values=(token, group_id, item[0])) response += '- {item}\n({url}/{type}?t={token})\n'.format(item=item[1], url=URL, token=token, type=i_or_d) return response
def message_group(msg, group_id): # GroupMe limits the number of characters to post at ~1000, so if we exceed 990 chars, the message # needs to be broken up i = 0 messages = [] while (i < len(msg)): messages.append(msg[i:i + 990]) i += 990 # Identify the id to send the query to bot_id = db.execute_query( 'SELECT bot_id FROM dining.groups WHERE group_id = %s', values=group_id, results=True) if len(bot_id) > 0: bot_id = bot_id[0][0] if len(messages) == 1: sleep_time = 0 else: sleep_time = 0.5 for message in messages: data = {"bot_id": bot_id, "text": message} response = requests.post("https://api.groupme.com/v3/bots/post", data=data) if response.status_code < 400: print("Bot successfully posted message!") else: print("Error, message wasn't sent") time.sleep(sleep_time)
def scrape_and_store_hours(): item_query = "('{hall}', '{meal}', %s)" query_values = [] time_values = [] res = parse_hours() for hall, values in res.items(): # De-translation from the names that are presented on the webpage if hall == 'FEAST at Rieber': hall = 'FEAST' for meal, hours in values.items(): if meal == 'Late': meal = 'Late Night' query_values.append(item_query.format(hall=hall, meal=meal)) time_values.append(hours) query = insert_hours.format(', '.join(query_values)) db.execute_query(query, values=time_values)
def load_token_query(token, insert): result = db.execute_query(select_saved_query, values=token, results=True) if len(result) == 0: return [ 'Unable to {} the selected item'.format( 'add' if insert else 'remove') ] result = result[0] if insert: db.execute_query(add_food_id_query, values=(result[0], result[1])) db.execute_query(drop_token_query, values=token) return ['Now tracking {}'.format(result[2]), result[0]] else: db.execute_query(delete_food_id_query, values=(result[0], result[1])) db.execute_query(drop_token_query, values=token) return ['No longer tracking {}'.format(result[2]), result[0]]
def remove_tracked_item(name, group_id): query = get_food_id_query.format('group_id = %s AND ' + ' AND '.join('name ~* %s' for _ in name)) item_key = db.execute_query(query, values=[group_id] + name, results=True) if len(item_key) == 0: query = get_food_id_query.format(' AND '.join('name ~* %s' for _ in name)) item_key = db.execute_query(query, values=name, results=True) if len(item_key) == 0: return "{} doesn't appear to be a valid food item".format( ' '.join(name)) else: return "{} isn't being tracked".format(' '.join(name)) if len(item_key) > 1: return _generate_temporary_modify_urls(item_key, group_id, 'd') db.execute_query(delete_food_id_query, values=(group_id, item_key[0][0])) return "No longer tracking {}".format(item_key[0][1])
def add_tracked_item(name, group_id): query = get_food_id_query.format( '(group_id IS NULL OR group_id != %s) AND ' + ' AND '.join('name ~* %s' for _ in name)) item_key = db.execute_query(query, values=[group_id] + name, results=True) if len(item_key) == 0: query = get_food_id_query.format(' AND '.join('name ~* %s' for _ in name)) item_key = db.execute_query(query, values=name, results=True) if len(item_key) == 0: return "{} doesn't appear to be a valid food item".format( ' '.join(name)) else: return "{} is already being tracked".format(' '.join(name)) if len(item_key) > 1: return _generate_temporary_modify_urls(item_key, group_id, 'i') db.execute_query(add_food_id_query, values=(group_id, item_key[0][0])) return "Now tracking {}".format(item_key[0][1])
def purge_old_cached_queries(): deleted = db.execute_query(purge_query, results=True) return 'Deleted {} cached queries from the database'.format(len(deleted))