-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhats-for-dinner.py
executable file
·95 lines (73 loc) · 2.26 KB
/
whats-for-dinner.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
import random, re, json, os, sys, getopt
from urllib.request import urlopen
# Keep track of chosen dinners.
debug = False
source = ''
weekdays = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
##
# Provides the dinners as JSON from source.
##
def get_dinners(source):
# Fetches the dinner source. A source is a json file containing a string for each day.
# Example: [ "Dinner #1", "Dinner #2" ] or objects: [ { name: "Dinner name" } ]
content = ''
# Test if we must download the source.
# Only check some protocols, as files on the fs can be named "dinners.json"
if re.match('(htt|ft)(p|ps):\/\/', source):
if debug:
print("> Downloading source " + source)
# The source is a URL, must download it if I can.
content = urlopen(source).read()
else:
if debug:
print("> Reading source " + source)
content = open(source, 'r').read()
# Finally, parse the content as json.
return json.loads(content)
def get_dinner_for_day(dinners, day):
# Attempt to find a dinner randomly.
# This might hang forever if no dinners are ever found.
while True:
try:
key = random.randint(1, len(dinners))
return dinners.pop(key)
except KeyError:
pass
return None
def usage():
print("Whats-for-dinner, determine whats for dinner from a json source.")
print("Usage: whats-for-dinner.py -s [source]")
# Main script
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], 's:d', ['source=', 'debug', 'help'])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
# Debug flag.
if opt in ('-d', '--debug'):
debug = True
if opt in ('-s', '--source'):
source = arg
if len(source) < 1:
print("Missing source.")
usage()
sys.exit(1)
dinners = get_dinners(source=source)
print("")
print("Dinners:")
for number, weekday in enumerate(weekdays):
print("> " + weekday + ": " + get_dinner_for_day(dinners=dinners, day=number))