-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmpvDLNA.py
136 lines (111 loc) · 4.05 KB
/
mpvDLNA.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import sys
# Try to import upnp
upnp = True
try:
import upnpclient
except ImportError as error:
upnp = False
# Try to import wake on lan
wol = True
try:
import wakeonlan
except ImportError as error:
wol = False
from lxml import etree
import logging
# important information is passed through stdout so we need to supress
# the output of the upnp client module
logging.getLogger("upnpclient").setLevel(logging.CRITICAL)
logging.getLogger("ssdp").setLevel(logging.CRITICAL)
sys.stdout.reconfigure(encoding='utf-8')
def wake(mac):
if wol:
try:
wakeonlan.send_magic_packet(mac);
print("packet sent")
except:
print("send failed")
else:
print("wakeonlan import failed")
def info(url, id, count):
device = upnpclient.Device(url)
result = device.ContentDirectory.Browse(ObjectID=id, BrowseFlag="BrowseMetadata", Filter="*", StartingIndex=0, RequestedCount=count, SortCriteria="")
root = etree.fromstring(result["Result"])
# Determine if we should be looking at items or containers
list = root.findall("./item", root.nsmap)
type = "item"
if len(list) == 0:
list = root.findall("./container", root.nsmap)
type = "container"
print(type)
for t in list:
print("")
print(t.findtext("upnp:episodeNumber", "No Episode Number", root.nsmap))
print(t.findtext("dc:description", "No Description", root.nsmap))
def browse(url, id, count):
device = upnpclient.Device(url)
result = device.ContentDirectory.Browse(ObjectID=id, BrowseFlag="BrowseDirectChildren", Filter="*", StartingIndex=0, RequestedCount=count, SortCriteria="")
root = etree.fromstring(result["Result"])
list = {}
# Determine if we should be looking at items or containers
list["item"] = root.findall("./item", root.nsmap)
list["container"] = root.findall("./container", root.nsmap)
for type in list.keys():
print(type + "s:")
for t in list[type]:
print("")
print(t.findtext("dc:title", "untitled", root.nsmap))
print(t.get("id"))
if type == "item":
print(t.findtext("res", "", root.nsmap))
print("\x1F")
def list(timeout):
devices = []
possibleDevices = upnpclient.discover(timeout)
for device in possibleDevices:
if "MediaServer" in device.device_type:
addToList = True
for d in devices:
if d.friendly_name == device.friendly_name:
addToList = False
break
if addToList:
devices.append(device)
for device in devices:
print("")
print(device.friendly_name)
print(device.location)
def help():
print("mpvDLNA.py supports the following commands:")
print("-h, --help Prints the help dialog")
print("-v, --version Prints version information")
print("-l, --list Takes a timeout in seconds and outputs a list of DLNA Media Servers on the network")
print("-b, --browse Takes a DLNA url and the id of a DLNA element and outputs its direct children")
print("-i, --info Takes a DLNA url and the id of a DLNA element and outputs its metadata")
print("-w, --wake Takes a MAC address and attempts to send a wake on lan packet to it")
if len(sys.argv) == 2:
if sys.argv[1] == "-v" or sys.argv[1] == "--version":
print("mpvDLNA.py Plugin Version 2.1.0")
if not upnp:
print("upnp import failed")
else:
help()
elif len(sys.argv) == 3:
if sys.argv[1] == "-l" or sys.argv[1] == "--list":
list(int(sys.argv[2]))
elif sys.argv[1] == "-w" or sys.argv[1] == "--wake":
wake(sys.argv[2])
else:
help()
elif len(sys.argv) >= 4:
count = 2000
if len(sys.argv) == 5:
count = sys.argv[4]
if sys.argv[1] == "-b" or sys.argv[1] == "--browse":
browse(sys.argv[2], sys.argv[3], count)
elif sys.argv[1] == "-i" or sys.argv[1] == "--info":
info(sys.argv[2], sys.argv[3], count)
else:
help()
else:
help()