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 threading
from owrx.map import Map, LatLngLocation
from owrx.bands import Bandplan
from owrx.parser import Parser
logger = logging.getLogger(__name__)

View File

@ -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:

View File

@ -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()