-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_orders.py
executable file
·56 lines (48 loc) · 2.03 KB
/
get_orders.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
#!/usr/bin/env python
from __future__ import print_function
import json
import sys
try:
from urllib.request import urlopen # Python 3
except ImportError:
from urllib2 import urlopen # Python 2
# Place orders at http://collabedit.com/em3ev
URL = 'http://collabedit.com/download?id=em3ev'
def parse_data(data):
orders = {}
total = 0
for category, items in data.items():
# category will be "pizza", "pasta", etc.
for item_type, item_name in items.items():
# item_type will be pasta type or pizza name
if 'choosers' in item_name:
# item_type is pizza name
count = len(item_name['choosers'])
if count:
choosers = []
for chooser in item_name['choosers']:
extras = item_name.get('extras', dict()).get(chooser)
if extras:
choosers.append('{}-{}'.format(chooser, tuple(extras)))
else:
choosers.append(chooser)
order = ' > '.join([category, item_type])
orders[order] = (count, choosers)
total += count
else:
# item_type is pasta type, item_name is pasta name
for name, choosers in item_name.items():
count = len(choosers['choosers'])
if count:
order = ' > '.join([category, item_type, name])
orders[order] = (count, choosers['choosers'])
total += count
return orders, total
if __name__ == '__main__':
#data = json.loads(sys.stdin.read()) # $ cat menu.json | ./get_orders.py
resp = urlopen(URL)
data = json.loads(resp.read().decode('utf-8'))
orders, total = parse_data(data['orders']['data'])
for item, (count, choosers) in orders.items():
print(item + ': ', count, '(' + ', '.join(choosers) + ')')
print('Total:', total)