Skip to content

Commit

Permalink
batch translations
Browse files Browse the repository at this point in the history
update cli
remove dish sorting
  • Loading branch information
Friendly-Banana committed Feb 26, 2025
1 parent f1a1d5f commit 495bff5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 46 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,20 @@ The JSON files are produced by the tool shown in this repository. Hence, it is e

```bash
$ python3 src/main.py --help
usage: main.py [-h] [-p CANTEEN] [-d DATE] [-j PATH] [-c] [--openmensa PATH]
[--canteens] [--language LANGUAGE]
usage: main.py [-h] (-p CANTEEN | --canteen-ids | --print-canteens) [-d DATE] [-j PATH] [-c] [--openmensa PATH] [--language LANGUAGE]

options:
-h, --help show this help message and exit
-p CANTEEN, --parse CANTEEN
the canteen you want to eat at
-d DATE, --date DATE date (DD.MM.YYYY) of the day of which you want to get
the menu
-c, --combine creates a "combined.json" file containing all dishes
for the canteen specified
--openmensa PATH directory for OpenMensa XML output (date parameter
will be ignored if this argument is used)
--canteens prints all available canteens formated as JSON
--language LANGUAGE The language to translate the dish titles to, needs an
DeepL API-Key in the environment variable
DEEPL_API_KEY_EAT_API

--canteen-ids prints all available canteen IDs to stdout with a new line after each canteen
--print-canteens prints all available canteens formated as JSON
-d DATE, --date DATE date (DD.MM.YYYY) of the day of which you want to get the menu
-j PATH, --jsonify PATH
directory for JSON output (date parameter will be ignored if this argument is used)
-c, --combine creates a "combined.json" file containing all dishes for the canteen specified
--openmensa PATH directory for OpenMensa XML output (date parameter will be ignored if this argument is used)
--language LANGUAGE The language to translate the dish titles into, needs an DeepL API-Key in the environment variable DEEPL_API_KEY_EAT_API
```
It is mandatory to specify the canteen (e.g. mensa-garching). Furthermore, you can specify a date, for which you would like to get the menu. If no date is provided, all the dishes for the current week will be printed to the command line. the `--jsonify` option is used for the API and produces some JSON files containing the menu data.
Expand Down
32 changes: 15 additions & 17 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,35 @@ def parse_cli_args():
# pylint:enable=protected-access
help="the canteen you want to eat at",
)
parseGroup: argparse._MutuallyExclusiveGroup = group.add_argument_group(
"parse",
) # type: ignore # pylint: disable=protected-access
parseGroup.add_argument("-d", "--date", help="date (DD.MM.YYYY) of the day of which you want to get the menu")
parseGroup.add_argument(
group.add_argument(
"--canteen-ids",
action="store_true",
help="prints all available canteen IDs to stdout with a new line after each canteen",
)
group.add_argument(
"--print-canteens",
action="store_true",
help="prints all available canteens formated as JSON",
)

parser.add_argument("-d", "--date", help="date (DD.MM.YYYY) of the day of which you want to get the menu")
parser.add_argument(
"-j",
"--jsonify",
help="directory for JSON output (date parameter will be ignored if this argument is used)",
metavar="PATH",
)
parseGroup.add_argument(
parser.add_argument(
"-c",
"--combine",
action="store_true",
help='creates a "combined.json" file containing all dishes for the canteen specified',
)
parseGroup.add_argument(
parser.add_argument(
"--openmensa",
help="directory for OpenMensa XML output (date parameter will be ignored if this argument is used)",
metavar="PATH",
)
group.add_argument(
"--canteens",
action="store_true",
help="prints all available canteens formated as JSON",
)
group.add_argument(
"--canteen-ids",
action="store_true",
help="prints all available canteen IDs to stdout with a new line after each canteen",
)
parser.add_argument(
"--language",
help="The language to translate the dish titles to, "
Expand Down
13 changes: 3 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def main():
# get command line args
args = cli.parse_cli_args()

# print canteens
if args.canteens:
if args.print_canteens:
sys.exit(enum_json_creator.enum_to_api_representation_dict(list(Canteen)))
if args.canteen_ids:
for c in list(Canteen):
Expand All @@ -104,10 +103,6 @@ def main():
if menus is None:
sys.exit("Error. Could not retrieve menu(s)")

# sort dishes before translating to keep the order
for menu in menus.values():
menu.dishes.sort(key=lambda dish: dish.name)

# optionally translate the dish titles
if args.language is not None and args.language.upper() != "DE":
util.translate_dishes(menus, args.language)
Expand All @@ -123,14 +118,12 @@ def main():

# jsonify argument is set
if args.jsonify is not None:
os.makedirs(args.jsonify, exist_ok=True)
weeks = Week.to_weeks(menus)
if not os.path.exists(args.jsonify):
os.makedirs(args.jsonify)
jsonify(weeks, args.jsonify, canteen, args.combine)
elif args.openmensa is not None:
os.makedirs(args.openmensa, exist_ok=True)
weeks = Week.to_weeks(menus)
if not os.path.exists(args.openmensa):
os.makedirs(args.openmensa)
openmensa(weeks, args.openmensa)
# date argument is set
elif args.date is not None:
Expand Down
12 changes: 6 additions & 6 deletions src/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def make_duplicates_unique(names_with_duplicates):

def translate_dishes(menus: Dict[date, Menu], language: str) -> None:
"""
Translate the dish titles of a menu
Translate the dish titles of a menu. Source language is always german.
:param menus: Menus dictionary as given by the menu parser, will be modified
:param language: Identifier for a language
Expand All @@ -49,9 +49,9 @@ def translate_dishes(menus: Dict[date, Menu], language: str) -> None:

translator = deepl.Translator(deepl_api_key)

# traverse through all dish titles
# batch dish names to prevent being rate limited
for menu in menus.values():
for dish in menu.dishes:
# source language is always german
result = translator.translate_text(dish.name, source_lang="DE", target_lang=language)
dish.name = result.text
batch = [dish.name for dish in menu.dishes]
results = translator.translate_text(batch, source_lang="DE", target_lang=language)
for dish, name in zip(menu.dishes, results):
dish.name = name

0 comments on commit 495bff5

Please sign in to comment.