use urllib to update sdr.hu, no wget dependency

ref: #52
This commit is contained in:
Jakob Ketterl 2020-02-15 00:16:04 +01:00
parent eaa98b0d64
commit b948e06a4f
3 changed files with 32 additions and 21 deletions

View File

@ -5,7 +5,6 @@ from datetime import datetime, timedelta
import logging import logging
import threading import threading
from owrx.map import Map, LatLngLocation from owrx.map import Map, LatLngLocation
from owrx.bands import Bandplan
from owrx.parser import Parser from owrx.parser import Parser
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -1,7 +1,7 @@
import threading import threading
import subprocess
import time import time
from owrx.config import PropertyManager from owrx.config import PropertyManager
from urllib import request, parse
import logging import logging
@ -14,24 +14,28 @@ class SdrHuUpdater(threading.Thread):
super().__init__(daemon=True) super().__init__(daemon=True)
def update(self): def update(self):
pm = PropertyManager.getSharedInstance() pm = PropertyManager.getSharedInstance().collect("server_hostname", "web_port", "sdrhu_key")
cmd = 'wget --timeout=15 -4qO- https://sdr.hu/update --post-data "url=http://{server_hostname}:{web_port}&apikey={sdrhu_key}" 2>&1'.format( data = parse.urlencode({
**pm.__dict__() "url": "http://{server_hostname}:{web_port}".format(**pm.__dict__()),
) "apikey": pm["sdrhu_key"]
logger.debug(cmd) }).encode()
returned = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()
returned = returned[0].decode("utf-8") res = request.urlopen("https://sdr.hu/update", data=data)
if "UPDATE:" in returned: if res.getcode() < 200 or res.getcode() >= 300:
retrytime_mins = 20 logger.warning('sdr.hu update failed with error code %i', res.getcode())
return 2
returned = res.read().decode("utf-8")
if "UPDATE:" not in returned:
logger.warning("Update failed, your receiver cannot be listed on sdr.hu!")
return 2
value = returned.split("UPDATE:")[1].split("\n", 1)[0] value = returned.split("UPDATE:")[1].split("\n", 1)[0]
if value.startswith("SUCCESS"): if value.startswith("SUCCESS"):
logger.info("Update succeeded!") logger.info("Update succeeded!")
else: else:
logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value) logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value)
else: return 20
retrytime_mins = 2
logger.warning("wget failed while updating, your receiver cannot be listed on sdr.hu!")
return retrytime_mins
def run(self): def run(self):
while self.doRun: while self.doRun:

View File

@ -1,4 +1,4 @@
#!/usr/bin/python2 #!/usr/bin/python3
""" """
This file is part of OpenWebRX, This file is part of OpenWebRX,
@ -24,9 +24,17 @@
from owrx.sdrhu import SdrHuUpdater from owrx.sdrhu import SdrHuUpdater
from owrx.config import PropertyManager from owrx.config import PropertyManager
import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
if __name__ == "__main__": if __name__ == "__main__":
pm = PropertyManager.getSharedInstance().loadConfig() pm = PropertyManager.getSharedInstance().loadConfig()
if not "sdrhu_key" in pm: if "sdrhu_public_listing" not in pm or not pm["sdrhu_public_listing"]:
logger.error('Public listing on sdr.hu is not activated. Please check "sdrhu_public_listing" in your config.')
exit(1)
if "sdrhu_key" not in pm or pm["sdrhu_key"] is None or pm["sdrhu_key"] == "":
logger.error('Missing "sdrhu_key" in your config. Aborting')
exit(1) exit(1)
SdrHuUpdater().update() SdrHuUpdater().update()