Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ The way I have found importing the package and its module to be most intuitive i
import hpa_menu.parse as menu

# Then using the package to create a python dict object of the week's menu data can be as easy as:
weeks_dictionary_data = menu.collect_week("YYYY-MM-DD") # Date must be in this format.
## note: default meal data will be lunch unless otherwise specified with the meal= parameter.
weeks_dictionary_data = menu.collect_week("YYYY-MM-DD", meal='dinner') # Date must be in this format. Only provide lowercase meal names ('breakfast, brunch, lunch, dinner')
```
The ```collect_week()``` and ```collect_month()``` function's take 1 required argument and 3 optional arguments.
The ```date``` argument for the ```collect_week()``` function must be a string in the "YYYY-MM-DD" format.

The optional arguments for the `collect_month()` function are:
`meal='lunch'(default)`: You can specify the meal to be collected, lunch is the default.
`full_week=False(default)`: True will include a whole weeks data even if some days in the begginning or ending week doesn't occur in the given month. For example if Feb 1 is a thursday, the dataset will also include the Mon Tues Wed of the previous January. False will not include another month's data.
`nutrition_info=True(default)`: True will include all the nutritional info for each dish in the nested dictionary with "nutrition info" as the key
`ingredients=True(default)`: True will include all the ingrediends that Flik provides for each dish in the nested dictionary with "ingredients" as the key.
Expand Down
Binary file added dist/hpa-menu-scraper-0.2.2.tar.gz
Binary file not shown.
Binary file removed dist/hpa_menu_scraper-0.1.5-py3-none-any.whl
Binary file not shown.
Binary file removed dist/hpa_menu_scraper-0.1.5.tar.gz
Binary file not shown.
Binary file added dist/hpa_menu_scraper-0.2.2-py3-none-any.whl
Binary file not shown.
22 changes: 21 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
[build-system]
requires = [
"requests",
"setuptools>=54",
"wheel"
]
build-backend = "setuptools.build_meta"

[project]
name = "hpa-menu-scraper"
description = "A package to scrape the HPA menu"
version = "0.2.2"
authors = [
{ name="Zeke Sarosi", email="zeke.sarosi@gmail.com" },
]

classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]



[project.urls]
"Homepage" = "https://github.com/zekesarosi/hpa_menu_package"
"Bug Tracker" = "https://github.com/zekesarosi/hpa_menu_package/issues"
Empty file added src/hpa_menu/__init__.py
Empty file.
Binary file added src/hpa_menu/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added src/hpa_menu/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added src/hpa_menu/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added src/hpa_menu/__pycache__/parse.cpython-310.pyc
Binary file not shown.
Binary file added src/hpa_menu/__pycache__/parse.cpython-311.pyc
Binary file not shown.
178 changes: 178 additions & 0 deletions src/hpa_menu/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import json
import requests
import threading



def save(data, file_name):
with open(file=file_name, mode='w') as file:
json.dump(data, file, indent=4)

def check_len(month, year):
months = {"0" + str(month) if len(str(month)) < 2 else str(month) for month in range(1, 13)}
three_months = {"04", "06", "09", "11"}
three_one_months = months - three_months - {"02"}
month = "0" + str(month) if len(str(month)) < 2 else str(month)
if month in months and month in three_one_months:
return 31
if month in months and month in three_months:
return 30
if month == "02":
if int(year) % 4 == 0:
return 29
else:
return 28

def collect_week_multi(date, lock, menulist, nutrition_info=True, ingredients=True):
url = week_url(date)
data = request(url)
menudict = dataparser(data, ingredients, nutrition_info)
lock.acquire()
menulist.append(menudict)
lock.release()
print(menudict)

def collect_month_multi(date, full_week=False, nutrition_info=True, ingredients=True):
lock = threading.Lock()
month_specified = date.split("-")[1]
date += "-01"
year, month, day = date.split('-')[0], date.split('-')[1], date.split('-')[2]
start_menu = collect_week(f"{year}-{month}-{day}", nutrition_info=nutrition_info, ingredients=ingredients)
menu_dict = {}
weeks = [ele*7 for ele in range(1,3)]
thread_list = []
menu_list = [start_menu]
for day in weeks:
thread = threading.Thread(target=collect_week_multi, args=[f"{year}-{month}-{day}", lock, menu_list, False, False])
thread.start()
thread_list.append(thread)
for t in thread_list:
t.join()
for d in menu_list:
menu_dict = menu_dict | d
delete = []
if not full_week:
for date in menu_dict.keys():
year, month, day = date.split('-')[0], date.split('-')[1], date.split('-')[2]
if month != month_specified:
delete.append(date)
for i in delete:
del menu_dict[i]
return menu_dict

# Pass in a month and a year. YY-MM. Either include or exclude Nutrition Info / Ingredients. full_week param when
# True will include a whole weeks data even if some days in the begginning or ending week doesn't occur in the given
# month. For example if Feb 1 is a thursday, the dataset will also include the Mon Tues Wed of the previous January.
def collect_month(date, meal='lunch', full_week=False, nutrition_info=True, ingredients=True):
month_specified = date.split("-")[1]
date += "-01"
year, month, day = date.split('-')[0], date.split('-')[1], date.split('-')[2]
start_menu = collect_week(f"{year}-{month}-{day}", meal, nutrition_info=nutrition_info, ingredients=ingredients)
menu_dict = {}
menu_list = [start_menu]
cycling = True
while cycling:
day = ("0" + str(int(day) + 7) if int(day) < 3 else int(day) + 7)
week_menu = collect_week(f"{year}-{month}-{day}", meal, nutrition_info=nutrition_info, ingredients=ingredients)
menu_list.append(week_menu)
if int(day) >= check_len(month, year):
cycling = False
for d in menu_list:
menu_dict = menu_dict | d
delete = []
if not full_week:
for date in menu_dict.keys():
year, month, day = date.split('-')[0], date.split('-')[1], date.split('-')[2]
if month != month_specified:
delete.append(date)
for i in delete:
del menu_dict[i]
return menu_dict


def collect_week(date, meal='lunch', nutrition_info=True, ingredients=True):
url = week_url(date, meal)
data = request(url)
menudict = dataparser(data, meal, ingredients, nutrition_info)

return menudict

def request(url):
try:
data = requests.get(url)
return json.loads(data.text)
except Exception as e:
print(e)
exit()


def week_url(date, meal):
school_info = {
"prefix": 'hawaiiprep.api',
"school_id": 'hawaii-preparatory-academy',
"menu_type": meal,
}
meta = school_info
date = date.split("-")
prefix, school_id, menu_type = meta["prefix"], meta["school_id"], meta["menu_type"]
year, month, day = date[0], date[1], date[2]
url = rf"https://{prefix}.flikisdining.com/menu/api/weeks/school/{school_id}/menu-type/{menu_type}/{year}/{month}/{day}"
return url

def dataparser(data, meal, include_ingredients=True, include_nutrition_data=True):
menudict = dict()
for day in data['days']:
if len(day['menu_items']) != 0:
date = day['date']
menudict[date] = []
daylist = list()
for info in day['menu_items']:
if len(info['text']) > 1:
station_name = info['text']
daylist.append(station_name)
else:
food_dict = info["food"]
if include_ingredients and include_nutrition_data:
daylist.append({
"name": food_dict['name'],
"ingredients": food_dict["ingredients"].split(","),
"nutrition info": food_dict["rounded_nutrition_info"],
})
elif include_nutrition_data and not include_ingredients:
daylist.append({
"name": food_dict['name'],
"nutrition info": food_dict["rounded_nutrition_info"],
})
elif include_ingredients and not include_nutrition_data:
daylist.append({
"name": food_dict['name'],
"ingredients": food_dict["ingredients"].split(","),
})
elif not include_ingredients and not include_nutrition_data:
daylist.append({
"name": food_dict['name']
})
if meal.lower() == 'dinner':
station_list = ["Entree/Sides","Chefs Table Dinner"]
elif meal.lower() == 'lunch':
station_list = ["Entree/Sides", "Chefs Table", "Soup"]
elif meal.lower() == 'breakfast':
station_list = ["Breakfast Specials"]
elif meal.lower() == 'brunch':
station_list = ["Entree/Sides"]
station_index_a = 0
day_dict = {}
ind = 1
for stations in station_list:
try:
station_index = daylist.index(station_list[ind])
except:
station_index = len(daylist)
dishes = [dish for dish in daylist[(station_index_a + 1) : (station_index)]]
station_index_a = station_index
ind += 1
day_dict[stations] = dishes
menudict[date] = day_dict
return menudict


11 changes: 11 additions & 0 deletions src/hpa_menu_scraper.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Metadata-Version: 2.1
Name: hpa-menu-scraper
Version: 0.2.2
Summary: A package to scrape the HPA menu
Author-email: Zeke Sarosi <zeke.sarosi@gmail.com>
Project-URL: Homepage, https://github.com/zekesarosi/hpa_menu_package
Project-URL: Bug Tracker, https://github.com/zekesarosi/hpa_menu_package/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
License-File: LICENSE
9 changes: 9 additions & 0 deletions src/hpa_menu_scraper.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LICENSE
README.md
pyproject.toml
src/hpa_menu/__init__.py
src/hpa_menu/parse.py
src/hpa_menu_scraper.egg-info/PKG-INFO
src/hpa_menu_scraper.egg-info/SOURCES.txt
src/hpa_menu_scraper.egg-info/dependency_links.txt
src/hpa_menu_scraper.egg-info/top_level.txt
1 change: 1 addition & 0 deletions src/hpa_menu_scraper.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/hpa_menu_scraper.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hpa_menu