Skip to content

Commit 495bff5

Browse files
batch translations
update cli remove dish sorting
1 parent f1a1d5f commit 495bff5

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,20 @@ The JSON files are produced by the tool shown in this repository. Hence, it is e
6161

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

6766
options:
6867
-h, --help show this help message and exit
6968
-p CANTEEN, --parse CANTEEN
7069
the canteen you want to eat at
71-
-d DATE, --date DATE date (DD.MM.YYYY) of the day of which you want to get
72-
the menu
73-
-c, --combine creates a "combined.json" file containing all dishes
74-
for the canteen specified
75-
--openmensa PATH directory for OpenMensa XML output (date parameter
76-
will be ignored if this argument is used)
77-
--canteens prints all available canteens formated as JSON
78-
--language LANGUAGE The language to translate the dish titles to, needs an
79-
DeepL API-Key in the environment variable
80-
DEEPL_API_KEY_EAT_API
81-
70+
--canteen-ids prints all available canteen IDs to stdout with a new line after each canteen
71+
--print-canteens prints all available canteens formated as JSON
72+
-d DATE, --date DATE date (DD.MM.YYYY) of the day of which you want to get the menu
73+
-j PATH, --jsonify PATH
74+
directory for JSON output (date parameter will be ignored if this argument is used)
75+
-c, --combine creates a "combined.json" file containing all dishes for the canteen specified
76+
--openmensa PATH directory for OpenMensa XML output (date parameter will be ignored if this argument is used)
77+
--language LANGUAGE The language to translate the dish titles into, needs an DeepL API-Key in the environment variable DEEPL_API_KEY_EAT_API
8278
```
8379
8480
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.

src/cli.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,35 @@ def parse_cli_args():
2020
# pylint:enable=protected-access
2121
help="the canteen you want to eat at",
2222
)
23-
parseGroup: argparse._MutuallyExclusiveGroup = group.add_argument_group(
24-
"parse",
25-
) # type: ignore # pylint: disable=protected-access
26-
parseGroup.add_argument("-d", "--date", help="date (DD.MM.YYYY) of the day of which you want to get the menu")
27-
parseGroup.add_argument(
23+
group.add_argument(
24+
"--canteen-ids",
25+
action="store_true",
26+
help="prints all available canteen IDs to stdout with a new line after each canteen",
27+
)
28+
group.add_argument(
29+
"--print-canteens",
30+
action="store_true",
31+
help="prints all available canteens formated as JSON",
32+
)
33+
34+
parser.add_argument("-d", "--date", help="date (DD.MM.YYYY) of the day of which you want to get the menu")
35+
parser.add_argument(
2836
"-j",
2937
"--jsonify",
3038
help="directory for JSON output (date parameter will be ignored if this argument is used)",
3139
metavar="PATH",
3240
)
33-
parseGroup.add_argument(
41+
parser.add_argument(
3442
"-c",
3543
"--combine",
3644
action="store_true",
3745
help='creates a "combined.json" file containing all dishes for the canteen specified',
3846
)
39-
parseGroup.add_argument(
47+
parser.add_argument(
4048
"--openmensa",
4149
help="directory for OpenMensa XML output (date parameter will be ignored if this argument is used)",
4250
metavar="PATH",
4351
)
44-
group.add_argument(
45-
"--canteens",
46-
action="store_true",
47-
help="prints all available canteens formated as JSON",
48-
)
49-
group.add_argument(
50-
"--canteen-ids",
51-
action="store_true",
52-
help="prints all available canteen IDs to stdout with a new line after each canteen",
53-
)
5452
parser.add_argument(
5553
"--language",
5654
help="The language to translate the dish titles to, "

src/main.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def main():
8686
# get command line args
8787
args = cli.parse_cli_args()
8888

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

107-
# sort dishes before translating to keep the order
108-
for menu in menus.values():
109-
menu.dishes.sort(key=lambda dish: dish.name)
110-
111106
# optionally translate the dish titles
112107
if args.language is not None and args.language.upper() != "DE":
113108
util.translate_dishes(menus, args.language)
@@ -123,14 +118,12 @@ def main():
123118

124119
# jsonify argument is set
125120
if args.jsonify is not None:
121+
os.makedirs(args.jsonify, exist_ok=True)
126122
weeks = Week.to_weeks(menus)
127-
if not os.path.exists(args.jsonify):
128-
os.makedirs(args.jsonify)
129123
jsonify(weeks, args.jsonify, canteen, args.combine)
130124
elif args.openmensa is not None:
125+
os.makedirs(args.openmensa, exist_ok=True)
131126
weeks = Week.to_weeks(menus)
132-
if not os.path.exists(args.openmensa):
133-
os.makedirs(args.openmensa)
134127
openmensa(weeks, args.openmensa)
135128
# date argument is set
136129
elif args.date is not None:

src/utils/util.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def make_duplicates_unique(names_with_duplicates):
3636

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

5050
translator = deepl.Translator(deepl_api_key)
5151

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

0 commit comments

Comments
 (0)