-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbmc-nice.py
executable file
·76 lines (59 loc) · 2.41 KB
/
xbmc-nice.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
#!/usr/bin/python
import subprocess
import time
import os.path
import urllib2, urllib
import json
services = ['couchpotato', 'sickbeard', 'transmission-daemon']
def is_running_ps(process_name):
proc1 = subprocess.Popen(['ps', 'aux'],stdout=subprocess.PIPE)
proc2 = subprocess.Popen(['grep', "%s" % process_name], stdin=proc1.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
proc3 = subprocess.Popen(['grep', '-v', 'grep'], stdin=proc2.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
proc2.stdout.close() # Allow proc2 to receive a SIGPIPE if proc3 exits.
out,err=proc3.communicate()
return out
return bool(out.strip())
def is_running_service(service_name):
proc = subprocess.Popen(['service', "%s" % service_name ,'status'],stdout=subprocess.PIPE)
out,err=proc.communicate()
return (proc.returncode == 0)
def is_xbmc_playing():
headers = {'Content-Type': 'application/json'}
request = urllib2.Request('http://localhost:8080/jsonrpc', '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}', headers)
try:
handler = urllib2.urlopen(request)
j = json.loads(handler.read())
except:
return 0
return len(j["result"]) > 0
def stop_service(service_name):
print "Stopping: %s" % service_name
p = subprocess.Popen(['service', service_name, 'stop'],stdout=subprocess.PIPE)
p.wait()
if p.returncode == 0:
p2 = subprocess.Popen(['touch', "/tmp/.mc_%s" % service_name],stdout=subprocess.PIPE)
print "Errorcode: %d" % p.returncode
return p.returncode
def start_service(service_name):
print "Starting: %s" % service_name
p = subprocess.Popen(['service', service_name, 'start'],stdout=subprocess.PIPE)
p.wait()
if p.returncode == 0:
p2 = subprocess.Popen(['rm', '-f', "/tmp/.mc_%s" % service_name],stdout=subprocess.PIPE)
print "Errorcode: %d" % p.returncode
return p.returncode
def was_running(service_name):
return os.path.isfile("/tmp/.mc_%s" % service_name)
def run():
while True:
for service in services:
if is_xbmc_playing():
if is_running_service(service):
stop_service(service)
else:
if was_running(service):
start_service(service)
time.sleep(5)
if __name__ == "__main__":
run()