-
Notifications
You must be signed in to change notification settings - Fork 6
/
kodisendto
executable file
·76 lines (69 loc) · 2.75 KB
/
kodisendto
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/env python2
import argparse
import logging
import subprocess
import koditools
from koditools import utils
import os
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack(b'256s', ifname[:15].encode())
)[20:24])
def ispidalive(pidfile):
if os.path.exists(pidfile):
pid = open(pidfile).read().strip()
if pid.isdigit():
pid = int(pid)
try:
os.kill(pid, 0)
return True
except OSError:
return False
return False
def launchnginx(interface, filepath):
nginxconfpath = '/tmp/koditoolsnginx.conf'
nginxpidfile = '/tmp/koditoolsnginx.pid'
templatepath = os.path.join(os.path.dirname(koditools.__file__),
'templates', 'nginx.conf')
with open(templatepath) as fd:
nginxconf = fd.read()
filename = os.path.basename(filepath)
dirname = os.path.dirname(filepath)
nginxconf = nginxconf.format(filename=filename, dirpath=dirname)
with open(nginxconfpath, 'w') as fd:
fd.write(nginxconf)
if ispidalive(nginxpidfile):
subprocess.Popen(['nginx', '-c', nginxconfpath, '-s', 'reload'])
else:
subprocess.Popen(['nginx', '-c', nginxconfpath])
ip = get_ip_address(interface)
return "http://%s:55656/stream/%s" % (ip, filename)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Send media to Kodi. Server host and port can be defined in [server] group of $XDG_CONFIG_HOME/koditools/remote.conf')
parser.add_argument('--host', dest='host', action='store', default=None)
parser.add_argument('-p', '--port', dest='port', type=int, default=None)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-f', '--file', dest='file', default=None)
group.add_argument('filename', nargs='?')
parser.add_argument('-i', '--iface', dest='interface', default=None, required=False)
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False)
args = parser.parse_args()
logging.root.name = 'kodiremote'
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
cfg = utils.getConfigFile()
host, port = utils.getHostPort(cfg, args.host, args.port)
client = utils.getJSONRC(host, port)
interface = args.interface or cfg.get('client', 'interface')
filename = args.file or args.filename
if not os.path.exists(filename):
url = filename
else:
url = launchnginx(interface, os.path.realpath(filename))
print(client.command('Player.Open', item={'file': url}))