-
Notifications
You must be signed in to change notification settings - Fork 140
/
dlna.groovy
executable file
·79 lines (64 loc) · 2.59 KB
/
dlna.groovy
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
#!/usr/bin/env -S filebot -script
device = any{ _args.db }{ '*' } // --db 'http://192.168.1.101:50001/desc/device.xml'
query = any{ _args.query }{ '0' } // --q 'ObjectID'
agent = any{ agent }{ 'DLNADOC/1.50' } // --def agent='User-Agent'
mx = any{ mx }{ 2 } // --def mx='5'
def browse(controlURL, objectID, depth) {
def envelope = controlURL.post(
"""<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
<ObjectID>${objectID}</ObjectID>
<BrowseFlag>BrowseDirectChildren</BrowseFlag>
<StartingIndex>0</StartingIndex>
<RequestedCount>1000</RequestedCount>
</u:Browse>
</s:Body>
</s:Envelope>
""".replaceAll(/\n|\t/, '').getBytes('UTF-8'), 'text/xml; charset="utf-8"', ['User-Agent': agent, SOAPACTION: '"urn:schemas-upnp-org:service:ContentDirectory:1#Browse"']
).text
def response = new XmlSlurper().parseText(envelope).Body.BrowseResponse.Result.text()
if (_args.outputPath) {
def f = response.saveAs(_args.outputPath.resolve("${controlURL.host}_${controlURL.port}_ObjectID_${objectID}.xml".validateFileName()))
help "* Save as ${f}"
}
def xml = new XmlSlurper().parseText(response)
xml.item.each{ item ->
log.fine "${'\t' * depth}[ObjectID=${item.'@id'}] ${item.title}"
item.children().each{ element ->
if (element.attributes()) {
log.finest "${'\t' * depth}└─ ${element.name()} ${element.attributes().collect{ k, v -> k.removeBrackets() + '=' + v }} ${element.text()}"
}
}
}
xml.container.each{ container ->
log.info "${'\t' * depth}[ObjectID=${container.'@id'}] ${container.title}"
browse(controlURL, container.'@id', depth + 1)
}
}
def crawl(deviceURL) {
def response = deviceURL.get('User-Agent': agent).text
if (_args.outputPath) {
def f = response.saveAs(_args.outputPath.resolve("${deviceURL.host}_${deviceURL.port}_device.xml".validateFileName()))
help "* Save as ${f}"
}
def xml = new XmlSlurper().parseText(response)
log.info "[${xml.device.friendlyName}] ${deviceURL}"
def controlURL = xml.device.serviceList.service.each{ service ->
if (service.serviceType.text() == 'urn:schemas-upnp-org:service:ContentDirectory:1') {
try {
browse(deviceURL / service.controlURL.text(), query, 0)
} catch(e) {
log.severe "${e.message}"
}
}
}
}
if (device == '*') {
SSDP.discover('urn:schemas-upnp-org:device:MediaServer:1', mx){ ms ->
crawl(ms.'LOCATION'.toURL())
}
} else {
crawl(device.toURL())
}