2019-05-12 12:35:25 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2020-03-21 21:40:39 +00:00
|
|
|
from owrx.config import Config
|
2020-02-14 23:16:04 +00:00
|
|
|
from urllib import request, parse
|
2019-05-12 12:35:25 +00:00
|
|
|
|
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-12 12:35:25 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class SdrHuUpdater(threading.Thread):
|
|
|
|
def __init__(self):
|
|
|
|
self.doRun = True
|
2019-07-21 17:40:28 +00:00
|
|
|
super().__init__(daemon=True)
|
2019-05-12 12:35:25 +00:00
|
|
|
|
|
|
|
def update(self):
|
2020-03-24 21:16:11 +00:00
|
|
|
pm = Config.get().filter("server_hostname", "web_port", "sdrhu_key")
|
2020-02-14 23:16:04 +00:00
|
|
|
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!")
|
2019-05-12 12:35:25 +00:00
|
|
|
else:
|
2020-02-14 23:16:04 +00:00
|
|
|
logger.warning("Update failed, your receiver cannot be listed on sdr.hu! Reason: %s", value)
|
|
|
|
return 20
|
2019-05-12 12:35:25 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while self.doRun:
|
|
|
|
retrytime_mins = self.update()
|
2019-07-21 17:40:28 +00:00
|
|
|
time.sleep(60 * retrytime_mins)
|