Analyze_qsl.py

  1. import re
  2. import datetime
  3. import time
  4.  
  5. file_name = 'ok4bx-main-20120824.ADI'
  6.      
  7. def parse_adif(fn):
  8.     raw = re.split('<eor>|<eoh>(?i)',open(fn).read() )
  9.     raw.pop(0)  #remove header
  10.     raw.pop()   #remove last empty item
  11.     logbook =[]
  12.     for record in raw:
  13.         qso = {}
  14.         tags = re.findall('<(.*?):(\d+).*?>([^<\t\n\r\f\v\Z]+)',record)
  15.         for tag in tags:
  16.             qso[tag[0].lower()] = tag[2][:int(tag[1])]
  17.         logbook.append(qso)    
  18.     return logbook
  19.  
  20. def adif_date(d):
  21.     return datetime.datetime.fromtimestamp(time.mktime(time.strptime(d, "%Y%m%d")))
  22.  
  23. delta_year = datetime.timedelta(days=365)  
  24. log = parse_adif(file_name)
  25.  
  26. output = []
  27. for rec in log:
  28.     if rec.has_key('qsl_rcvd_via') and rec.has_key('qslrdate'):
  29.         if ((adif_date(rec['qslrdate']) > datetime.datetime.today() - delta_year)
  30.             and (rec['qsl_rcvd_via'].lower() == 'b')):
  31.             if rec.has_key('qsl_via'):
  32.                 output.append("- \"%s\":[http://qrz.com/db/%s] via %s" % (rec['call'],rec['call'],rec['qsl_via']))
  33.             else:
  34.                 output.append("- \"%s\":[http://qrz.com/db/%s]" % (rec['call'],rec['call']))
  35.  
  36. output = sorted(list(set(output))) #sort & remove dupes
  37. print "\n".join(output)
  38. print "Total of %s unique station." % (len(output))