From b948e06a4fe677a5d92874b3bdcb5288f1c57344 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 15 Feb 2020 00:16:04 +0100 Subject: [PATCH] use urllib to update sdr.hu, no wget dependency ref: #52 --- owrx/meta.py | 1 - owrx/sdrhu.py | 40 ++++++++++++++++++++++------------------ sdrhu.py | 12 ++++++++++-- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/owrx/meta.py b/owrx/meta.py index 3d1b3d9..d69127d 100644 --- a/owrx/meta.py +++ b/owrx/meta.py @@ -5,7 +5,6 @@ from datetime import datetime, timedelta import logging import threading from owrx.map import Map, LatLngLocation -from owrx.bands import Bandplan from owrx.parser import Parser logger = logging.getLogger(__name__) diff --git a/owrx/sdrhu.py b/owrx/sdrhu.py index c84a2f5..f9b5207 100644 --- a/owrx/sdrhu.py +++ b/owrx/sdrhu.py @@ -1,7 +1,7 @@ import threading -import subprocess import time from owrx.config import PropertyManager +from urllib import request, parse import logging @@ -14,24 +14,28 @@ class SdrHuUpdater(threading.Thread): super().__init__(daemon=True) def update(self): - pm = PropertyManager.getSharedInstance() - cmd = 'wget --timeout=15 -4qO- https://sdr.hu/update --post-data "url=http://{server_hostname}:{web_port}&apikey={sdrhu_key}" 2>&1'.format( - **pm.__dict__() - ) - logger.debug(cmd) - returned = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate() - returned = returned[0].decode("utf-8") - if "UPDATE:" in returned: - retrytime_mins = 20 - value = returned.split("UPDATE:")[1].split("\n", 1)[0] - if value.startswith("SUCCESS"): - logger.info("Update succeeded!") - else: - logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value) + pm = PropertyManager.getSharedInstance().collect("server_hostname", "web_port", "sdrhu_key") + data = parse.urlencode({ + "url": "http://{server_hostname}:{web_port}".format(**pm.__dict__()), + "apikey": pm["sdrhu_key"] + }).encode() + + res = request.urlopen("https://sdr.hu/update", data=data) + if res.getcode() < 200 or res.getcode() >= 300: + 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] + if value.startswith("SUCCESS"): + logger.info("Update succeeded!") else: - retrytime_mins = 2 - logger.warning("wget failed while updating, your receiver cannot be listed on sdr.hu!") - return retrytime_mins + logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value) + return 20 def run(self): while self.doRun: diff --git a/sdrhu.py b/sdrhu.py index d74d166..6dd9daa 100755 --- a/sdrhu.py +++ b/sdrhu.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 """ This file is part of OpenWebRX, @@ -24,9 +24,17 @@ from owrx.sdrhu import SdrHuUpdater 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__": 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) SdrHuUpdater().update()