-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroku_app.py
82 lines (73 loc) · 2.07 KB
/
roku_app.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
import os
from flask import Flask, request, jsonify, json
from roku import Roku
from slackclient import SlackClient
app = Flask(__name__)
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
def discover_roku():
# this gives an 'IndexError: list index out of range' error if no roku is found
# TODO: handle this better
roku = Roku.discover(timeout=10)
print(roku)
return roku
roku = discover_roku()
def chunk_list(list, n):
return [list[i:i + n] for i in range(0, len(list), n)]
@app.route('/remote', methods=['POST'])
def show_roku_remote():
print(roku.commands)
return jsonify(
text="Press button to execute",
response_type="ephemeral",
attachments=[
{
"actions": [
{
"name": "command",
"color": "#4a357d",
"text": command,
"type": "button",
"value": command
} for command in commands
],
"callback_id": "command"
} for commands in chunk_list(roku.commands, 5)
]
)
@app.route('/apps', methods=['POST'])
def show_roku_apps():
print(roku.apps)
return jsonify(
text="Press app button to launch",
response_type="ephemeral",
attachments=[
{
"actions": [
{
"name": "app",
"color": "#4a357d",
"text": app.name,
"type": "button",
"value": app.name
} for app in apps
],
"callback_id": "app"
} for apps in chunk_list(roku.apps, 5)
]
)
@app.route('/command', methods=['POST'])
def command_roku():
payload = json.loads(request.form['payload'])
if payload['callback_id'] == 'app':
app = payload['actions'][0]['value']
roku[app].launch()
print(app + " launched")
elif payload['callback_id'] == 'command':
button = payload['actions'][0]['value']
command = getattr(roku, button)
command()
print(button + " pressed")
return ''
if __name__ == '__main__':
app.run(debug=False)