-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlc.py
executable file
·315 lines (288 loc) · 9.74 KB
/
vlc.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
import cgi
import cgitb
import logging
import telnetlib
import os
import subprocess
import socket
class Logger:
LOG_FILENAME = '/FIXME/logs/python.log'
BABIES_VIDEOS_DIR = '/mount/babies_videos_dir'
PELIS = '/mount/pelis'
scriptname=None
logger=None
def debug(self,message):
self.logger.debug(message)
def __init__(self,scriptname):
logging.basicConfig(filename=self.LOG_FILENAME,level=logging.DEBUG)
self.scriptname=scriptname
self.logger=logging.getLogger(scriptname)
class Vlc:
""" Permite comunicar con VLC"""
connected=False
tn=telnetlib.Telnet()
logger=None
def __init__(self,logger):
self.logger=logger
def connect(self):
l=self.logger
if not self.connected:
try:
self.tn.open('127.0.0.1',6885)
l.debug(self.tn.read_until("> ",2))
self.connected=True
l.debug('connect completado')
except socket.error,e:
print 'Cannot connect to vlc - '+str(e)+'<br>\n'
else:
l.debug('connect already connected')
def sendcommand(self,command):
result=self.sendcommand_raw(command).replace('\n','<br>\n')
return result
def sendcommand_raw(self,command):
l=self.logger
if self.connected:
self.tn.write(command+"\n")
resultado=self.tn.read_until("> ",2)[:-2]
else:
resultado='Error: Not connected'
l.debug(resultado)
return resultado
def disconnect(self):
l=self.logger
if self.connected:
self.tn.write("quit\n")
l.debug(self.tn.read_until("Bye-bye!",2))
self.tn.close()
self.connected=False
l.debug('disconnect completed')
else:
l.debug('disconnect not connected')
class Main:
vlc=None
extensions=set (['avi','divx','flv','mp4','mp3','mkv','ogg','wav'])
logger=None
def getPrintableTime(self,secs):
l=self.logger
l.debug('secs = '+str(secs))
if isinstance(secs,int):
l.debug('secs is int')
l.debug(str(secs / 60))
result='{0:0>2}:{1:0>2}'.format((secs / 60),(secs % 60))
else:
l.debug('secs is not int')
result='00:00'
return result
def add_action(self,command,path=self.PELIS):
l=self.logger
l.debug('add_action path='+path)
dirtoshow=os.listdir(path)
indexuppath=path.rfind('/')
if indexuppath== -1: uppath=None
else: uppath=path[:indexuppath]
print '<p>\n'
if uppath is not None: print '<a href="vlc.py?action=add&path='+uppath+'">up</a><br>\n'
for subdir in dirtoshow:
if os.path.isdir(os.path.join(path,subdir)):
print '<a href="vlc.py?action=add&path='+path+'/'+subdir+'">'+subdir+'</a><br>\n'
for fil in dirtoshow:
indexpoint=fil.rfind('.')
if indexpoint != -1:
ext=fil[indexpoint+1:].lower()
if ext in self.extensions:
print '<a href="vlc.py?action=add&path='+path+'&command=file://'+path+'/'+fil+'">'+fil+'</a><br>\n'
print '</p>\n'
if command is not None:
self.vlc.sendcommand("add "+command)
print "add "+command+" - done<br>\n"
def control_action(self,command):
l=self.logger
v=self.vlc
if command is None: l.debug('inside control_action no command')
else: l.debug('inside control_action '+command)
get_time=v.sendcommand_raw("get_time")
if get_time.find('Error') != -1:
l.debug('Error found')
get_time = -1
else:
l.debug('Error not found')
get_time = int(get_time)
get_length=v.sendcommand_raw("get_length")
if get_length.find('Error') != -1: get_length = -1
else: get_length=int(get_length)
print '<p>\n\
<a href="vlc.py?action=control&command=play">play</a><br>\n\
<a href="vlc.py?action=control&command=pause">pause</a><br>\n\
<a href="vlc.py?action=control&command=stop">stop</a><br>\n\
<a href="vlc.py?action=control&command=seek0">go0</a><br>\n\
<a href="vlc.py?action=control&command=seek-1800">-30min</a><br>\n\
<a href="vlc.py?action=control&command=seek-300">-5min</a><br>\n\
<a href="vlc.py?action=control&command=seek-60">-1min</a><br>\n\
<a href="vlc.py?action=control&command=seek-10">-10sec</a><br>\n\
<a href="vlc.py?action=control&command=seek+10">+10sec</a><br>\n\
<a href="vlc.py?action=control&command=seek+60">+1min</a><br>\n\
<a href="vlc.py?action=control&command=seek+300">+5min</a><br>\n\
<a href="vlc.py?action=control&command=seek+1800">+30min</a><br>\n\
</p>\n'
print 'status='+self.vlc.sendcommand("status")+'<br>\n'
if get_time != -1: print 'Current '+self.getPrintableTime(get_time)+'<br>\n'
if get_length != -1: print 'Total '+self.getPrintableTime(get_length)+'<br>\n'
if command is not None:
if command == "play": v.sendcommand("play")
if command == "pause": v.sendcommand("pause")
if command == "stop": v.sendcommand("stop")
if command.startswith('seek') and get_time != -1:
if command[4] == '0':
value=0
else:
value=int(command[5:])
l.debug('value '+str(value))
# urls or python converts '+' in ' ' (very weird)
if command[4] == ' ':
value=get_time+value
l.debug('signal +')
elif command[4] == '-':
value=get_time-value
l.debug('signal +')
else:
value=-1
l.debug('Invalid signal='+command[4])
print 'Invalid signal='+command[4]+'<br>\n'
if value >= 0:
l.debug ('seek '+str(value))
v.sendcommand('seek '+str(value))
else:
l.debug('Invalid seek value '+str(value))
print 'Invalid seek value '+str(value)+'<br>\n'
print command+" - done<br>\n"
def babyeinstein_action(self,command):
l=self.logger
v=self.vlc
v.sendcommand("clear")
v.sendcommand("random")
v.sendcommand("add "+self.BABIES_VIDEOS_DIR)
#TODO: write pause
v.sendcommand("f")
def fullscreen_action(self,command):
self.vlc.sendcommand("f")
def playlist_action(self,command):
l=self.logger
print '<p>\n\
<a href="vlc.py?action=add">add</a><br>\n\
<a href="vlc.py?action=playlist&command=clear">clear</a><br>\n\
</p>\n'
if command == "clear": self.vlc.sendcommand("clear")
if command is not None: print command+" - done<br>\n"
print self.vlc.sendcommand("playlist")+'<br>\n'
def process_action(self,command):
l=self.logger
print '<p>\n\
<a href="vlc.py?action=process&command=restart">restart</a><br>\n\
<a href="vlc.py?action=process&command=shutdown">shutdown</a><br>\n\
</p>\n'
if command == "restart":
output=subprocess.Popen(["sudo", "/opt/scripts/controlservices.sh", "vlcrestart"],stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
print output[0]
print output[1]
if command == "shutdown": self.vlc.sendcommand("shutdown")
if command is not None: print command+" - done<br>\n"
def volume_action(self,command):
l=self.logger
v=self.vlc
print '<p>\n\
<a href="vlc.py?action=volume&command=down">down</a><br>\n\
<a href="vlc.py?action=volume&command=downabit">downabit</a><br>\n\
<a href="vlc.py?action=volume&command=full">full</a><br>\n\
<a href="vlc.py?action=volume&command=half">half</a><br>\n\
<a href="vlc.py?action=volume&command=mute">mute</a><br>\n\
<a href="vlc.py?action=volume&command=up">up</a><br>\n\
<a href="vlc.py?action=volume&command=upabit">upabit</a><br>\n\
</p>\n'
if command == "down": v.sendcommand("voldown")
if command == "downabit": v.sendcommand("volume -1")
if command == "full": v.sendcommand("volume 100")
if command == "half": v.sendcommand("volume 50")
if command == "mute": v.sendcommand("volume 0")
if command == "up": v.sendcommand("volup")
if command == "upabit": v.sendcommand("volume +1")
print v.sendcommand("volume")+'<br>\n'
if command is not None:
print command+" - done<br>\n"
def vratio_action(self,command):
l=self.logger
v=self.vlc
print '<p>\n\
<a href="vlc.py?action=vratio&command=1610">16:10</a><br>\n\
<a href="vlc.py?action=vratio&command=43">4:3</a><br>\n\
</p>\n'
if command == "1610": v.sendcommand("vratio 16:10")
if command == "43": v.sendcommand("vratio 4:3")
print v.sendcommand("vratio")+'<br>\n'
if command is not None: print command+" - done<br>\n"
def __init__(self):
action=None
command=None
path=None
print 'Content-type: text/html\n'
print '<!DOCTYPE HTML>\n\
<html>\n\
<head>\n\
<link rel="stylesheet" type="text/css" href="/css/vlc.css" />\n\
<meta name="HandheldFriendly" content="true" />\n\
<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no" />\n\
<title>vlc</title>\n\
</head>\n\
<body>\n\
<p><div class="header">VLC</div></p>\n\
<p>\n\
<a href="vlc.py?action=babyeinstein">babyeinstein</a><br>\n\
<a href="vlc.py?action=control">control</a><br>\n\
<a href="vlc.py?action=fullscreen">fullscreen</a><br>\n\
<a href="vlc.py?action=playlist">playlist</a><br>\n\
<a href="vlc.py?action=process">process</a><br>\n\
<a href="vlc.py?action=volume">volume</a><br>\n\
<a href="vlc.py?action=vratio">vratio</a><br>\n\
</p>\n\
<p><a href="..">up</a></p>\n\
<div class="result">\n'
try:
self.logger=Logger(os.environ['SCRIPT_NAME'])
l=self.logger
l.debug('main - gettingFields')
form = cgi.FieldStorage() # parse query
if form.has_key("action") and form["action"] != "":
self.vlc=Vlc(self.logger)
v=self.vlc
v.connect()
action=form["action"].value
l.debug('Executing action '+action)
if form.has_key("command") and form["command"] != "":
command=form["command"].value
l.debug('command '+command)
if form.has_key("path") and form["path"] != "":
path=form["path"].value
l.debug('path='+path)
if action == "add":
if path is None: self.add_action(command)
else: self.add_action(command,path)
else:
try:
f=getattr(self,action+'_action')
f(command)
#except AttributeError:
# print 'Invalid action<br>\n'
finally:
pass
print action+' - done<br>\n'
else:
print 'Select an action'
except:
print 'Oops. An error ocurred.<br>\n'
cgi.print_exception() # Print traceback,safely
finally:
if self.vlc is not None:
self.vlc.disconnect()
print '</div></body>\n</html>'
cgitb.enable()
Main()