forked from denics/yatse-kodi-startup
-
Notifications
You must be signed in to change notification settings - Fork 5
/
wolkodi.py
60 lines (49 loc) · 2 KB
/
wolkodi.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
#!/usr/bin/env python
import logging
import platform
import socket
import subprocess
from logging.handlers import TimedRotatingFileHandler
LOG_FILENAME = '/var/log/wolkodi.log'
UDP_PORT = 9
def config_log(log_filename):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(log_filename, when="d", interval=30, backupCount=5)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", UDP_PORT)) # could also use "0.0.0.0"
while True:
data, addr = sock.recvfrom(512) # buffer size is 512 bytes
logger.info("UPD package from IP '%s'", addr[0])
sock.recv(1024*128)
distro, version,_ = platform.linux_distribution()
if distro.lower() == 'arch':
p = subprocess.Popen(["systemctl", "status", "kodi"], stdout=subprocess.PIPE)
out, err = p.communicate()
if "Active: active (running)" in str(out):
logger.info("kodi is already running. Nothing to do")
else:
logger.info("Starting kodi (Arch)...")
subprocess.Popen(['systemctl','start','kodi'])
logger.info("kodi has been started :-)")
elif distro.lower() == 'debian':
p = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE)
out, err = p.communicate()
if "kodi.bin" in out:
logger.info("kodi is already running. Nothing to do")
else:
logger.info("Starting kodi (Debian)...")
subprocess.Popen(["/usr/bin/kodi"], stdout=subprocess.PIPE)
logger.info("kodi has been started :-)")
if __name__ == "__main__":
try:
logger = config_log(LOG_FILENAME)
main()
except Exception as e:
logger.error(e)