utility-scripts/netflix-screensaver.py
2015-06-15 01:58:20 +02:00

77 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python
# Send a signal to DBus to inhibit the screensaver
# if Netflix is running (and playing a movie)
#
# Timothy Allen <tim@treehouse.org.za>
import dbus
import gobject
import atexit
from re import match, search
from time import sleep
from subprocess import Popen, PIPE
from dbus.mainloop.glib import DBusGMainLoop
debugging=0
loop = gobject.MainLoop()
dbus_loop = DBusGMainLoop()
bus = dbus.SessionBus(mainloop=dbus_loop)
screensaver = bus.get_object("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
def debug(text):
if debugging:
print text
# Inhibit
def inhibit():
debug("Inhibiting...")
return screensaver.Inhibit("netflix-desktop", "Playing a movie", dbus_interface="org.freedesktop.ScreenSaver")
# Uninhibit
def uninhibit(cookie):
if match("\d{2,}", str(cookie)):
debug("Uninhibiting...")
# Should return None
return screensaver.UnInhibit(cookie, dbus_interface="org.freedesktop.ScreenSaver")
else:
debug("No cookie, not uninhibiting")
return None
atexit.register(loop.quit)
if loop.is_running:
cookie=None
netflix_on=False
while True:
# Get process list
ps = Popen(['ps', 'ax'], shell=False, stdout=PIPE).communicate()[0]
# Search for the netflix process (and Silverlight to
# only inhibit if a movie is playing)
#if search("netflix-desktop", ps):
if search("netflix-desktop", ps) and search("Silverlight", ps):
netflix_on=True
else:
netflix_on=False
if netflix_on == True and cookie == None:
cookie = inhibit()
atexit.register(uninhibit, cookie)
debug(cookie)
elif netflix_on == False and cookie != None:
cookie = uninhibit(cookie)
try:
atexit.unregister(uninhibit)
except (SyntaxError, AttributeError):
debug("No unregister for atexit. We must be running on an earlier version of python. No problem.")
pass
debug(cookie)
sleep(30)
loop.run()
# vi: filetype=python:expandtab:shiftwidth=4:softtabstop=4:tw=0