kopia lustrzana https://github.com/n1ywb/python-hamtools
Improve QRZ error handling; cache not found responses to reduce load
rodzic
68065a2620
commit
827dfcc059
|
@ -37,10 +37,13 @@ testSessionXML = """\
|
||||||
</QRZDatabase>
|
</QRZDatabase>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class NotFound(Exception):
|
class QrzError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class CallMismatch(Exception):
|
class NotFound(QrzError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CallMismatch(QrzError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Callsign(object):
|
class Callsign(object):
|
||||||
|
@ -98,27 +101,6 @@ class Session(object):
|
||||||
raise Exception("Status %d" % resp.status)
|
raise Exception("Status %d" % resp.status)
|
||||||
return resp.read()
|
return resp.read()
|
||||||
|
|
||||||
def checkErr(self, session):
|
|
||||||
errs = []
|
|
||||||
try:
|
|
||||||
errs = session.getElementsByTagName("Error")
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if len(errs) > 0:
|
|
||||||
err = errs[0].firstChild.data
|
|
||||||
if err.startswith("Not found"):
|
|
||||||
raise NotFound(err)
|
|
||||||
else:
|
|
||||||
raise Exception(err)
|
|
||||||
|
|
||||||
alert = None
|
|
||||||
try:
|
|
||||||
alert = session.getElementsByTagName("Alert")[0].firstChild.data
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if alert is not None:
|
|
||||||
raise Exception(alert)
|
|
||||||
|
|
||||||
|
|
||||||
def qrz(self, callsign):
|
def qrz(self, callsign):
|
||||||
# http://xml.qrz.com/xml?s=2331uf894c4bd29f3923f3bacf02c532d7bd9;callsign=aa7bq
|
# http://xml.qrz.com/xml?s=2331uf894c4bd29f3923f3bacf02c532d7bd9;callsign=aa7bq
|
||||||
|
@ -128,24 +110,32 @@ class Session(object):
|
||||||
xml = c.execute('select val from dict where key == ?',
|
xml = c.execute('select val from dict where key == ?',
|
||||||
(callsign,)).fetchone()
|
(callsign,)).fetchone()
|
||||||
if not xml:
|
if not xml:
|
||||||
xml = self.request(dict(s=self.key,callsign=callsign))
|
|
||||||
log.debug("miss %s" % callsign)
|
log.debug("miss %s" % callsign)
|
||||||
|
xml = self.request(dict(s=self.key,callsign=callsign))
|
||||||
else:
|
else:
|
||||||
|
log.debug("hit %s" % callsign)
|
||||||
miss = False
|
miss = False
|
||||||
xml = xml[0]
|
xml = xml[0]
|
||||||
log.debug("hit %s" % callsign)
|
|
||||||
try:
|
try:
|
||||||
dom = minidom.parseString(xml)
|
dom = minidom.parseString(xml)
|
||||||
session = dom.getElementsByTagName("Session")[0]
|
session = Callsign(dom.getElementsByTagName("Session")[0])
|
||||||
self.checkErr(session)
|
e = session['Error']
|
||||||
|
if e:
|
||||||
|
if not e.startswith("Not found"):
|
||||||
|
raise QrzError(e)
|
||||||
if miss:
|
if miss:
|
||||||
self.db.execute("insert into dict values (?, ?)", (callsign, xml))
|
self.db.execute("insert into dict values (?, ?)", (callsign, xml))
|
||||||
self.db.commit();
|
self.db.commit();
|
||||||
|
if e:
|
||||||
|
raise NotFound(callsign)
|
||||||
callnode = dom.getElementsByTagName("Callsign")[0]
|
callnode = dom.getElementsByTagName("Callsign")[0]
|
||||||
data = Callsign(callnode)
|
data = Callsign(callnode)
|
||||||
if data['call'].lower() != callsign.lower():
|
if data['call'].lower() != callsign.lower():
|
||||||
raise CallMismatch("Calls do not match", data['call'], callsign)
|
raise CallMismatch("Calls do not match", data['call'], callsign)
|
||||||
return data
|
return data
|
||||||
|
except QrzError, e:
|
||||||
|
log.warning("QRZ.com lookup failed: %s", repr(e))
|
||||||
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
log.debug(xml)
|
log.debug(xml)
|
||||||
raise
|
raise
|
||||||
|
|
Ładowanie…
Reference in New Issue