Skip to content

Commit

Permalink
Added disk caching to XML
Browse files Browse the repository at this point in the history
Added user configurable disk caching to lookups
  • Loading branch information
StephenGenusa committed Apr 3, 2015
1 parent 3a23fc8 commit cb4cb8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -33,3 +33,9 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# Local Settings File
settings.cfg

# Local XML Cache
/qrz-xml
44 changes: 33 additions & 11 deletions qrz/qrz_query.py
@@ -1,8 +1,10 @@
#!/usr/bin/env python
#coding:utf-8

import os
import requests
import re
import time
import xmltodict
from ConfigParser import SafeConfigParser

Expand Down Expand Up @@ -54,15 +56,35 @@ def _get_session(self):


def callsign(self, callsign):
if self._session_key is None:
self._get_session()
url = """http://xmldata.qrz.com/xml/current/?s={0}&callsign={1}""".format(self._session_key, callsign)
r = self._session.get(url)
if r.status_code != 200:
raise Exception("Error Querying")
raw = xmltodict.parse(r.content)
raw_xml = ''
xml_cache = self._cfg.cache['cache-path']
if xml_cache != '':
xml_filename = os.path.join(xml_cache, callsign + '.xml')
if os.path.isfile(xml_filename):
# Cache expires time is file's modification datetime + time allowed for file cache
# secs*mins*hours*days
cache_expiry_time = os.path.getmtime(xml_filename) + (60*60*24*int(self._cfg.cache['cache-expires']))
if cache_expiry_time > time.time():
raw_xml = "".join(open(xml_filename).readlines())
else:
os.remove(xml_filename)
if raw_xml == '':
if self._session_key is None:
self._get_session()
url = """http://xmldata.qrz.com/xml/current/?s={0}&callsign={1}""".format(self._session_key, callsign)
r = self._session.get(url)
if r.status_code != 200:
raise Exception("Error Querying")
if xml_cache != '':
if not os.path.isdir(xml_cache):
os.mkdir(xml_cache)
if r.content.find('<Callsign>') > -1:
open(xml_filename, 'a').writelines(r.content)
raw_xml = r.content

raw_dict = xmltodict.parse(raw_xml)
calldict = {}
if raw['QRZDatabase'].has_key('Callsign'):
for key in raw['QRZDatabase']['Callsign'].keys():
calldict[key] = raw['QRZDatabase']['Callsign'][key]
return calldict
if raw_dict['QRZDatabase'].has_key('Callsign'):
for key in raw_dict['QRZDatabase']['Callsign'].keys():
calldict[key] = raw_dict['QRZDatabase']['Callsign'][key]
return calldict
6 changes: 5 additions & 1 deletion qrz_example.py
Expand Up @@ -5,4 +5,8 @@
result = qrz.callsign("w7atc")
print result['fname'], result['name']
print result['addr2'], result['state']
print result['country']
print result['country']
# Show all the data available from QRZ.com
print '-' * 50
for dict_key, dict_value in sorted(result.items()):
print u'{0}: {1}'.format(dict_key, dict_value)
6 changes: 5 additions & 1 deletion settings_example.cfg
@@ -1,3 +1,7 @@
[qrz]
username=blah
password=blahblah
password=blahblah

[cache]
cache-path=./qrz-xml
cache-expires=30 # Days XML allowed to live in cache

0 comments on commit cb4cb8e

Please sign in to comment.