#!/bin/sh # # # dining.sh # # Charles Duan # # Retrieves the dining menu. # # # # USAGE: dining.sh [MEAL [DATE]] # # Meal may be any one of the words breakfast, lunch, dinner, etc.; all the # available meals should be printed out if a bad meal name is given. Date may be # any date format allowable by the GNU strtotime function; typical values are # "tomorrow", "next monday", or "Sept 12", all of which should work (given that # menus exist for those days). # # If no arguments are given, it will guess which meal is next. # # This script has been revised to work with the new HUDS menu web page. # # Determine the current time, so we can autodetect the meal. HOUR=`date +%H` DAY=`date +%w` # Determine which meal to use. if [ "$1" ] ; then case "$1" in breakfast|lunch|dinner|pasta|grill|deli|salad) MEAL=$1 ;; *) echo "Usage: `basename $0` [MEAL [DATE]]" echo 'MEAL is breakfast|lunch|dinner|pasta|grill|deli|salad' exit ;; esac else if [ $DAY -eq 0 ] ; then # If it's Sunday, only lunch and dinner if [ $HOUR -le 13 ] ; then MEAL='lunch' else MEAL='dinner' fi else if [ $HOUR -le 9 ] ; then MEAL='breakfast' else if [ $HOUR -le 13 ] ; then MEAL='lunch' else MEAL='dinner' fi fi fi fi # Determine the locationNum for the meal type case "$MEAL" in pasta|grill) LOCAT=08 ;; deli|salad) LOCAT=15 ;; *) LOCAT=05 ;; esac # If there's a date argument, then reformat the date into mm/dd/yyyy. $DATE will # contain the GET request specifier for the URL, preceded by an ampersand. if [ "$2" ] ; then DATE="&dtdate=`date +"%m/%d/%Y" "-d$2"`" else DATE="" fi # Print out a header line. case "$HOST" in is??) echo "\033[1m$MEAL menu\033[0m" ;; *) echo -e "\033[1m$MEAL menu\033[0m" ;; esac echo '' if [ "$ISTYPE" = ws ] ; then DOS2UNIX="sed s/.$//" else DOS2UNIX=dos2unix fi # PRE_MEAL and POST_MEAL are prepended and appended to the meal name, for the # purposes of regular expression matching. PRE_MEAL='.*' POST_MEAL='.*<\/b><\/font>' # PAGE is the web page to download. OPTIONS is all of the GET specifications for # the page. PAGE="http://www.huds.harvard.edu/foodpro/menusamp.asp" OPTIONS="myaction=read&locationNum=$LOCAT$DATE" # Retrieve the menu. I wish I could comment inside it, but I can't... # The way it works is as follows: # # 1. Get the page via WGET to STDOUT # 2. dos2unix it (remove ^M) # 3. Transpose the meal name to lowercase # 4. Delete everything from the first line to $PRE_MEAL$MEAL$POST_MEAL # 5. Delete everything from the end marker () to EOF # 6. Get rid of HTML tags # 7. Get rid of empty lines # wget -qO- "$PAGE?$OPTIONS" \ | $DOS2UNIX \ | sed -e "s/$MEAL/$MEAL/gi" \ -e "s/ / /g" \ -e "1,/$PRE_MEAL$MEAL$POST_MEAL/d" \ -e "/$PRE_MEAL/,\$d" \ -e 's/<[^>]*>//g' \ -e 's/^[ ]\+//;/^$/d' \ -e '/^--.*--$/d' \ -e '/Powered by FoodPro/d' echo ''