forked from klattimer/LGWebOSRemote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlgtv.py
95 lines (84 loc) · 2.68 KB
/
lgtv.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from __future__ import print_function
import sys
import os
import json
from inspect import getargspec
from LGTV import LGTVScan, LGTVClient, getCommands
def usage(error=None):
if error:
print ("Error: " + error)
print ("LGTV Controller")
print ("Author: Karl Lattimer <[email protected]>")
print ("Usage: lgtv <command> [parameter]\n")
print ("Available Commands:")
print (" scan")
print (" auth Hostname/IP Authenticate and exit, creates initial config ~/.lgtv.json")
for c in getCommands(LGTVClient):
print (" " + c, end=" ")
print (" " * (20 - len(c)), end=" ")
args = getargspec(LGTVClient.__dict__[c])
print (' '.join(args.args[1:-1]))
def parseargs(command, argv):
args = getargspec(LGTVClient.__dict__[command])
args = args.args[1:-1]
if len(args) != len(argv):
raise Exception("Argument lengths do not match")
output = {}
for (i, a) in enumerate(args):
#
# do some basic conversions for bools, ints and floats
#
if argv[i].lower() == "true":
argv[i] = True
elif argv[i].lower() == "false":
argv[i] = False
try:
f = int(argv[i])
argv[i] = f
except:
try:
f = float(argv[i])
argv[i] = f
except:
pass
output[a] = argv[i]
return output
if __name__ == '__main__':
if len(sys.argv) < 2:
usage("Too few arguments")
elif sys.argv[1] == "scan":
results = LGTVScan()
if len(results) > 0:
print (json.dumps({
"result": "ok",
"count": len(results),
"list": results
}, sort_keys=True, indent=4))
else:
print (json.dumps({
"result": "failed",
"count": len(results)
}, sort_keys=True, indent=4))
elif sys.argv[1] == "on":
ws = LGTVClient()
ws.on()
elif sys.argv[1] == "auth":
if len(sys.argv) < 3:
usage("Hostname or IP is required for auth")
if os.path.exists(os.path.expanduser("~/.lgtv.json")):
os.remove(os.path.expanduser("~/.lgtv.json"))
ws = LGTVClient(sys.argv[2])
ws.connect()
ws.run_forever()
else:
try:
ws = LGTVClient()
try:
args = parseargs(sys.argv[1], sys.argv[2:])
except Exception as e:
usage(e.message)
ws.connect()
ws.exec_command(sys.argv[1], args)
ws.run_forever()
except KeyboardInterrupt:
ws.close()