-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
113 lines (95 loc) · 2.92 KB
/
code.py
File metadata and controls
113 lines (95 loc) · 2.92 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import subprocess
import sys
import web
urls = (
'/images/current.png' , 'currentPNG',
'/.*' , 'index',
)
render = web.template.render('templates/')
commands = {
'prev' : 'DOWN',
'next' : 'UP',
'less' : 'LEFT',
'more' : 'RIGHT',
'mute' : 'TOGGLE_MUTE',
'fullscreen': 'TOGGLE_FULLSCREEN',
'quit' : 'QUIT',
'0' : 'CHANNEL_0',
'1' : 'CHANNEL_1',
'2' : 'CHANNEL_2',
'3' : 'CHANNEL_3',
'4' : 'CHANNEL_4',
'5' : 'CHANNEL_5',
'6' : 'CHANNEL_6',
'7' : 'CHANNEL_7',
'8' : 'CHANNEL_8',
'9' : 'CHANNEL_9',
'screenshot': 'SCREENSHOT',
}
path = "/home/janus/Projects/python/webapp/images/current.png"
def setup_xmltv_environ():
os.environ.pop('LC_ALL', None)
os.environ['LC_MESSAGES'] = 'C'
def run_xmltv_command(cmd, arg=None):
"""
Returns without value if command succesfully sent, and returns error string otherwise.
"""
cmdline = []
for add in cmd.split(" "):
cmdline.append(add)
cmdline.insert(0, 'tvtime-command')
if arg:
cmdline.append(arg)
try:
p = subprocess.Popen(cmdline, stdin=None, stdout=None, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
# Everything is ok
if p.returncode == 0:
return
# Error occured, reason is in last line of stderr
return stderr.rstrip('\n').split('\n')[-1]
except OSError, e:
# Error occured, return the error to caller
return str(e)
def check_xmltv():
return run_xmltv_command('SCREENSHOT', path)
def numToChannelId(num):
output = ""
for i in range(0, len(num)):
if i != " ":
try:
output += "%s " % commands[num[i]]
except:
output += ""
if len(num) > 1:
return "%sENTER" % output
else:
return "%s" % output
class index:
def GET(self):
name = check_xmltv()
return render.index(name)
def POST(self):
iPost = web.input()
action = iPost.keys()[0]
if action in commands:
if action == "screenshot":
name = run_xmltv_command(commands[action], path)
else:
name = run_xmltv_command(commands[action])
else:
name = run_xmltv_command(numToChannelId(action))
return render.index(name)
class currentPNG:
def GET(self):
path = "/home/janus/Projects/python/webapp/images/current.png"
if not os.path.exists(path):
raise web.NotFound()
with open(path) as f:
content = f.read()
web.header("Content-Type", "image/png")
return content
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()