Code: Select all
import requests
data = '''\
<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>Q:0</ObjectID>
<BrowseFlag>BrowseDirectChildren</BrowseFlag>
<Filter>dc:title,dc:creator</Filter>
<StartingIndex>{starting_index}</StartingIndex>
<RequestedCount>10</RequestedCount>
<SortCriteria></SortCriteria>
</u:Browse>
</s:Body>
</s:Envelope>
'''
starting_index = eg.event.payload.split('=')[-1]
data = data.format(starting_index=starting_index )
headers = {
'Content-Type': 'text/html; charset=UTF-8',
'SOAPACTION': 'urn:schemas-upnp-org:service:ContentDirectory:1#Browse',
'Content-Length': len(data)
}
response = requests.post('http://192.168.0.32:1400/MediaServer/ContentDirectory/Control', data=data, headers=headers)
from xml.etree import ElementTree
data = response.content
# this function strips namespace information
def strip_namespaces(in_data):
lines = []
line = ''
for char in list(in_data):
if char == '>':
line += char
lines += [line]
line = ''
continue
elif char == '<' and line:
lines += [line]
line = ''
line += char
if line:
lines += [line]
for i, line in enumerate(lines):
if '<' not in line:
continue
while 'xmlns=' in line:
beg, end = line.split('xmlns=', 1)
end = end.split('"', 2)[-1].strip()
line = beg + end
while 'xmlns:' in line:
beg, end = line.split('xmlns:', 1)
xmlns, end = end.split('=', 1)
end = end.split('"', 2)[-1].strip()
line = beg + end
while xmlns + ':encodingStyle=' in line:
beg, end = line.split(xmlns + ':encodingStyle=', 1)
end = end.split('"', 2)[-1].strip()
line = beg + end
lines[i] = line
for j, lne in enumerate(lines[i:]):
if '<' not in lne:
continue
lne = lne.replace('<' + xmlns + ':', '<')
lne = lne.replace('</' + xmlns + ':', '</')
lines[i + j] = lne
out_data = ''.join(lines)
return ''.join(line for line in out_data.split('\n'))
# strip the namespace data here
data = strip_namespaces(data)
# create an xml object from the stripped data
xml = ElementTree.fromstring(data)
# locate the elements that we are looking for
body = xml.find('Body')
browse_response = body.find('BrowseResponse')
result = browse_response.find('Result')
# data is stored in the text area of the result field
data = result.text
# strip the namespaces from the result data
data = strip_namespaces(data)
# turn the result data into an xml object
xml = ElementTree.fromstring(data.encode('utf-8'))
track = 1
for item in xml:
message = 'artist' + str(track) + ':' + item.find('creator').text + ':'
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
message = 'title' + str(track) + ':' + item.find('title').text + ':'
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
if track == 1:
message = 'artist:' + item.find('creator').text + ':'
message = message.replace('!', '%21')
message = message.replace('&', '%26')
message = message.replace(';', '%3B')
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
message = 'album:' + item.find('album').text + ':'
message = message.replace('!', '%21')
message = message.replace('&', '%26')
message = message.replace(';', '%3B')
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
# print 'res:', item.find('res').text
# print 'albumArtURI:', item.find('albumArtURI').text
# print 'class:', item.find('class').text
track = track + 1
while track < 11:
message = 'artist' + str(track) + ': :'
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
message = 'title' + str(track) + ': :'
eg.plugins.BroadcastListener.Broadcast(message, u'', 33339)
track = track + 1
Peter