""" Parse the weekly menu from a webpage into a json struct and write it to a file. """

import json, urllib, libxml2, os, os.path

API_VERSION = "0.1"

def get_menu_page (week):
	f = urllib.urlopen("http://www.ugent.be/nl/voorzieningen/resto/studenten/menu/weekmenu/week%02d.htm" % week)
	return f.read()

def get_meat_and_price (meat):
	content = meat.content.split(' - ')
	result = {}
	result['recommended'] = len(meat.xpathEval('.//u')) > 0
	result['price'] = content[0]
	result['name'] = content[1]
	return result

def parse_menu_from_html (page):
	# replace those pesky non-breakable spaces
	page = page.replace('&nbsp;', '')
	
	doc = libxml2.htmlParseDoc(page, 'utf-8')
	menuElement = doc.xpathEval("//div[@id='parent-fieldname-text']")
	rows = menuElement[0].xpathEval('.//tr')[1:-2]
	
	week = doc.xpathEval("//span[@id='parent-fieldname-title']")[0].content.strip().split()
	#monday = datetime.datetime.strptime("%s %s %s" % (week[2], week[3], week[7]), "%d %B %Y")
	
	menu = {}
	day = None
	for row in rows:
		fields = row.xpathEval('.//td')
		if len(fields[0].content) != 0:
			# first row of a day
			day = fields[0].content
			menu[day] = {}
			if fields[2].content == 'Gesloten':
				menu[day]['open'] = False
			else:
				menu[day]['open'] = True
				menu[day]['soup'] = {'name' : fields[1].content}
				menu[day]['meat'] = []
				menu[day]['meat'].append(get_meat_and_price(fields[2]))
				menu[day]['vegetables'] = []
				menu[day]['vegetables'].append(fields[3].content)
		elif len(fields[1].content) != 0:
			# second row of a day
			menu[day]['soup']['price'] = fields[1].content
			menu[day]['meat'].append(get_meat_and_price(fields[2]))
			menu[day]['vegetables'].append(fields[3].content)
		else:
			# the third and forth row of a day
			menu[day]['meat'].append(get_meat_and_price(fields[2]))
	return menu

def dump_menu_to_file (week, menu):
	path = './resto/api/%s/week/' % API_VERSION
	if not os.path.isdir(path):
		os.makedirs(path)
	f = open ('%s/%s.json' % (path, week), 'w')
	json.dump(menu, f, sort_keys=True, indent=4)
	f.close()

if __name__ == "__main__":
	week = 6
	page = get_menu_page(week)
	menu = parse_menu_from_html(page)
	dump_menu_to_file(week, menu)


@@ -123,16 +123,75 @@
  os.path
+, datetime, locale%0Afrom datetime import datetime, timedelta
 %0A%0AAPI_VE
@@ -231,16 +231,51 @@
 (week):%0A
+%09print %22Fetching weekmenu webpage%22%0A
 %09f = url
@@ -430,43 +430,129 @@
 ):%0A%09
-content = meat.content.split(' - ')
+# splitting on '-' doesn't work, FAIL!%0A%09name = meat.content%5B8:%5D%0A%09try:%0A%09%09name = name%5Bname.index('-')+1:%5D%0A%09except:%0A%09%09pass%0A%09
 %0A%09re
@@ -637,24 +637,29 @@
 ice'%5D = 
+meat.
 content%5B
 0%5D%0A%09resu
@@ -650,17 +650,18 @@
 content%5B
-0
+:8
 %5D%0A%09resul
@@ -676,18 +676,20 @@
 %5D = 
-content%5B1%5D
+name.strip()
 %0A%09re
@@ -734,16 +734,68 @@
 (page):%0A
+%09print %22Parsing weekmenu webpage to an object tree%22%0A
 %09# repla
@@ -1121,27 +1121,226 @@
 ()%0A%09
-#monday = datetime.
+if len(week) == 7:%0A%09%09# start and end of week are in the same month%0A%09%09monday = datetime.strptime(%22%25s %25s %25s%22 %25 (week%5B2%5D, week%5B5%5D, week%5B6%5D), %22%25d %25B %25Y%22)%0A%09else:%0A%09%09# start and end of week are in different months%0A%09%09monday = 
 date
@@ -1428,15 +1428,18 @@
 %09day
- = None
+OfWeek = 0
 %0A%09fo
@@ -1554,33 +1554,75 @@
 %09%09day = 
-fields%5B0%5D.content
+str(monday.date() + timedelta(dayOfWeek))%0A%09%09%09dayOfWeek += 1
 %0A%09%09%09menu
@@ -2344,16 +2344,68 @@
  menu):%0A
+%09print %22Writing object tree to file in json format%22%0A
 %09path = 
@@ -2620,24 +2620,74 @@
 ain__%22:%0A
+%09locale.setlocale(locale.LC_ALL, ('nl_BE.UTF-8'))%0A
 %09week = 
 6%0A%09page 
@@ -2678,17 +2678,17 @@
 %09week = 
-6
+8
 %0A%09page =

Add date instead of name of the day, fix parsing of prices & names of meat