download dmr ids asynchronously
This commit is contained in:
parent
7100d43d9e
commit
f565b4dbcd
40
owrx/meta.py
40
owrx/meta.py
@ -3,36 +3,44 @@ from urllib import request
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class DmrMetaEnricher(object):
|
class DmrMetaEnricher(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
self.threads = {}
|
||||||
self.cacheTimeout = timedelta(seconds = 86400)
|
self.cacheTimeout = timedelta(seconds = 86400)
|
||||||
def cacheEntryValid(self, id):
|
def cacheEntryValid(self, id):
|
||||||
if not id in self.cache: return False
|
if not id in self.cache: return False
|
||||||
entry = self.cache[id]
|
entry = self.cache[id]
|
||||||
return entry["timestamp"] + self.cacheTimeout > datetime.now()
|
return entry["timestamp"] + self.cacheTimeout > datetime.now()
|
||||||
|
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]
|
||||||
def enrich(self, meta):
|
def enrich(self, meta):
|
||||||
if not PropertyManager.getSharedInstance()["digital_voice_dmr_id_lookup"]: return None
|
if not PropertyManager.getSharedInstance()["digital_voice_dmr_id_lookup"]: return None
|
||||||
if not "source" in meta: return None
|
if not "source" in meta: return None
|
||||||
source = meta["source"]
|
id = meta["source"]
|
||||||
if not self.cacheEntryValid(source):
|
if not self.cacheEntryValid(id):
|
||||||
try:
|
if not id in self.threads:
|
||||||
logger.debug("requesting DMR metadata for id=%s", source)
|
self.threads[id] = threading.Thread(target=self.downloadRadioIdData, args=[id])
|
||||||
res = request.urlopen("https://www.radioid.net/api/dmr/user/?id={0}".format(source), timeout=5).read()
|
self.threads[id].start()
|
||||||
data = json.loads(res.decode("utf-8"))
|
return None
|
||||||
self.cache[source] = {
|
data = self.cache[id]["data"]
|
||||||
"timestamp": datetime.now(),
|
|
||||||
"data": data
|
|
||||||
}
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
self.cache[source] = {
|
|
||||||
"timestamp": datetime.now(),
|
|
||||||
"data": None
|
|
||||||
}
|
|
||||||
data = self.cache[source]["data"]
|
|
||||||
if "count" in data and data["count"] > 0 and "results" in data:
|
if "count" in data and data["count"] > 0 and "results" in data:
|
||||||
return data["results"][0]
|
return data["results"][0]
|
||||||
return None
|
return None
|
||||||
|
Loading…
Reference in New Issue
Block a user