openwebrx-clone/owrx/meta.py

66 lines
2.3 KiB
Python
Raw Normal View History

2019-05-30 15:19:46 +00:00
from owrx.config import PropertyManager
from urllib import request
import json
from datetime import datetime, timedelta
import logging
2019-05-30 16:32:08 +00:00
import threading
2019-05-30 15:19:46 +00:00
logger = logging.getLogger(__name__)
class DmrMetaEnricher(object):
def __init__(self):
self.cache = {}
2019-05-30 16:32:08 +00:00
self.threads = {}
2019-05-30 15:19:46 +00:00
self.cacheTimeout = timedelta(seconds = 86400)
def cacheEntryValid(self, id):
if not id in self.cache: return False
entry = self.cache[id]
return entry["timestamp"] + self.cacheTimeout > datetime.now()
2019-05-30 16:32:08 +00:00
def downloadRadioIdData(self, id):
try:
logger.debug("requesting DMR metadata for id=%s", id)
res = request.urlopen("https://www.radioid.net/api/dmr/user/?id={0}".format(id), timeout=5).read()
data = json.loads(res.decode("utf-8"))
self.cache[id] = {
"timestamp": datetime.now(),
"data": data
}
except json.JSONDecodeError:
self.cache[id] = {
"timestamp": datetime.now(),
"data": None
}
del self.threads[id]
2019-05-30 15:19:46 +00:00
def enrich(self, meta):
if not PropertyManager.getSharedInstance()["digital_voice_dmr_id_lookup"]: return None
if not "source" in meta: return None
2019-05-30 16:32:08 +00:00
id = meta["source"]
if not self.cacheEntryValid(id):
if not id in self.threads:
self.threads[id] = threading.Thread(target=self.downloadRadioIdData, args=[id])
self.threads[id].start()
return None
data = self.cache[id]["data"]
2019-05-30 15:19:46 +00:00
if "count" in data and data["count"] > 0 and "results" in data:
return data["results"][0]
return None
2019-05-30 14:12:13 +00:00
class MetaParser(object):
2019-05-30 15:19:46 +00:00
enrichers = {
"DMR": DmrMetaEnricher()
}
2019-05-30 14:12:13 +00:00
def __init__(self, handler):
self.handler = handler
def parse(self, meta):
fields = meta.split(";")
2019-05-30 15:19:46 +00:00
meta = {v[0] : "".join(v[1:]) for v in map(lambda x: x.split(":"), fields)}
if "protocol" in meta:
protocol = meta["protocol"]
if protocol in MetaParser.enrichers:
additional_data = MetaParser.enrichers[protocol].enrich(meta)
if additional_data is not None: meta["additional"] = additional_data
self.handler.write_metadata(meta)