1
0
mirror of https://github.com/rofafor/vdr-plugin-satip.git synced 2023-10-10 13:37:42 +02:00

Update detectsatip for Python3

This commit is contained in:
Rolf Ahrenberg 2021-03-13 19:28:17 +02:00
parent ba0b04ba12
commit 27122747a7

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Simple tool to detect SAT>IP devices as JSON. """ Simple tool to detect SAT>IP devices as JSON.
""" """
import json import json
@ -8,17 +7,22 @@ import sys
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import requests import requests
SSDP_BIND = '0.0.0.0' SSDP_BIND = "0.0.0.0"
SSDP_ADDR = '239.255.255.250' SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900 SSDP_PORT = 1900
SSDP_MX = 1 SSDP_MX = 1
SSDP_ST = 'urn:ses-com:device:SatIPServer:1' SSDP_ST = "urn:ses-com:device:SatIPServer:1"
SSDP_REQUEST = 'M-SEARCH * HTTP/1.1\r\n' + \ SSDP_REQUEST = "\r\n".join(
'HOST: %s:%d\r\n' % (SSDP_ADDR, SSDP_PORT) + \ [
'MAN: "ssdp:discover"\r\n' + \ "M-SEARCH * HTTP/1.1",
'MX: %d\r\n' % (SSDP_MX, ) + \ f"HOST: {SSDP_ADDR}:{SSDP_PORT}",
'ST: %s\r\n' % (SSDP_ST, ) + \ 'MAN: "ssdp:discover"',
'\r\n' f"MX: {SSDP_MX}",
f"ST: {SSDP_ST}",
"USER-AGENT: vdr-detectsatip",
"\r\n",
]
)
def parse_satip_xml(data): def parse_satip_xml(data):
@ -30,15 +34,15 @@ def parse_satip_xml(data):
Returns: Returns:
dict: Parsed SAT>IP device name and frontend information. dict: Parsed SAT>IP device name and frontend information.
""" """
result = {'name': '', 'frontends': {}} result = {"name": "", "frontends": {}}
if data: if data:
root = ET.fromstring(data) root = ET.fromstring(data)
name = root.find('.//*/{urn:schemas-upnp-org:device-1-0}friendlyName') name = root.find(".//*/{urn:schemas-upnp-org:device-1-0}friendlyName")
result['name'] = name.text result["name"] = name.text
satipcap = root.find('.//*/{urn:ses-com:satip}X_SATIPCAP') satipcap = root.find(".//*/{urn:ses-com:satip}X_SATIPCAP")
if satipcap is None: if satipcap is None:
# fallback for non-standard Panasonic # fallback for non-standard Panasonic
satipcap = root.find('.//*/{urn-ses-com:satip}X_SATIPCAP') satipcap = root.find(".//*/{urn-ses-com:satip}X_SATIPCAP")
caps = {} caps = {}
for system in satipcap.text.split(","): for system in satipcap.text.split(","):
cap = system.split("-") cap = system.split("-")
@ -47,7 +51,7 @@ def parse_satip_xml(data):
if cap[0] in caps: if cap[0] in caps:
count = count + caps[cap[0]] count = count + caps[cap[0]]
caps[cap[0]] = count caps[cap[0]] = count
result['frontends'] = caps result["frontends"] = caps
return result return result
@ -69,14 +73,14 @@ def detect_satip_devices():
pass pass
sock.settimeout(1) sock.settimeout(1)
sock.bind((SSDP_BIND, SSDP_PORT)) sock.bind((SSDP_BIND, SSDP_PORT))
sock.sendto(SSDP_REQUEST, (SSDP_ADDR, SSDP_PORT)) sock.sendto(SSDP_REQUEST.encode("utf-8"), (SSDP_ADDR, SSDP_PORT))
try: try:
while 1: while 1:
data = sock.recv(1024) data = sock.recv(1024).decode("utf-8")
if data: if data:
for row in data.split('\r\n'): for row in data.split("\r\n"):
if 'LOCATION:' in row: if "LOCATION:" in row:
url = row.replace('LOCATION:', '').strip() url = row.replace("LOCATION:", "").strip()
if url in urls: if url in urls:
continue continue
urls.append(url) urls.append(url)
@ -90,5 +94,5 @@ def detect_satip_devices():
return devices return devices
if __name__ == '__main__': if __name__ == "__main__":
json.dump(detect_satip_devices(), fp=sys.stdout, sort_keys=True, indent=2) json.dump(detect_satip_devices(), fp=sys.stdout, sort_keys=True, indent=2)