forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.py
executable file
·311 lines (245 loc) · 10.3 KB
/
viewer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from os import path, getenv, utime, listdir
import os
from platform import machine
from random import shuffle
from requests import get as req_get
from time import sleep
from json import load as json_load
from signal import signal, SIGUSR1, SIGUSR2
import logging
import sh
import urllib
import threading
from settings import settings
import html_templates
from lib.utils import url_fails, get_mimetype, get_video_duration
from lib.models import *
__author__ = "WireLoad Inc"
__copyright__ = "Copyright 2012-2016, WireLoad Inc"
__license__ = "Dual License: GPLv2 and Commercial License"
SPLASH_DELAY = 60 # secs
EMPTY_PL_DELAY = 5 # secs
BLACK_PAGE = '/tmp/screenly_html/black_page.html'
WATCHDOG_PATH = '/tmp/screenly.watchdog'
SCREENLY_HTML = '/tmp/screenly_html/'
LOAD_SCREEN = '/screenly/loading.jpg' # relative to $HOME
UZBLRC = '/.config/uzbl/config-screenly' # relative to $HOME
INTRO = '/screenly/intro-template.html'
DEFAULT_DURATION = 30
current_browser_url = None
browser = None
home = None
def sigusr1(signum, frame):
"""
The signal interrupts sleep() calls, so the currently playing web or image asset is skipped.
omxplayer is killed to skip any currently playing video assets.
"""
logging.info('USR1 received, skipping.')
browser_clear()
kill_player()
def sigusr2(signum, frame):
"""Reload settings"""
logging.info("USR2 received, reloading settings.")
load_settings()
def load_browser(url):
global browser, current_browser_url, home
logging.info('Loading browser...')
if browser:
logging.info('killing previous uzbl %s', browser.pid)
browser.process.kill()
if url is not None:
current_browser_url = url
# --config=- read commands (and config) from stdin
# --print-events print events to stdout
browser = sh.Command('uzbl-browser')(print_events=True, config='-', uri=current_browser_url, _bg=True)
logging.info('Browser loading %s. Running as PID %s.', current_browser_url, browser.pid)
uzbl_rc = 'set ssl_verify = 0'
with open(home + UZBLRC) as f: # load uzbl.rc
uzbl_rc = f.read() + uzbl_rc
browser_send(uzbl_rc)
def browser_send(command, cb=lambda _: True):
if not (browser is None) and browser.process.alive:
while not browser.process._pipe_queue.empty(): # flush stdout
browser.next()
browser.process.stdin.put(command + '\n')
while True: # loop until cb returns True
if cb(browser.next()):
break
else:
logging.info('browser found dead, restarting')
load_browser()
def browser_clear():
"""Load a black page. Default cb waits for the page to load."""
browser_send('uri ' + 'file://' + BLACK_PAGE, cb=lambda buf: 'LOAD_FINISH' in buf and BLACK_PAGE in buf)
def kill_player():
sh.killall('omxplayer-loop', _ok_code=[1])
def view_url(url, cb=lambda _: True, force=False):
global current_browser_url
logging.info("view_url: %s" % url)
if url == current_browser_url and not force:
logging.info('Already showing %s, not doing anything.', current_browser_url)
else:
current_browser_url = url
browser_send('uri ' + current_browser_url, cb=cb)
def view_image(url, cb=lambda _: True, force=False):
global current_browser_url
logging.info("view_image: %s" % url)
url = urllib.quote(url)
if url == current_browser_url and not force:
logging.info('Already showing %s, not doing anything.', current_browser_url)
else:
current_browser_url = url
browser_send('js window.setimg("{0}")'.format(url), cb=lambda b: 'COMMAND_EXECUTED' in b and 'setimg' in b)
def view_video(uri):
logging.info('view_video (%s)', uri)
arch = machine()
if arch in ('armv6l', 'armv7l'):
player_args = ['omxplayer-loop', uri]
player_kwargs = {'o': settings['audio_output'], '_bg': True, '_ok_code': [0, 124]}
player_kwargs['_ok_code'] = [0, 124]
else:
player_args = ['mplayer', uri, '-nosound']
player_kwargs = {'_bg': True}
run = sh.Command(player_args[0])(*player_args[1:], **player_kwargs)
browser_clear()
while run.process.alive:
sleep(1)
if run.exit_code == 124:
logging.error('omxplayer timed out')
def get_setting(key, default, cast=id):
try:
return cast(settings[key])
except:
return default
class VideoThread(threading.Thread):
def __init__(self, file):
super(VideoThread, self).__init__()
self.file = file
def run(self):
view_video(self.file)
class DirectoryThread(threading.Thread):
def __init__(self, entry):
super(DirectoryThread, self).__init__()
self.entry = entry
self.loopTime = 1
self.__stop = threading.Event()
def stop(self):
self.__stop.set()
self.join()
def run(self):
directory = self.entry.directory
files = [path.join(directory, f) for f in os.listdir(directory) if path.isfile(path.join(directory, f))]
num_files = len(files)
if num_files == 0:
load_browser(url='http://{0}:{1}/splash_page'.format(settings.get_listen_ip(), settings.get_listen_port()) if settings['show_splash'] else 'file://' + BLACK_PAGE)
while not self.__stop.isSet():
sleep(self.loopTime)
else:
num = 0
currentEntryDuration = 0
mime = None
file = None
isNew = True
while not self.__stop.isSet():
logging.info("worker still waiting %d seconds for %s" % (currentEntryDuration, file))
if currentEntryDuration <= 0:
kill_player()
file = files[num]
mime = get_mimetype(file)
num = (num+1) % num_files
if mime is not None:
if 'image' in mime:
currentEntryDuration = get_setting('image_duration', DEFAULT_DURATION, cast=int)
view_image(file, force=isNew)
isNew = False
elif 'text' in mime:
currentEntryDuration = get_setting('webpage_duration', DEFAULT_DURATION, cast=int)
with open(file, 'r') as urlfile:
try:
line = urlfile.readlines()[0].replace('\n', '')
prefix = line[:5]
if prefix == "live:":
currentEntryDuration = 24*60*60 # large number of seconds
VideoThread(line[5:]).start()
else:
view_url(line, force=isNew)
except Exception as e:
currentEntryDuration = 0
logging.error('cannot show text file: %s, error: %s' % (file, e))
isNew = False
elif 'video' in mime:
currentEntryDuration = get_video_duration(file)
VideoThread(file).start()
else:
# cannot show this entry, so skip
logging.info('mimetype (%s) of file (%s) not supported.' % (mime, file))
mime = None
currentEntryDuration = 0
else:
logging.info('cannot show suspect file: %s' % file)
sleep(self.loopTime)
currentEntryDuration -= self.loopTime
class Viewer(object):
def __init__(self):
global home
home = getenv('HOME', '/home/pi')
self.currentId = None
self.currentDirectory = None
self.worker = None
signal(SIGUSR1, sigusr1)
signal(SIGUSR2, sigusr2)
settings.load()
logging.getLogger().setLevel(logging.INFO)
try:
sh.mkdir(SCREENLY_HTML)
except:
pass
html_templates.black_page(BLACK_PAGE)
load_browser(url='http://{0}:{1}/splash_page'.format(settings.get_listen_ip(), settings.get_listen_port()))
sleep(30)
def play(self, entry):
self.stop()
self.currentId = entry.id
self.currentDirectory = entry.directory
self.worker = DirectoryThread(entry)
self.worker.setDaemon(True)
self.worker.start()
def stop(self):
if self.worker is not None:
self.worker.stop()
browser_clear()
kill_player()
self.currentId = None
self.currentDirectory = None
self.worker = None
def run(self):
while True:
schedules = list(Schedule.select(Schedule.q.active==True, orderBy='id'))
if len(schedules) > 0: # there is an active schedule
schedule = schedules[0]
dt = datetime.time(datetime.now())
time = dt.hour * 60 * 60 + dt.minute * 60 + dt.second
for entry in schedule.entries:
logging.info('checking activity of: %s, time: %d' % (entry, time))
if entry.start <= time and entry.end >= time and (self.currentId is None or self.currentId != entry.id or self.currentDirectory != entry.directory):
logging.info('show directory: %s' % entry)
self.play(entry)
break
else:
# if an entry is currently running there is nothing
# to do
pass
else: # there is no active schedule
self.stop()
# sleep 10 seconds then run again
sleep(10)
if __name__ == "__main__":
try:
viewer = Viewer()
viewer.run()
except:
logging.exception("Viewer crashed.")
raise