-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
85 lines (56 loc) · 2.14 KB
/
server.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
from functools import wraps
import logging
import sys
from flask import Flask
from flask_ask import Ask, statement
from pycycles import Client, ServiceArea
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
app = Flask(__name__)
ask = Ask(app, '/')
# set these.
USERNAME = ''
PASSWORD = ''
def get_cycles_statement(inputs):
inputs['be_form'] = 'is' if len(inputs['cycles']) == 1 else 'are'
inputs['cycles'] = 'no' if len(input['cycles']) == 0 else inputs['cycles']
return statement('There {be_form} {count} cycles at {portname}'.format(**inputs))
def rent_cycle_statement(inputs):
return statement('Cycle rented from {portname}. Your PIN is {pin}'.format(**inputs))
def port_not_found_statement(inputs):
return statement('Could not find cycleport: {portname}'.format(**inputs))
def match_port(target_port, ports):
match = None
for port in ports:
if target_port.lower() in port['name_en'].lower():
match = port
break
return match
def get_client():
client = Client(USERNAME, PASSWORD)
client.login()
return client
@ask.intent('GetCyclesForCycleport')
def get_cycles_for_port(client, portname):
client = get_client()
cycleports = client.cycleports(ServiceArea.CHUO)
cycleport = match_port(portname, cycleports)
if cycleport == None:
return port_not_found_statement({'portname': portname})
cycles = client.cycles(cycleport)
return get_cycles_statement(
{'cycles': cycles, 'portname': cycleport['name_en'], 'count': len(cycles)})
@ask.intent('RentCycle')
def rent_cycle_from_port(client, portname):
client = get_client()
cycleports = client.cycleports(ServiceArea.CHUO)
cycleport = match_port(portname, cycleports)
if cycleport == None:
return port_not_found_statement({'portname': portname})
cycles = client.cycles(cycleport)
if len(cycles) == 0:
return get_cycles_statement({'cycles': cycles, 'portname': portname})
rental = client.rent(cycles[0])
return rent_cycle_statement(
{'portname': rental['cycleport']['name_en'], 'pin': rental['pin']})
if __name__ == '__main__':
app.run()