moved from screen to tmux
[tvweb.git] / tv_web.py
blob7021e2e922807b33780e348e2637f5bad78b81e9
1 """
2 Interface for controlling tv
3 """
5 # Import CherryPy global namespace
6 import cherrypy
7 import time
8 from genshi.template import TemplateLoader
9 import tv_controller
10 import filechooser
12 class TvWeb:
14 # constructor
15 def __init__(self):
16 self.mpc = tv_controller.VideoController()
17 self.vol = tv_controller.VolumeController('PCM')
18 self.monitor = tv_controller.MonitorController()
19 self.filechooser = filechooser.FileChooser()
20 self.lastplayed = 'Unknown'
21 self.lastip = "Unknown"
23 # main page
24 def index(self):
25 if not self.hostallowed(): return "You are not alowed."
26 tmpl = loader.load('tvweb.html')
27 stream = tmpl.generate(
28 title="TV Station Web Interface!",
29 channels=self.mpc.getchannels(),
30 lastplayed=self.lastplayed,
31 curvolume=self.vol.getvolume()[0],
32 lastip=self.lastip,
33 curdir=self.filechooser.getCurrentPath(),
34 dirlisting=self.filechooser.getList(),
35 alloweddirs=self.filechooser.getAllowed())
36 return stream.render('html', doctype='html')
38 index.exposed = True
40 # changes channel
41 def changechannel(self, channel=None):
42 if not self.hostallowed(): return "You are not alowed."
43 if cherrypy.request.method == 'POST':
44 if channel :
45 self.lastplayed = channel
46 self.lastip = cherrypy.request.remote.ip
47 self.mpc.play_tv(channel)
48 time.sleep(2)
49 raise cherrypy.HTTPRedirect('/tvweb')
50 return "POST request expected, but "+cherrypy.request.method+" arrived!"
52 changechannel.exposed = True
54 # stops playing
55 def stopplaying(self):
56 if not self.hostallowed(): return "You are not alowed."
57 if cherrypy.request.method == 'POST':
58 self.lastplayed = 'Stopped'
59 self.lastip = cherrypy.request.remote.ip
60 self.mpc.kill()
61 raise cherrypy.HTTPRedirect('/tvweb')
62 return "POST request expected, but "+cherrypy.request.method+" arrived!"
64 stopplaying.exposed = True
66 # volume up
67 def volup(self):
68 if not self.hostallowed(): return "You are not alowed."
69 if cherrypy.request.method == 'POST':
70 self.vol.volup()
71 raise cherrypy.HTTPRedirect('/tvweb')
72 return "POST request expected, but "+cherrypy.request.method+" arrived!"
74 volup.exposed = True
76 # volume down
77 def voldown(self):
78 if not self.hostallowed(): return "You are not alowed."
79 if cherrypy.request.method == 'POST':
80 self.vol.voldown()
81 raise cherrypy.HTTPRedirect('/tvweb')
82 return "POST request expected, but "+cherrypy.request.method+" arrived!"
84 voldown.exposed = True
86 # monitor turn on
87 def monitoron(self):
88 if not self.hostallowed(): return "You are not alowed."
89 if cherrypy.request.method == 'POST':
90 self.monitor.turnon()
91 raise cherrypy.HTTPRedirect('/tvweb')
92 return "POST request expected, but "+cherrypy.request.method+" arrived!"
94 monitoron.exposed = True
96 # monitor turn off
97 def monitoroff(self):
98 if not self.hostallowed(): return "You are not alowed."
99 if cherrypy.request.method == 'POST':
100 self.monitor.turnoff()
101 raise cherrypy.HTTPRedirect('/tvweb')
102 return "POST request expected, but "+cherrypy.request.method+" arrived!"
104 monitoroff.exposed = True
106 # file action
107 def fcaction(self, item=None):
108 if not self.hostallowed(): return "You are not alowed."
109 if cherrypy.request.method == 'GET':
110 if os.path.isfile(item):
111 self.lastplayed = item
112 self.lastip = cherrypy.request.remote.ip
113 self.mpc.play(item)
114 time.sleep(2)
115 elif os.path.isdir(item):
116 self.filechooser.setPath(item)
117 else:
118 self.filechooser.goBack(item)
119 raise cherrypy.HTTPRedirect('/tvweb')
120 return "GET request expected, but "+cherrypy.request.method+" arrived!"
122 fcaction.exposed = True
124 def test(self):
125 return "test ok."
127 test.exposed = True
129 # default function
130 def default(self, *another):
131 raise cherrypy.HTTPRedirect('/tvweb')
133 default.exposed = True
135 # checks allowed hosts
136 def hostallowed(self):
137 allowed = [ '128.10.20.1', '128.10.20.5' , '10.10.60.14', '127.0.0.1', '192.168.100.104', ]
138 hostip = cherrypy.request.remote.ip
139 if hostip in allowed:
140 return True
141 return False
144 class Prefix:
145 tvweb = TvWeb()
147 import os.path
148 tutconf = os.path.join(os.path.dirname(__file__), 'etc/cherryd-tvweb.conf')
150 loader = TemplateLoader('templates', auto_reload=True)
152 if __name__ == '__main__':
153 # CherryPy always starts with app.root when trying to map request URIs
154 # to objects, so we need to mount a request handler root. A request
155 # to '/' will be mapped to HelloWorld().index().
156 cherrypy.quickstart(HelloWorld(), config=tutconf)
157 else:
158 # This branch is for the test suite; you can ignore it.
159 cherrypy.tree.mount(Prefix(), config=tutconf)