-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetaddresses.py
executable file
·75 lines (65 loc) · 2.15 KB
/
getaddresses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
import sys
import getopt
import csv
import xml.etree.ElementTree as ET
import urllib
def isValid(v):
if v == None:
return ""
else:
return v.text
def lookup(key,call,ns):
url = "http://xmldata.qrz.com/xml/current/?s=" + key + ";callsign=" + call
calltxt = urllib.urlopen(url).read()
callroot = ET.XML(calltxt)
call = isValid(callroot.find('.//qrz:call',ns))
fname = isValid(callroot.find('.//qrz:fname',ns))
name = isValid(callroot.find('.//qrz:name',ns))
addr1 = isValid(callroot.find('.//qrz:addr1',ns))
addr2 = isValid(callroot.find('.//qrz:addr2',ns))
state = isValid(callroot.find('.//qrz:state',ns))
zipcode = isValid(callroot.find('.//qrz:zip',ns))
country = isValid(callroot.find('.//qrz:country',ns))
address = {'call': call,'fname':fname,'name':name,'addr1':addr1,'addr2':addr2,'state':state,'zip':zipcode,'country':country}
return address
def main(argv):
inputfile = ''
outputfile = ''
login = ''
pwd = ''
try:
opts, args = getopt.getopt(argv,"hi:l:p:o:")
except getopt.GetoptError:
print sys.argv[0] + ' -l <qrz login> -p <qrz password> -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print sys.argv[0] + ' -l <qrz login> -p <qrz password> -i <inputfile> -o <outputfile>'
sys.exit()
elif opt == '-l':
login = arg
elif opt == '-p':
pwd = arg
elif opt == '-i':
inputfile = arg
elif opt == '-o':
outputfile = arg
print 'Input file is ', inputfile
print 'Output file is ', outputfile
target_url = "http://xmldata.qrz.com/xml/current/?username=" + login + "&password=" + pwd + ";agent=q5.0"
sessiontxt = urllib.urlopen(target_url).read()
root = ET.XML(sessiontxt)
ns = {'qrz': 'http://xmldata.qrz.com'}
Key = root.find('.//qrz:Key',ns).text
with open(outputfile, 'w') as csvfile:
fieldnames = ['call','fname','name','addr1','addr2','state','zip','country']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
with open(inputfile,'r') as callfile:
reader = csv.DictReader(callfile);
for crow in reader:
print crow['CALL']
writer.writerow(lookup(Key,crow['CALL'],ns))
if __name__ == "__main__":
main(sys.argv[1:])