Skip to content

Commit a87bb87

Browse files
author
George Hafiz
committed
first commit
0 parents  commit a87bb87

File tree

16 files changed

+293
-0
lines changed

16 files changed

+293
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/cli-papajohns.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# cli-papajohns-uk
2+
3+
This WIP software leverages the Papajohn's UK iPhone API to order pizza.
4+
5+
At the moment, it only accepts your postcode, offers you local restaurants with delivery in the proximity and then presents
6+
you with a list of the deals they have on offer.

main.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/env python3
2+
3+
from __future__ import print_function, unicode_literals
4+
5+
import asyncio
6+
7+
import regex
8+
from papajohns.api import Api
9+
10+
from pprint import pprint
11+
from PyInquirer import style_from_dict, Token, prompt
12+
from PyInquirer import Validator, ValidationError
13+
14+
style = style_from_dict({
15+
Token.QuestionMark: '#E91E63 bold',
16+
Token.Selected: '#673AB7 bold',
17+
Token.Instruction: '', # default
18+
Token.Answer: '#2196f3 bold',
19+
Token.Question: '',
20+
})
21+
22+
23+
class PostCodeValidator(Validator):
24+
def validate(self, document):
25+
ok = True
26+
if not ok:
27+
raise ValidationError(message='Invalid postcode',
28+
cursor_position=len(document.text))
29+
30+
31+
class PhoneNumberValidator(Validator):
32+
def validate(self, document):
33+
ok = regex.match(
34+
'^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$',
35+
document.text)
36+
if not ok:
37+
raise ValidationError(
38+
message='Please enter a valid phone number',
39+
cursor_position=len(document.text)) # Move cursor to end
40+
41+
42+
class NumberValidator(Validator):
43+
def validate(self, document):
44+
try:
45+
int(document.text)
46+
except ValueError:
47+
raise ValidationError(
48+
message='Please enter a number',
49+
cursor_position=len(document.text)) # Move cursor to end
50+
51+
52+
async def main():
53+
print('Hi, welcome to Python Pizza')
54+
55+
api = Api()
56+
57+
questions = [
58+
{
59+
'type': 'confirm',
60+
'name': 'forDelivery',
61+
'message': 'Is this for delivery?',
62+
'default': True
63+
},
64+
{
65+
'type': 'input',
66+
'name': 'postcode',
67+
'message': 'What\'s your post code?',
68+
'default': 'po48ap',
69+
'filter': lambda val: val.upper(),
70+
'validate': PostCodeValidator
71+
},
72+
# {
73+
# 'type': 'list',
74+
# 'name': 'size',
75+
# 'message': 'What size do you need?',
76+
# 'choices': ['Large', 'Medium', 'Small'],
77+
# 'filter': lambda val: val.lower()
78+
# },
79+
# {
80+
# 'type': 'input',
81+
# 'name': 'quantity',
82+
# 'message': 'How many do you need?',
83+
# 'validate': NumberValidator,
84+
# 'filter': lambda val: int(val)
85+
# },
86+
# {
87+
# 'type': 'expand',
88+
# 'name': 'toppings',
89+
# 'message': 'What about the toppings?',
90+
# 'choices': [
91+
# {
92+
# 'key': 'p',
93+
# 'name': 'Pepperoni and cheese',
94+
# 'value': 'PepperoniCheese'
95+
# },
96+
# {
97+
# 'key': 'a',
98+
# 'name': 'All dressed',
99+
# 'value': 'alldressed'
100+
# },
101+
# {
102+
# 'key': 'w',
103+
# 'name': 'Hawaiian',
104+
# 'value': 'hawaiian'
105+
# }
106+
# ]
107+
# },
108+
# {
109+
# 'type': 'rawlist',
110+
# 'name': 'beverage',
111+
# 'message': 'You also get a free 2L beverage',
112+
# 'choices': ['Pepsi', '7up', 'Coke']
113+
# },
114+
# {
115+
# 'type': 'input',
116+
# 'name': 'comments',
117+
# 'message': 'Any comments on your purchase experience?',
118+
# 'default': 'Nope, all good!'
119+
# },
120+
# {
121+
# 'type': 'list',
122+
# 'name': 'prize',
123+
# 'message': 'For leaving a comment, you get a freebie',
124+
# 'choices': ['cake', 'fries'],
125+
# 'when': lambda answers: answers['comments'] != 'Nope, all good!'
126+
# }
127+
]
128+
129+
delivery = prompt(questions, style=style)
130+
address_choices = await api.get_addresses(delivery['postcode'])
131+
address = prompt(questions=[{
132+
'type': 'list',
133+
'name': 'address1',
134+
'message': 'What is your address?',
135+
'choices': [x['address1'] for x in address_choices]
136+
}])
137+
full_address = [x for x in address_choices if x['address1'] == address['address1']][0]
138+
139+
delivery_stores = await api.get_delivery_stores(full_address)
140+
store_address = prompt(questions=[{
141+
'type': 'list',
142+
'name': 'address1',
143+
'message': 'Which delivery store?',
144+
'choices': [x['storeLocation']['address1'] for x in delivery_stores]
145+
}])
146+
store_id = [x['storeId'] for x in delivery_stores if x['storeLocation']['address1'] == store_address['address1']][0]
147+
148+
store_deals = await api.get_store_deals(store_id)
149+
store_deal = prompt(questions=[{
150+
'type': 'list',
151+
'name': 'title',
152+
'message': 'Which deal?',
153+
'choices': [x['title'] for x in store_deals]
154+
}])
155+
deal_id = [x['dealId'] for x in store_deals if x['title'] == store_deal['title']][0]
156+
157+
158+
159+
del api
160+
161+
162+
if __name__ == '__main__':
163+
asyncio.run(main())

0 commit comments

Comments
 (0)