-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_recipe.py
executable file
·71 lines (56 loc) · 1.56 KB
/
set_recipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""
Typeset a recipe
"""
import argparse
import gzip
import json
import pprint
RECIPE_TEMPLATE = """---
title: {title}
author: {author}
date: {date}
papersize: a4
---
Dauer
: {preptime}
Schwierigkeit
: {difficulty}
Bewertung
: {rating} ({rates} Bewertungen)
## Zutaten (für {servings} Portionen)
{ingredients}
## Zubereitung
{instructions}
"""
def format_comments(comments):
"""Formats ingredients"""
out = ""
for comment in comments:
out += f"{comment['user']}\n:{' '.join(comment['text'].split())}\n\n"
return out.strip()
def format_ingredients(ingredients):
"""Formats ingredients"""
out = ""
for ingredient in ingredients:
out += f"{ingredient[0]}\n: {' '.join(ingredient[1])}\n\n"
return out.strip()
def recipe_to_markdown(recipe):
"""Converts recipe to markdown"""
recipe['ingredients'] = format_ingredients(recipe['ingredients'])
return RECIPE_TEMPLATE.format(**recipe)
def main():
"""Main function"""
parser = argparse.ArgumentParser(description="Typeset a recipe")
parser.add_argument("input", nargs=1, type=str, help="Recipe to typeset")
parser.add_argument("-o", "--output", nargs="?", type=str, help="File to output recipe at")
args = parser.parse_args()
with gzip.open(args.input[0], 'rt') as connection:
recipe = json.load(connection)
if args.output:
with open(args.output, 'w') as connection:
connection.write(recipe_to_markdown(recipe))
else:
print(recipe_to_markdown(recipe))
if __name__ == "__main__":
main()