Created
January 13, 2015 16:08
-
-
Save jcraig77/bac92a968abdc7b3b558 to your computer and use it in GitHub Desktop.
A python script that monitors idle time on a Windows machine. After the idle threshold is hit, windows media player is started and it plays a playlist. After the playlist is finished or when the operating system is no longer idle, the windows media player is killed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' A python script that monitors idle time on a Windows machine. | |
After the idle threshold is hit, windows media player is started | |
and it plays a playlist. After the playlist is finished or when the | |
operating system is no longer idle, the windows media player is killed. ''' | |
from ctypes import Structure, windll, c_uint, sizeof, byref | |
import subprocess | |
import time | |
# how long before the playlist should begin | |
IDLE_TIME_SECONDS = 300 | |
# how long is the playlist | |
PLAYLIST_LENGTH_SECONDS = 184 | |
class LASTINPUTINFO(Structure): | |
_fields_ = [ | |
('cbSize', c_uint), | |
('dwTime', c_uint), | |
] | |
def get_idle_duration(): | |
lastInputInfo = LASTINPUTINFO() | |
lastInputInfo.cbSize = sizeof(lastInputInfo) | |
windll.user32.GetLastInputInfo(byref(lastInputInfo)) | |
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime | |
return millis / 1000.0 | |
def play_video(): | |
video_subprocess = subprocess.Popen(["C:/Program Files (x86)/Windows Media Player/wmplayer.exe", "/play", "/fullscreen", "/Playlist", "davos"]) | |
def kill_video(): | |
subprocess.call('Taskkill.exe /IM wmplayer.exe') | |
while 1: | |
GetLastInputInfo = int(get_idle_duration()) | |
print GetLastInputInfo | |
# awake from idle; kill the player | |
if GetLastInputInfo == 0: | |
kill_video() | |
# idle time reached; play video | |
elif GetLastInputInfo % IDLE_TIME_SECONDS == 0: | |
play_video() | |
# playlist has played; kill the player | |
elif (GetLastInputInfo - PLAYLIST_LENGTH_SECONDS) % (IDLE_TIME_SECONDS) == 0: | |
kill_video() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment