Skip to content

Commit

Permalink
feat: add allergen info (#6)
Browse files Browse the repository at this point in the history
* add explanations, rework arg passing

* fix webdriver bug

* update pre-commit

* update requirements.txt
  • Loading branch information
jkerola authored Aug 18, 2023
1 parent d669ee7 commit 1147748
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ repos:
exclude: "pre-commit-config.yaml"
# Prettier for any YAML, README
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.0-alpha.9-for-vscode" # Use the sha or tag you want to point at
rev: "v3.0.2" # Use the sha or tag you want to point at
hooks:
- id: prettier
additional_dependencies:
- prettier@2.7.1
# Black formatter
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
Expand All @@ -27,7 +27,7 @@ repos:
# Ruff linter
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.0.265"
rev: "v0.0.285"
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
15 changes: 14 additions & 1 deletion jmenu/restaurants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import namedtuple

Restaurant = namedtuple("Restaurant", ["name", "url"])

Marking = namedtuple("Marking", ["letters", "explanation"])
RESTAURANTS = [
Restaurant("Foobar", "https://fi.jamix.cloud/apps/menu/?anro=93077&k=49&mt=84"),
Restaurant("Foodoo", "https://fi.jamix.cloud/apps/menu/?anro=93077&k=48&mt=89"),
Expand All @@ -10,3 +10,16 @@
Restaurant("Mara", "https://fi.jamix.cloud/apps/menu/?anro=93077&k=49&mt=111"),
Restaurant("Napa", "https://fi.jamix.cloud/apps/menu/?anro=93077&k=48&mt=79"),
]

MARKINGS = [
Marking("G", "Gluteeniton"),
Marking("M", "Maidoton"),
Marking("L", "Laktoositon"),
Marking("SO", "Sisältää soijaa"),
Marking("SE", "Sisältää selleriä"),
Marking("MU", "Munaton"),
Marking("[S], *", "Kelan korkeakouluruokailunsuosituksen mukainen"),
Marking("SIN", "Sisältää sinappia"),
Marking("<3", "Sydänmerkki"),
Marking("VEG", "Vegaani"),
]
54 changes: 32 additions & 22 deletions jmenu/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
from version import VERSION
from restaurants import RESTAURANTS
from restaurants import RESTAURANTS, MARKINGS
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
import argparse
from time import time
from webdriver_manager.chrome import ChromeDriverManager


class ArgsNamespace:
explain: bool
allergens: list[str]


def main():
args = get_args()
if args.explain:
print_explanations()
exit(0)
start = time()

allergens = []
if args.allergens:
allergens = [" " + x.upper() for x in args.allergens]
print_menu(allergens, args.hide)
print_menu(args)
print("Process took {:.2f} seconds.".format(time() - start))


Expand All @@ -34,10 +40,13 @@ def get_args():
version=VERSION,
)
parser.add_argument(
"--hide",
"-e",
"--explain",
dest="explain",
action="store_true",
help="Hide bad results instead of highlighting good ones",
help="Display allergen and marking information",
)

allergens = parser.add_argument_group("highlighting allergens")
allergens.add_argument(
"-a",
Expand All @@ -49,7 +58,7 @@ def get_args():
nargs="+",
help='List of allergens, ex. "g veg"',
)
return parser.parse_args()
return parser.parse_args(namespace=ArgsNamespace())


def get_selenium_opts() -> Options:
Expand All @@ -60,7 +69,10 @@ def get_selenium_opts() -> Options:

def get_soup(url: str) -> BeautifulSoup:
try:
driver = webdriver.Chrome(options=get_selenium_opts())
driver = webdriver.Chrome(
service=ChromeService(ChromeDriverManager().install()),
options=get_selenium_opts(),
)
driver.get(url)
cond = expected_conditions.presence_of_element_located(
(
Expand All @@ -80,13 +92,15 @@ def parse_soup(soup: BeautifulSoup) -> list[str]:
if len(results) == 0:
return []
results = soup.find_all("span", attrs={"class": "menu-item"})
menu = []
for result in results:
menu.append(result.text)
menu = [result.text for result in results]
return menu


def print_menu(allergens: list[str], hide: bool = False):
def print_menu(args: ArgsNamespace):
allergens = []
if args.allergens:
allergens = [" " + x.upper() for x in args.allergens]

print_header()
for res in RESTAURANTS:
try:
Expand All @@ -99,19 +113,15 @@ def print_menu(allergens: list[str], hide: bool = False):
for item in items:
print("\t", item)
else:
if not hide:
print_highlight(items, allergens)
else:
print_hide(items, allergens)
print_highlight(items, allergens)

except Exception:
print("Couldn't fetch menu for", res.name)


def print_hide(items: list[str], allergens: list[str]):
for item in items:
if any(marker in item for marker in allergens):
print("\t", item)
def print_explanations():
for mark in MARKINGS:
print(mark.letters, "\t", mark.explanation)


def print_highlight(items: list[str], allergens: list[str]):
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ attrs==23.1.0
beautifulsoup4==4.12.2
bs4==0.0.1
certifi==2023.5.7
charset-normalizer==3.2.0
exceptiongroup==1.1.1
h11==0.14.0
idna==3.4
outcome==1.2.0
packaging==23.1
PySocks==1.7.1
python-dotenv==1.0.0
requests==2.31.0
selenium==4.9.1
sniffio==1.3.0
sortedcontainers==2.4.0
soupsieve==2.4.1
trio==0.22.0
trio-websocket==0.10.2
urllib3==2.0.2
webdriver-manager==4.0.0
wsproto==1.2.0

0 comments on commit 1147748

Please sign in to comment.