<commit_before># Scraper for HMC (Hoch-Shanahan) dining hall.

import requests
from datetime import datetime
from bs4 import BeautifulSoup

class MuddBackend(object):
    def __init__(self):
        self.DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

    def _get_menu_data(self, week_number):
        resp = requests.get('http://hmcdining.com/Week%d.htm' % week_number)
        if resp.status_code == 404: # Sometimes Mudd does not update its menu on time...
            return None
        else:
            return BeautifulSoup(resp.text)

    def _parse_menu_data(self, menu_data):
        current_meal = None

        # Menu structure to return
        menus = {
            'mon': {}, # Each day dict contains key value pairs as meal_name, [fooditems]
            'tue': {},
            'wed': {},
            'thu': {},
            'fri': {},
            'sat': {},
            'sun': {}
        }

        if not menu_data: # If the data can't be loaded for some reason, just return an empty dict
            return menus

        for day in self.DAYS:
            day_node = menu_data.find(id=day) # i.e. Find all the meals that correspond to 'monday' first

            day_elements = day_node.find_all('tr')

            for element in day_elements:
                if len(element.find_all('td', {'class':'mealname'})):
                    current_meal = element.find_all('td', {'class':'mealname'})[0].text.lower()
                    continue
                elif element.find('div', {'class':'menuitem'}) and element.find('div', {'class':'menuitem'}).find('span'):
                    try:
                        menus[day[:3].lower()][current_meal].append(element.find('div', {'class':'menuitem'}).find('span').text)
                    except KeyError: # Create the list if nothing has been loaded yet for this day's current meal
                        menus[day[:3].lower()][current_meal] = [element.find('div', {'class':'menuitem'}).find('span').text]

        return menus

    def menu(self):
        # HMC stupidly changes the url to their menu every week (honestly, who conceived of this...?)
        # so we have to calculate the difference in weeks from now and the start of term
        # This code is fairly unstable and should be checked at the beginning of each semester at the very least
        start_date = datetime(year=2014, month=1, day=19)
        week_number = (datetime.today() - start_date).days / 7 + 1

        return self._parse_menu_data(self._get_menu_data(week_number))<commit_msg>Updates Mudd's URL. Might have to do this manually weekly.<commit_after># Scraper for HMC (Hoch-Shanahan) dining hall.

import requests
from datetime import datetime
from bs4 import BeautifulSoup

class MuddBackend(object):
    def __init__(self):
        self.DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

    def _get_menu_data(self, week_number):
        # The old URL used to be at least somewhat predicable, now it seems to change weekly on the whim of whoever updates the menu
        #menu_url = 'http://hmcdining.com/Week%d.htm' % week_number
        menu_url = 'http://www.hmcdining.com/Wk1Spring.htm'
        resp = requests.get(menu_url)
        if resp.status_code == 404: # Sometimes Mudd does not update its menu on time...
            return None
        else:
            return BeautifulSoup(resp.text)

    def _parse_menu_data(self, menu_data):
        current_meal = None

        # Menu structure to return
        menus = {
            'mon': {}, # Each day dict contains key value pairs as meal_name, [fooditems]
            'tue': {},
            'wed': {},
            'thu': {},
            'fri': {},
            'sat': {},
            'sun': {}
        }

        if not menu_data: # If the data can't be loaded for some reason, just return an empty dict
            return menus

        for day in self.DAYS:
            day_node = menu_data.find(id=day) # i.e. Find all the meals that correspond to 'monday' first

            day_elements = day_node.find_all('tr')

            for element in day_elements:
                if len(element.find_all('td', {'class':'mealname'})):
                    current_meal = element.find_all('td', {'class':'mealname'})[0].text.lower()
                    continue
                elif element.find('div', {'class':'menuitem'}) and element.find('div', {'class':'menuitem'}).find('span'):
                    try:
                        menus[day[:3].lower()][current_meal].append(element.find('div', {'class':'menuitem'}).find('span').text)
                    except KeyError: # Create the list if nothing has been loaded yet for this day's current meal
                        menus[day[:3].lower()][current_meal] = [element.find('div', {'class':'menuitem'}).find('span').text]

        return menus

    def menu(self):
        # HMC stupidly changes the url to their menu every week (honestly, who conceived of this...?)
        # so we have to calculate the difference in weeks from now and the start of term
        # This code is fairly unstable and should be checked at the beginning of each semester at the very least
        start_date = datetime(year=2014, month=1, day=19)
        week_number = (datetime.today() - start_date).days / 7 + 1

        return self._parse_menu_data(self._get_menu_data(week_number))
{'fraction_non_alphanumeric': 0.12090353638215146, 'fraction_numerical': 0.006109979633401222, 'mean_word_length': 2.324307692307692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7455843', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1491, 'n_words': 476}