added error handling
This commit is contained in:
parent
faa7e43cb6
commit
d601462184
138
qrzlogger.py
138
qrzlogger.py
|
@ -116,24 +116,68 @@ def get_session():
|
||||||
session_key = raw_session.get('QRZDatabase').get('Session').get('Key')
|
session_key = raw_session.get('QRZDatabase').get('Session').get('Key')
|
||||||
if session_key:
|
if session_key:
|
||||||
return True
|
return True
|
||||||
|
except requests.exceptions.ConnectionError as e_conn:
|
||||||
|
print(errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
||||||
|
print(e_conn)
|
||||||
|
print("\nPlease check if\n * username and password are correct (see config.ini)\n * you are connected to the internet")
|
||||||
|
print(style.RESET)
|
||||||
except:
|
except:
|
||||||
pass
|
print(errorcol + "\nsomething unexpected has happened:\n")
|
||||||
|
print(e_conn)
|
||||||
|
print(style.RESET)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Sends a POST request to QRZ.com, checks for errors
|
||||||
|
# and returns teh response
|
||||||
|
def sendRequest(post_data):
|
||||||
|
try:
|
||||||
|
resp = requests.post(config['qrzlogger']['api_url'], headers=headers, data=post_data)
|
||||||
|
if resp.status_code == 200:
|
||||||
|
str_resp = resp.content.decode("utf-8")
|
||||||
|
response = urllib.parse.unquote(str_resp)
|
||||||
|
resp_list = response.splitlines()
|
||||||
|
if resp_list[0]:
|
||||||
|
if "invalid api key" in resp_list[0]:
|
||||||
|
print(errorcol + "\nThe API key configured in config.ini is not correct.\n" + style.RESET)
|
||||||
|
else:
|
||||||
|
return response
|
||||||
|
elif resp.status_code == 404:
|
||||||
|
print(errorcol + "\nThe API URL could not be found. Please check the URL in config.ini\n" + style.RESET)
|
||||||
|
except requests.exceptions.ConnectionError as e_conn:
|
||||||
|
print(errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
||||||
|
print(e_conn)
|
||||||
|
print("\nPlease check if you are connected to the internet")
|
||||||
|
print(style.RESET)
|
||||||
|
except:
|
||||||
|
print(errorcol + "\nsomething unexpected has happened:\n")
|
||||||
|
print(e_conn)
|
||||||
|
print(style.RESET)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
# Query QRZ.com's xml api to gather information
|
# Query QRZ.com's xml api to gather information
|
||||||
# about a specific call sign
|
# about a specific call sign
|
||||||
def getCallData(call):
|
def getCallData(call):
|
||||||
global session
|
global session
|
||||||
global session_key
|
global session_key
|
||||||
|
|
||||||
xml_url = """https://xmldata.QRZ.com/xml/current/?s={0}&callsign={1}""" .format(session_key, call)
|
try:
|
||||||
r = session.get(xml_url)
|
xml_url = """https://xmldata.QRZ.com/xml/current/?s={0}&callsign={1}""" .format(session_key, call)
|
||||||
raw = xmltodict.parse(r.content).get('QRZDatabase')
|
r = session.get(xml_url)
|
||||||
calldata = raw.get('Callsign')
|
raw = xmltodict.parse(r.content).get('QRZDatabase')
|
||||||
if calldata:
|
calldata = raw.get('Callsign')
|
||||||
return calldata
|
if calldata:
|
||||||
|
return calldata
|
||||||
|
except requests.exceptions.ConnectionError as e_conn:
|
||||||
|
print(errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
||||||
|
print(e_conn)
|
||||||
|
print("\nPlease check if you are connected to the internet")
|
||||||
|
print(style.RESET)
|
||||||
|
except:
|
||||||
|
print(errorcol + "\nsomething unexpected has happened:\n")
|
||||||
|
print(e_conn)
|
||||||
|
print(style.RESET)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -145,26 +189,26 @@ def getQSOsForCallsign(callsign):
|
||||||
'ACTION' : 'FETCH',
|
'ACTION' : 'FETCH',
|
||||||
'OPTION' : "TYPE:ADIF,CALL:" + callsign
|
'OPTION' : "TYPE:ADIF,CALL:" + callsign
|
||||||
}
|
}
|
||||||
post_data_enc = urllib.parse.urlencode(post_data)
|
post_data = urllib.parse.urlencode(post_data)
|
||||||
|
|
||||||
resp = requests.post(config['qrzlogger']['api_url'], headers=headers, data=post_data_enc)
|
response = sendRequest(post_data)
|
||||||
|
|
||||||
str_resp = resp.content.decode("utf-8")
|
if response:
|
||||||
response = urllib.parse.unquote(str_resp)
|
resp_list = response.splitlines()
|
||||||
|
result = [{}]
|
||||||
resp_list = response.splitlines()
|
for i in resp_list:
|
||||||
result = [{}]
|
if not i:
|
||||||
for i in resp_list:
|
result.append({})
|
||||||
if not i:
|
else:
|
||||||
result.append({})
|
if any(s+":" in i for s in config['qrzlogger']['xml_fields']):
|
||||||
else:
|
i = re.sub('<','',i, flags=re.DOTALL)
|
||||||
if any(s+":" in i for s in config['qrzlogger']['xml_fields']):
|
i = re.sub(':.*>',":",i, flags=re.DOTALL)
|
||||||
i = re.sub('<','',i, flags=re.DOTALL)
|
v = re.sub('^.*:',"",i, flags=re.DOTALL)
|
||||||
i = re.sub(':.*>',":",i, flags=re.DOTALL)
|
k = re.sub(':.*$',"",i, flags=re.DOTALL)
|
||||||
v = re.sub('^.*:',"",i, flags=re.DOTALL)
|
result[-1][k] = v
|
||||||
k = re.sub(':.*$',"",i, flags=re.DOTALL)
|
return result
|
||||||
result[-1][k] = v
|
else:
|
||||||
return result
|
return None
|
||||||
|
|
||||||
|
|
||||||
# Generate a pretty ascii table containing all
|
# Generate a pretty ascii table containing all
|
||||||
|
@ -297,26 +341,27 @@ def sendQSO(qso):
|
||||||
# URL encode the payload
|
# URL encode the payload
|
||||||
data = urllib.parse.urlencode(post_data)
|
data = urllib.parse.urlencode(post_data)
|
||||||
# send the POST request to QRZ.com
|
# send the POST request to QRZ.com
|
||||||
resp = requests.post(config['qrzlogger']['api_url'], headers=headers, data=data)
|
response = sendRequest(data)
|
||||||
str_resp = resp.content.decode("utf-8")
|
|
||||||
response = urllib.parse.unquote(str_resp)
|
|
||||||
# Check if the upload failed and print out
|
# Check if the upload failed and print out
|
||||||
# the reason plus some additional info
|
# the reason plus some additional info
|
||||||
if "STATUS=FAIL" in response:
|
if response:
|
||||||
print(errorcol)
|
if "STATUS=FAIL" in response:
|
||||||
print("QSO upload failed. QRZ.com has send the following reason:\n")
|
print(errorcol)
|
||||||
resp_list = response.split("&")
|
print("QSO upload failed. QRZ.com has send the following reason:\n")
|
||||||
for item in resp_list:
|
resp_list = response.split("&")
|
||||||
print(item)
|
for item in resp_list:
|
||||||
print("\nPlease review the following request that led to this error:\n")
|
print(item)
|
||||||
print(style.RESET)
|
print("\nPlease review the following request that led to this error:\n")
|
||||||
print(post_data)
|
print(style.RESET)
|
||||||
|
print(post_data)
|
||||||
|
else:
|
||||||
|
print(successcol)
|
||||||
|
print("QSO successfully uploaded to QRZ.com")
|
||||||
|
print(style.RESET)
|
||||||
|
is_ok = True
|
||||||
|
return is_ok
|
||||||
else:
|
else:
|
||||||
print(successcol)
|
print(errorcol + "\nA critical error occured. Please review all previous output." + style.RESET)
|
||||||
print("QSO successfully uploaded to QRZ.com")
|
|
||||||
print(style.RESET)
|
|
||||||
is_ok = True
|
|
||||||
return is_ok
|
|
||||||
|
|
||||||
|
|
||||||
# ask a user a simple y/n question
|
# ask a user a simple y/n question
|
||||||
|
@ -335,8 +380,6 @@ def askUser(question):
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
keeponlogging = True
|
keeponlogging = True
|
||||||
# get a session after logging into QRZ with user/pass
|
|
||||||
get_session()
|
|
||||||
|
|
||||||
# print an awesome banner
|
# print an awesome banner
|
||||||
print(logocol + " _ ")
|
print(logocol + " _ ")
|
||||||
|
@ -345,6 +388,9 @@ if __name__ == '__main__':
|
||||||
print(" \__, |_| /__|_\___/\__, \__, \___|_| ")
|
print(" \__, |_| /__|_\___/\__, \__, \___|_| ")
|
||||||
print(" |_| |___/|___/ " + style.RESET)
|
print(" |_| |___/|___/ " + style.RESET)
|
||||||
|
|
||||||
|
# get a session after logging into QRZ with user/pass
|
||||||
|
get_session()
|
||||||
|
|
||||||
# Begin the main loop
|
# Begin the main loop
|
||||||
while keeponlogging:
|
while keeponlogging:
|
||||||
# query a call sign from the user
|
# query a call sign from the user
|
||||||
|
@ -381,7 +427,7 @@ if __name__ == '__main__':
|
||||||
# pull all previous QSOs from tzhe QRZ logbook
|
# pull all previous QSOs from tzhe QRZ logbook
|
||||||
result = getQSOsForCallsign(call)
|
result = getQSOsForCallsign(call)
|
||||||
# ignore this part if there were no previous QSOs
|
# ignore this part if there were no previous QSOs
|
||||||
if result[0]:
|
if result and result[0]:
|
||||||
print ('%s%sPrevious QSOs with %s%s' % (style.UNDERLINED, hlcol, call, style.RESET))
|
print ('%s%sPrevious QSOs with %s%s' % (style.UNDERLINED, hlcol, call, style.RESET))
|
||||||
# generate a nice ascii table with the result
|
# generate a nice ascii table with the result
|
||||||
tab = getQSOTable(result)
|
tab = getQSOTable(result)
|
||||||
|
|
Loading…
Reference in New Issue