2021-05-20 12:05:33 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
# pylint: disable=W1401
|
|
|
|
|
|
|
|
"""
|
|
|
|
+---------------------------------------------------------------------+
|
|
|
|
| _ |
|
|
|
|
| __ _ _ _ __| |___ __ _ __ _ ___ _ _ |
|
|
|
|
| / _` | '_|_ / / _ \/ _` / _` / -_) '_| |
|
|
|
|
| \__, |_| /__|_\___/\__, \__, \___|_| |
|
|
|
|
| |_| |___/|___/ |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| A python application to log QSOs directly to QRZ.com from the CLI |
|
|
|
|
| |
|
2022-07-29 15:54:37 -04:00
|
|
|
| Author: Michael Clemens, DK1MI (qrzlogger@qrz.is) |
|
2021-06-04 12:41:39 -04:00
|
|
|
| |
|
|
|
|
| Documentation: Please see the README.md file |
|
|
|
|
| License: Please see the LICENSE file |
|
|
|
|
| Repository: https://github.com/exitnode/qrzlogger |
|
|
|
|
| |
|
|
|
|
+---------------------------------------------------------------------+
|
|
|
|
"""
|
2021-05-20 12:05:33 -04:00
|
|
|
|
|
|
|
import urllib
|
|
|
|
import re
|
|
|
|
import datetime
|
|
|
|
import os
|
2021-06-01 18:58:01 -04:00
|
|
|
import sys
|
2021-05-20 12:05:33 -04:00
|
|
|
import configparser
|
2021-06-03 18:47:49 -04:00
|
|
|
import signal
|
|
|
|
import atexit
|
2021-06-04 12:41:39 -04:00
|
|
|
from datetime import timezone
|
|
|
|
from colored import attr, fg
|
|
|
|
from requests.structures import CaseInsensitiveDict
|
|
|
|
from prettytable import PrettyTable
|
|
|
|
import xmltodict
|
|
|
|
import requests
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-05-21 18:05:23 -04:00
|
|
|
|
2021-06-02 06:41:56 -04:00
|
|
|
class QRZLogger():
|
2021-06-04 12:41:39 -04:00
|
|
|
"""QRZLogger class"""
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
def __init__(self):
|
2021-06-04 12:41:39 -04:00
|
|
|
"""initialize things"""
|
2021-06-02 06:41:56 -04:00
|
|
|
|
2022-07-31 04:26:10 -04:00
|
|
|
self.version = "0.8.1"
|
2021-06-02 06:41:56 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
# Define the configuration object
|
|
|
|
self.config = configparser.ConfigParser()
|
|
|
|
self.config_file = os.path.expanduser('~/.qrzlogger.ini')
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
self.read_config(self.config, self.config_file)
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
if self.config and self.config['log']['log_file']:
|
|
|
|
self.log_file = self.config['log']['log_file']
|
2021-05-25 16:55:01 -04:00
|
|
|
else:
|
2021-06-02 06:41:56 -04:00
|
|
|
self.log_file = os.path.expanduser('~/.qrzlogger.log')
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
# QRZ.com URLs
|
|
|
|
self.xml_url = "https://xmldata.QRZ.com/xml/current/"
|
|
|
|
self.api_url = "https://logbook.qrz.com/api"
|
|
|
|
|
|
|
|
# headers for all POST requests
|
|
|
|
self.headers = CaseInsensitiveDict()
|
|
|
|
self.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
|
|
|
|
# Default colors
|
2021-06-04 12:41:39 -04:00
|
|
|
self.inputcol = attr('reset')
|
|
|
|
self.hlcol = attr('reset')
|
|
|
|
self.defvalcol = attr('reset')
|
|
|
|
self.errorcol = attr('reset')
|
|
|
|
self.successcol = attr('reset')
|
|
|
|
self.tablecol = attr('reset')
|
|
|
|
self.logocol = attr('reset')
|
|
|
|
|
|
|
|
self.qso = None
|
2021-06-07 13:45:45 -04:00
|
|
|
self.recent_qso_limit = 5
|
2021-06-17 07:11:55 -04:00
|
|
|
self.recent_qsos = []
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
# read colors from config and overwrite default vaulues
|
2021-06-04 12:41:39 -04:00
|
|
|
self.config_colors()
|
2021-06-01 10:17:31 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def print_banner(self):
|
|
|
|
"""print an awesome banner"""
|
|
|
|
ver = self.version
|
|
|
|
# print the banner
|
2021-06-02 06:41:56 -04:00
|
|
|
print(self.logocol)
|
|
|
|
print(" _ ")
|
|
|
|
print(" __ _ _ _ __| |___ __ _ __ _ ___ _ _ ")
|
|
|
|
print(" / _` | '_|_ / / _ \/ _` / _` / -_) '_|")
|
|
|
|
print(" \__, |_| /__|_\___/\__, \__, \___|_| ")
|
2022-07-29 15:54:37 -04:00
|
|
|
print(" |_| -=DK1MI=- |___/|___/ v"+ver+" ")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-02 06:41:56 -04:00
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def config_colors(self):
|
|
|
|
"""Read color settings from config file"""
|
2021-06-01 10:17:31 -04:00
|
|
|
if self.config and self.config['colors']['use_colors'] == "yes":
|
2021-06-04 12:41:39 -04:00
|
|
|
self.inputcol = fg(self.config['colors']['inputcol'])
|
|
|
|
self.hlcol = fg(self.config['colors']['hlcol'])
|
|
|
|
self.defvalcol = fg(self.config['colors']['defvalcol'])
|
|
|
|
self.errorcol = fg(self.config['colors']['errorcol'])
|
|
|
|
self.successcol = fg(self.config['colors']['successcol'])
|
|
|
|
self.tablecol = fg(self.config['colors']['tablecol'])
|
|
|
|
self.logocol = fg(self.config['colors']['logocol'])
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def read_config(config, file_name):
|
|
|
|
"""reads the configuration from the config file or
|
|
|
|
creates a default config file if none could be found"""
|
2021-06-01 10:17:31 -04:00
|
|
|
if os.path.isfile(file_name):
|
|
|
|
config.read(file_name)
|
|
|
|
else:
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config['qrz.com'] = {
|
2021-06-02 03:33:25 -04:00
|
|
|
'station_call': 'MYCALL',
|
|
|
|
'api_key': '1234-ABCD-1234-A1B2',
|
|
|
|
'qrz_user': 'MYCALL',
|
|
|
|
'qrz_pass': 'my_secret_password',
|
2021-06-04 12:41:39 -04:00
|
|
|
'xml_fields': '("call", "band", "mode", "qso_date",\
|
|
|
|
"time_on", "rst_sent", "rst_rcvd", "comment")'}
|
2021-06-01 10:17:31 -04:00
|
|
|
config['log'] = {
|
|
|
|
'log_file': '/tmp/qrzlogger.log'}
|
|
|
|
config['qso_defaults'] = {
|
|
|
|
'band': '40m',
|
|
|
|
'mode': 'SSB',
|
|
|
|
'rst_rcvd': '59',
|
|
|
|
'rst_sent': '59',
|
|
|
|
'tx_pwr': '5'}
|
|
|
|
config['colors'] = {
|
|
|
|
'use_colors': 'yes',
|
2021-06-04 12:41:39 -04:00
|
|
|
'inputcol': 'yellow',
|
|
|
|
'hlcol': 'yellow',
|
|
|
|
'defvalcol': 'light_blue',
|
|
|
|
'errorcol': 'red',
|
|
|
|
'successcol': 'green',
|
|
|
|
'tablecol': 'light_blue',
|
|
|
|
'logocol': 'yellow'}
|
2021-06-01 10:17:31 -04:00
|
|
|
config['bandfreqs'] = {
|
|
|
|
'160m': '1.850',
|
|
|
|
'80m': '3.700',
|
|
|
|
'60m': '5.355',
|
|
|
|
'40m': '7.100',
|
|
|
|
'30m': '10.130',
|
|
|
|
'20m': '14.200',
|
|
|
|
'17m': '18.130',
|
|
|
|
'15m': '21.200',
|
|
|
|
'12m': '24.950',
|
|
|
|
'10m': '28.500',
|
|
|
|
'6m': '50.150',
|
|
|
|
'2m': '145.500',
|
2022-03-17 16:15:29 -04:00
|
|
|
'70cm': '432.300' }
|
2021-06-04 12:41:39 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
with open(file_name, 'w') as configfile:
|
2021-06-04 12:41:39 -04:00
|
|
|
config.write(configfile)
|
2021-06-01 10:35:36 -04:00
|
|
|
print("\nNo configuration file found. A new configuration file has been created.")
|
2021-06-01 10:17:31 -04:00
|
|
|
print("\nPlease edit the file " + file_name + " and restart the application.\n" )
|
2021-06-04 12:41:39 -04:00
|
|
|
sys.exit()
|
2021-06-01 10:17:31 -04:00
|
|
|
return config
|
2021-05-25 16:55:01 -04:00
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
@staticmethod
|
|
|
|
def remove_indicators(call):
|
|
|
|
"""returns the actual call sign without any indicators
|
|
|
|
(e.g, "/p" or "F/")"""
|
|
|
|
|
|
|
|
# set the return value to the value of "call"
|
2021-06-03 18:47:49 -04:00
|
|
|
cleaned_call = call
|
2021-06-04 12:41:39 -04:00
|
|
|
# check if the callsign has a suffix (.e.g. /p)
|
2021-06-03 18:47:49 -04:00
|
|
|
if call.endswith(("/P","/M","/QRP")):
|
|
|
|
cleaned_call = re.sub(r'/\w$', "", call)
|
2021-06-04 12:41:39 -04:00
|
|
|
# check if the callsign has a prefix (e.g. DL/)
|
2021-06-03 18:47:49 -04:00
|
|
|
if "/" in cleaned_call:
|
|
|
|
cleaned_call = re.sub(r'^\w+/', "", cleaned_call)
|
|
|
|
return cleaned_call
|
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def print_table(self, tab):
|
|
|
|
"""Print the table object to stdout"""
|
2021-06-03 18:47:49 -04:00
|
|
|
print(self.tablecol)
|
|
|
|
print(tab)
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-03 18:47:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
#####################################################
|
|
|
|
# QRZ.com API Functions #
|
|
|
|
#####################################################
|
2021-05-25 16:55:01 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
def get_session(self):
|
2021-06-04 12:41:39 -04:00
|
|
|
"""Generate a session for QRZ.com's xml service with
|
|
|
|
the help of the QRZ.com username and password"""
|
2021-06-01 10:17:31 -04:00
|
|
|
session_key = None
|
2021-06-04 12:41:39 -04:00
|
|
|
data = {
|
2021-06-01 10:17:31 -04:00
|
|
|
'username' : self.config['qrz.com']['qrz_user'],
|
|
|
|
'password' : self.config['qrz.com']['qrz_pass']
|
|
|
|
}
|
2021-05-25 16:55:01 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
try:
|
|
|
|
session = requests.Session()
|
2021-06-04 12:41:39 -04:00
|
|
|
session.verify = True
|
|
|
|
result = session.post(self.xml_url, data=data)
|
|
|
|
if result.status_code == 200:
|
|
|
|
raw_session = xmltodict.parse(result.content)
|
2021-06-01 10:17:31 -04:00
|
|
|
if raw_session.get('QRZDatabase').get('Session').get('Error'):
|
|
|
|
print(self.errorcol + "\nError while logging into the QRZ.com XML Service:\n")
|
|
|
|
print(raw_session.get('QRZDatabase').get('Session').get('Error'))
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
session_key = raw_session.get('QRZDatabase').get('Session').get('Key')
|
|
|
|
if session_key:
|
|
|
|
return session_key
|
|
|
|
except requests.exceptions.ConnectionError as e_conn:
|
|
|
|
print(self.errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
|
|
|
print(e_conn)
|
2021-06-04 12:41:39 -04:00
|
|
|
print("\nPlease check if\n * username and password are correct \
|
|
|
|
(see config.ini)\n * you are connected to the internet")
|
|
|
|
print(attr('reset'))
|
|
|
|
except: # pylint: disable=bare-except
|
2021-06-01 10:17:31 -04:00
|
|
|
print(self.errorcol + "\nsomething unexpected has happened:\n")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
return session_key
|
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def send_request(self, post_data):
|
|
|
|
"""Sends a POST request to QRZ.com, checks for errors
|
|
|
|
and returns the response"""
|
2021-06-01 10:17:31 -04:00
|
|
|
try:
|
|
|
|
resp = requests.post(self.api_url, headers=self.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]:
|
2021-06-04 12:41:39 -04:00
|
|
|
print(self.errorcol + "\nThe API key configured \
|
|
|
|
in config.ini is not correct.\n" + attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
else:
|
|
|
|
return response
|
|
|
|
elif resp.status_code == 404:
|
2021-06-04 12:41:39 -04:00
|
|
|
print(self.errorcol + "\nThe API URL could not be found. \
|
|
|
|
Please check the URL in config.ini\n" + attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
except requests.exceptions.ConnectionError as e_conn:
|
|
|
|
print(self.errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
|
|
|
print(e_conn)
|
|
|
|
print("\nPlease check if you are connected to the internet")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
|
|
|
except: # pylint: disable=bare-except
|
2021-06-01 10:17:31 -04:00
|
|
|
print(self.errorcol + "\nsomething unexpected has happened:\n")
|
|
|
|
print(e_conn)
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
return None
|
2021-05-20 12:05:33 -04:00
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def get_call_data(self, call, session_key):
|
|
|
|
"""Query QRZ.com's xml api to gather information
|
|
|
|
about a specific call sign"""
|
2021-05-25 16:55:01 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
data = {
|
2021-06-01 10:17:31 -04:00
|
|
|
's' : session_key,
|
|
|
|
'callsign' : call
|
|
|
|
}
|
2021-05-25 16:55:01 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
try:
|
|
|
|
session = requests.Session()
|
2021-06-04 12:41:39 -04:00
|
|
|
session.verify = True
|
|
|
|
result = session.post(self.xml_url, data=data)
|
|
|
|
raw = xmltodict.parse(result.content).get('QRZDatabase')
|
2021-06-01 10:17:31 -04:00
|
|
|
calldata = raw.get('Callsign')
|
|
|
|
if calldata:
|
|
|
|
return calldata
|
|
|
|
except requests.exceptions.ConnectionError as e_conn:
|
|
|
|
print(self.errorcol + "\nUnable to connect to xmldata.qrz.com:")
|
|
|
|
print(e_conn)
|
|
|
|
print("\nPlease check if you are connected to the internet")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
|
|
|
except: # pylint: disable=bare-except
|
2021-06-01 10:17:31 -04:00
|
|
|
print(self.errorcol + "\nsomething unexpected has happened:\n")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def get_qsos(self, option):
|
|
|
|
"""Query QRZ.com's logbook for all previous QSOs
|
|
|
|
with a specific call sign or for a specific logid"""
|
|
|
|
|
|
|
|
result = [{}]
|
|
|
|
post_data = {
|
2021-06-01 10:17:31 -04:00
|
|
|
'KEY' : self.config['qrz.com']['api_key'],
|
|
|
|
'ACTION' : 'FETCH',
|
|
|
|
'OPTION' : "TYPE:ADIF," + option
|
2021-05-20 12:05:33 -04:00
|
|
|
}
|
2021-06-01 10:17:31 -04:00
|
|
|
post_data = urllib.parse.urlencode(post_data)
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
response = self.send_request(post_data)
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
if response:
|
|
|
|
resp_list = response.splitlines()
|
2021-06-04 12:41:39 -04:00
|
|
|
for resp in resp_list:
|
|
|
|
if not resp:
|
2021-06-01 10:17:31 -04:00
|
|
|
result.append({})
|
|
|
|
else:
|
2021-06-04 12:41:39 -04:00
|
|
|
if any(s+":" in resp for s in self.config['qrz.com']['xml_fields']):
|
|
|
|
resp = re.sub('<','',resp, flags=re.DOTALL)
|
|
|
|
resp = re.sub(':.*>',":",resp, flags=re.DOTALL)
|
|
|
|
value = re.sub('^.*:',"",resp, flags=re.DOTALL)
|
|
|
|
key = re.sub(':.*$',"",resp, flags=re.DOTALL)
|
|
|
|
result[-1][key] = value
|
|
|
|
return result
|
|
|
|
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def send_qso(self, qso, call):
|
|
|
|
"""Sends the previously collected QSO information as a new
|
|
|
|
QRZ.com logbook entry via the API"""
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
logid = "null"
|
|
|
|
log_status = "FAILED: "
|
|
|
|
|
|
|
|
# construct ADIF QSO entry
|
2021-06-04 12:41:39 -04:00
|
|
|
adif = '<station_callsign:' + str(len(self.config['qrz.com']['station_call'])) \
|
|
|
|
+ '>' + self.config['qrz.com']['station_call']
|
2021-06-01 10:17:31 -04:00
|
|
|
adif += '<call:' + str(len(call)) + '>' + call
|
|
|
|
for field in qso:
|
|
|
|
adif += '<' + field + ':' + str(len(qso[field][1])) + '>' + qso[field][1]
|
|
|
|
adif += '<eor>'
|
|
|
|
|
|
|
|
# construct POST data
|
2021-06-04 12:41:39 -04:00
|
|
|
post_data = { 'KEY' : self.config['qrz.com']['api_key'], \
|
|
|
|
'ACTION' : 'INSERT', 'ADIF' : adif }
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
# URL encode the payload
|
|
|
|
data = urllib.parse.urlencode(post_data)
|
|
|
|
# send the POST request to QRZ.com
|
2021-06-04 12:41:39 -04:00
|
|
|
response = self.send_request(data)
|
2021-08-09 10:55:52 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
# Check if the upload failed and print out
|
|
|
|
# the reason plus some additional info
|
|
|
|
if response:
|
|
|
|
if "STATUS=FAIL" in response:
|
|
|
|
print(self.errorcol)
|
|
|
|
print("QSO upload failed. QRZ.com has send the following reason:\n")
|
|
|
|
resp_list = response.split("&")
|
|
|
|
for item in resp_list:
|
|
|
|
print(item)
|
|
|
|
print("\nPlease review the following request that led to this error:\n")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
print(post_data)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
logid = re.search('LOGID=(\d+)', response).group(1)
|
2021-07-12 08:47:49 -04:00
|
|
|
print(self.successcol)
|
|
|
|
print("QSO successfully uploaded to QRZ.com (LOGID "+ logid + ")")
|
|
|
|
log_status = "SUCCESS: "
|
|
|
|
print(attr('reset'))
|
2021-06-04 12:41:39 -04:00
|
|
|
except: # pylint: disable=bare-except
|
2021-06-01 10:17:31 -04:00
|
|
|
logid = "null"
|
2021-07-12 08:47:49 -04:00
|
|
|
print(self.errorcol)
|
|
|
|
print("\nQSO upload to QRZ.com failed!\n")
|
2021-06-04 12:41:39 -04:00
|
|
|
print(attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
with open(self.log_file, "a") as log:
|
|
|
|
log.write(log_status + adif + "\n")
|
2021-06-04 12:41:39 -04:00
|
|
|
return logid
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#####################################################
|
|
|
|
# Functions for generating ASCII Tables #
|
|
|
|
#####################################################
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
@staticmethod
|
|
|
|
def get_qso_table(result):
|
|
|
|
"""Generate a pretty ascii table containing all
|
|
|
|
previous QSOs with a specific call sign"""
|
|
|
|
|
2022-03-06 00:48:55 -05:00
|
|
|
table = PrettyTable(['Date', 'Time', 'Band', 'Mode', 'RST-S', 'RST-R', 'Power', 'QRZ', 'LotW', 'Comment'])
|
2021-06-01 10:17:31 -04:00
|
|
|
for qso in result:
|
|
|
|
if "qso_date" in qso:
|
|
|
|
date = datetime.datetime.strptime(qso["qso_date"], '%Y%m%d').strftime('%Y/%m/%d')
|
|
|
|
time = datetime.datetime.strptime(qso["time_on"], '%H%M').strftime('%H:%M')
|
|
|
|
# add missing fields to dict
|
2022-03-06 00:48:55 -05:00
|
|
|
for field in ["band", "mode", "rst_sent", "rst_rcvd", "tx_pwr", "app_qrzlog_status", "lotw_qsl_rcvd", "comment"]:
|
2021-06-01 10:17:31 -04:00
|
|
|
if field not in qso:
|
|
|
|
qso[field] = ""
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row([date, time, qso["band"], qso["mode"], qso["rst_sent"], \
|
2022-03-06 00:48:55 -05:00
|
|
|
qso["rst_rcvd"], qso["tx_pwr"], qso["app_qrzlog_status"], qso["lotw_qsl_rcvd"], qso["comment"]])
|
2021-06-04 12:41:39 -04:00
|
|
|
table.align = "r"
|
|
|
|
return table
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
@staticmethod
|
|
|
|
def get_xml_query_table(result):
|
|
|
|
"""Print a pretty ascii table containing all interesting
|
|
|
|
data found for a specific call sign"""
|
|
|
|
|
|
|
|
table = PrettyTable(['key', 'value'])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "fname" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["First Name", result["fname"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "name" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["Last Name", result["name"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "addr1" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["Street", result["addr1"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "addr2" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["City", result["addr2"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "state" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["State", result["state"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "country" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["Country", result["country"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "grid" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["Locator", result["grid"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "email" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["Email", result["email"]])
|
2021-06-01 10:17:31 -04:00
|
|
|
if "qslmgr" in result:
|
2021-06-04 12:41:39 -04:00
|
|
|
table.add_row(["QSL via:", result["qslmgr"]])
|
|
|
|
table.align = "l"
|
|
|
|
table.header = False
|
|
|
|
return table
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
@staticmethod
|
|
|
|
def get_qso_detail_table(qso):
|
|
|
|
"""Print a pretty ascii table containing all
|
|
|
|
previously entered user data"""
|
2021-06-01 10:17:31 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
table = PrettyTable(['key', 'value'])
|
|
|
|
for item in qso:
|
|
|
|
table.add_row([qso[item][0], qso[item][1]])
|
|
|
|
table.align = "l"
|
|
|
|
table.header = False
|
|
|
|
return table
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
2021-06-07 13:45:45 -04:00
|
|
|
@staticmethod
|
|
|
|
def get_recent_qso_table(recent_qsos):
|
|
|
|
"""Print a pretty ascii table containing
|
|
|
|
the n previous QSOs"""
|
|
|
|
|
|
|
|
table = PrettyTable(['Time', 'Frequency', 'Call'])
|
|
|
|
for hist_qso in recent_qsos:
|
|
|
|
table.add_row([hist_qso[1], hist_qso[2], hist_qso[0]])
|
|
|
|
|
|
|
|
table.align = "l"
|
|
|
|
table.header = True
|
|
|
|
return table
|
|
|
|
|
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
#####################################################
|
|
|
|
# User Interaction Functions #
|
|
|
|
#####################################################
|
|
|
|
|
2022-03-17 16:15:29 -04:00
|
|
|
def get_band_from_freq(self, freq):
|
|
|
|
bandfreq = freq.split('.',1)[0]
|
|
|
|
if bandfreq == "1":
|
|
|
|
return "160m"
|
|
|
|
elif bandfreq == "3":
|
|
|
|
return "80m"
|
|
|
|
elif bandfreq == "5":
|
|
|
|
return "60m"
|
|
|
|
elif bandfreq == "7":
|
|
|
|
return "40m"
|
|
|
|
elif bandfreq == "10":
|
|
|
|
return "30m"
|
|
|
|
elif bandfreq == "14":
|
|
|
|
return "20m"
|
|
|
|
elif bandfreq == "18":
|
|
|
|
return "17m"
|
|
|
|
elif bandfreq == "21":
|
|
|
|
return "15m"
|
|
|
|
elif bandfreq == "24":
|
|
|
|
return "12m"
|
|
|
|
elif bandfreq == "28":
|
|
|
|
return "10m"
|
|
|
|
elif bandfreq == "29":
|
|
|
|
return "10m"
|
|
|
|
elif bandfreq == "50":
|
|
|
|
return "6m"
|
|
|
|
|
2022-07-31 04:11:05 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def query_qso_data(self, qso):
|
|
|
|
"""Queries QSO specific data from the user via
|
|
|
|
the command line"""
|
|
|
|
|
|
|
|
date_time = datetime.datetime.now(timezone.utc)
|
|
|
|
dt_now = date_time.replace(tzinfo=timezone.utc)
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
# If this is the first try filling out the QSO fields
|
|
|
|
# then we use defaults
|
|
|
|
if qso is None:
|
|
|
|
questions = {
|
2022-07-31 04:26:10 -04:00
|
|
|
"band": ["Band", self.config['qso_defaults']['band']],
|
|
|
|
"comment": ["Comment", ""],
|
2021-06-04 12:41:39 -04:00
|
|
|
"qso_date" : ["QSO Date", dt_now.strftime("%Y%m%d")],
|
|
|
|
"time_on": ["QSO Time", dt_now.strftime("%H%M")],
|
|
|
|
"freq": ["Frequency", ""],
|
|
|
|
"mode": ["Mode", self.config['qso_defaults']['mode']],
|
|
|
|
"rst_rcvd": ["RST Received", self.config['qso_defaults']['rst_rcvd']],
|
|
|
|
"rst_sent": ["RST Sent", self.config['qso_defaults']['rst_sent']],
|
2022-07-31 04:26:10 -04:00
|
|
|
"tx_pwr": ["Power (in W)", self.config['qso_defaults']['tx_pwr']]
|
2021-06-01 10:17:31 -04:00
|
|
|
}
|
|
|
|
# if this is not the first try, we pre-fill the
|
|
|
|
# vaulues we got from the last try
|
|
|
|
else:
|
|
|
|
questions = qso
|
|
|
|
|
|
|
|
# We now loop through all defined fields and ask
|
|
|
|
# the user for input
|
2021-06-04 12:41:39 -04:00
|
|
|
for question in questions:
|
|
|
|
txt = self.inputcol + questions[question][0] + " [" + self.defvalcol \
|
|
|
|
+ questions[question][1] + self.inputcol + "]:" + attr('reset')
|
2021-06-01 10:17:31 -04:00
|
|
|
inp = input(txt)
|
|
|
|
# If the user just hits enter, we keep the default value.
|
|
|
|
# If not, we keep the data provided by the user
|
|
|
|
if inp == "c":
|
|
|
|
return None
|
2022-07-31 04:26:10 -04:00
|
|
|
if inp == "d":
|
|
|
|
return questions
|
2021-06-03 13:53:07 -04:00
|
|
|
if inp == "quit":
|
|
|
|
sys.exit()
|
2021-06-01 10:17:31 -04:00
|
|
|
if inp != "":
|
2021-06-04 12:41:39 -04:00
|
|
|
questions[question][1] = inp
|
2021-06-01 10:17:31 -04:00
|
|
|
# check if we are asking for the band
|
2022-07-31 04:11:05 -04:00
|
|
|
if question == "band":
|
2021-06-01 10:17:31 -04:00
|
|
|
# check if the band is in the bandfreqs dictionary
|
|
|
|
try:
|
|
|
|
# populate the frequency with a common freq of this band
|
|
|
|
bandfreqs = dict(self.config.items('bandfreqs'))
|
2021-06-04 12:41:39 -04:00
|
|
|
questions['freq'][1] = bandfreqs[questions[question][1]]
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
print(self.errorcol + "\nUnable to read default frequency \
|
|
|
|
values from config file." + attr('reset'))
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
return questions
|
|
|
|
|
2022-07-31 04:11:05 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def get_input_callsign(self):
|
|
|
|
"""query a call sign from the user"""
|
|
|
|
call = ""
|
|
|
|
while True:
|
|
|
|
call = input("\n\n%sEnter Callsign:%s " % (self.inputcol, attr('reset')))
|
|
|
|
if call == "quit":
|
|
|
|
sys.exit()
|
|
|
|
# check if it has the format of a valid call sign
|
|
|
|
# (at least 3 characters, only alphanumeric and slashes)
|
|
|
|
if not (len(call) > 2 and call.replace("/", "").isalnum()):
|
|
|
|
print(self.errorcol + "\nPlease enter a callsign with\n * at least \
|
|
|
|
3 characters\n * only letters, numbers and slashes" + attr('reset'))
|
|
|
|
continue
|
|
|
|
# make the call sign all upper case
|
|
|
|
call = call.upper()
|
|
|
|
break
|
|
|
|
return call
|
|
|
|
|
|
|
|
|
|
|
|
def confirm_and_submit_qso(self, call):
|
|
|
|
"""ask user if everything is ok. If not, start over."""
|
|
|
|
done = False
|
|
|
|
while True:
|
|
|
|
answer = input("\n" + self.inputcol + "Is this correct? [" + \
|
|
|
|
self.defvalcol + "y/n/c/quit" + self.inputcol + "]: " + attr('reset'))
|
|
|
|
answer = answer.upper()
|
|
|
|
if answer == "Y":
|
2021-08-09 10:55:52 -04:00
|
|
|
while True:
|
|
|
|
logid = self.send_qso(self.qso, call)
|
|
|
|
if logid and logid != "null":
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
answer = input("\n" + self.inputcol + "QSO Upload failed. Retry? [" + \
|
|
|
|
self.defvalcol + "y/n" + self.inputcol + "]: " + attr('reset'))
|
|
|
|
answer = answer.upper()
|
|
|
|
if answer == "N":
|
|
|
|
done = True
|
|
|
|
break
|
2021-07-12 08:47:49 -04:00
|
|
|
if logid and logid.lower() != "null":
|
2021-06-04 12:41:39 -04:00
|
|
|
# pull the uploaded QSO from QRZ
|
|
|
|
result = self.get_qsos("LOGIDS:"+ logid)
|
|
|
|
if result and result[0]:
|
|
|
|
self.print_table(self.get_qso_table(result))
|
2021-06-17 07:11:55 -04:00
|
|
|
# add some of the QSO detail to the recent_qsos list
|
|
|
|
self.recent_qsos.append([call, self.qso["time_on"][1], self.qso["freq"][1]])
|
|
|
|
if len(self.recent_qsos)>self.recent_qso_limit:
|
|
|
|
self.recent_qsos.pop(0)
|
2021-06-04 12:41:39 -04:00
|
|
|
done = True
|
|
|
|
break
|
2021-08-09 10:55:52 -04:00
|
|
|
else:
|
|
|
|
break
|
2021-06-04 12:41:39 -04:00
|
|
|
elif answer == "C":
|
|
|
|
done = True
|
|
|
|
break
|
|
|
|
elif answer == "N":
|
|
|
|
break
|
|
|
|
elif answer == "QUIT":
|
|
|
|
sys.exit()
|
|
|
|
return done
|
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
|
2021-06-07 13:45:45 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
def handler(signum, frame): # pylint: disable=W0613
|
|
|
|
"""method for handlich SIGINTs"""
|
2021-06-03 18:47:49 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def quit_gracefully():
|
2021-06-04 12:41:39 -04:00
|
|
|
"""Prints a message when the application is terminated"""
|
2021-06-03 18:47:49 -04:00
|
|
|
print("\n73!\n")
|
2021-06-01 10:17:31 -04:00
|
|
|
|
|
|
|
|
2021-05-25 16:55:01 -04:00
|
|
|
|
|
|
|
#####################################################
|
|
|
|
# Main Routine #
|
|
|
|
#####################################################
|
|
|
|
|
2021-06-01 18:58:01 -04:00
|
|
|
def main():
|
2021-06-04 12:41:39 -04:00
|
|
|
"""the main routine"""
|
2021-06-01 18:58:01 -04:00
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
# signal handling for ctrl+c and ctrl+d
|
2021-06-03 18:47:49 -04:00
|
|
|
signal.signal(signal.SIGINT, handler)
|
|
|
|
atexit.register(quit_gracefully)
|
|
|
|
|
2021-06-04 12:41:39 -04:00
|
|
|
qrz = QRZLogger()
|
|
|
|
qrz.print_banner()
|
2021-05-20 12:05:33 -04:00
|
|
|
|
2021-06-01 10:17:31 -04:00
|
|
|
keeponlogging = True
|
|
|
|
session_key = None
|
2021-05-24 08:46:28 -04:00
|
|
|
|
2021-06-07 13:45:45 -04:00
|
|
|
|
2021-05-21 17:36:05 -04:00
|
|
|
# Begin the main loop
|
2021-05-21 04:47:02 -04:00
|
|
|
while keeponlogging:
|
2021-06-03 18:47:49 -04:00
|
|
|
# get a session after logging into QRZ with user/pass
|
2021-06-04 12:41:39 -04:00
|
|
|
session_key = qrz.get_session()
|
2021-06-07 13:45:45 -04:00
|
|
|
qrz.qso = None
|
|
|
|
# print a table containing the last n logged QSOs
|
2021-06-17 07:11:55 -04:00
|
|
|
if qrz.recent_qsos:
|
2021-06-07 13:45:45 -04:00
|
|
|
print ('\n%s%sYour last %s logged QSOs%s' \
|
|
|
|
% (attr('underlined'), qrz.hlcol, \
|
|
|
|
qrz.recent_qso_limit, attr('reset')))
|
2021-06-17 07:11:55 -04:00
|
|
|
qrz.print_table(qrz.get_recent_qso_table(qrz.recent_qsos))
|
2021-06-03 18:47:49 -04:00
|
|
|
# query a call sign from the user
|
2021-06-04 12:41:39 -04:00
|
|
|
call = qrz.get_input_callsign()
|
2021-06-03 18:47:49 -04:00
|
|
|
# query call sign data from QRZ
|
2021-06-04 12:41:39 -04:00
|
|
|
result = qrz.get_call_data(call, session_key)
|
2021-06-03 18:47:49 -04:00
|
|
|
# the query was successful
|
|
|
|
if result:
|
2021-06-04 12:41:39 -04:00
|
|
|
print ('\n%s%sQRZ.com results for %s%s' \
|
|
|
|
% (attr('underlined'), qrz.hlcol, call, attr('reset')))
|
2021-06-03 18:47:49 -04:00
|
|
|
# generate a nice ascii table with the result
|
2021-06-04 12:41:39 -04:00
|
|
|
qrz.print_table(qrz.get_xml_query_table(result))
|
2021-06-03 18:47:49 -04:00
|
|
|
# the query was unsuccessful
|
|
|
|
else:
|
2021-06-04 12:41:39 -04:00
|
|
|
print ('\n%s%s has no record on QRZ.com ¯\_(ツ)_/¯%s' \
|
|
|
|
% (qrz.errorcol, call, attr('reset')))
|
|
|
|
cleaned_call = qrz.remove_indicators(call)
|
2021-06-03 18:47:49 -04:00
|
|
|
if call != cleaned_call:
|
2021-06-03 13:53:07 -04:00
|
|
|
# query call sign data from QRZ
|
2021-06-04 12:41:39 -04:00
|
|
|
result = qrz.get_call_data(cleaned_call, session_key)
|
2021-06-03 13:53:07 -04:00
|
|
|
# the query was successful
|
|
|
|
if result:
|
2021-06-04 12:41:39 -04:00
|
|
|
print ('\n%s%sShowing results for %s instead%s' \
|
|
|
|
% (attr('underlined'), qrz.hlcol, cleaned_call, attr('reset')))
|
2021-05-24 06:54:55 -04:00
|
|
|
# generate a nice ascii table with the result
|
2021-06-04 12:41:39 -04:00
|
|
|
qrz.print_table(qrz.get_xml_query_table(result))
|
2021-06-03 18:47:49 -04:00
|
|
|
print("")
|
|
|
|
# pull all previous QSOs from tzhe QRZ logbook
|
2021-06-04 12:41:39 -04:00
|
|
|
result = qrz.get_qsos("CALL:"+ call)
|
2021-06-03 18:47:49 -04:00
|
|
|
# ignore this part if there were no previous QSOs
|
|
|
|
if result and result[0]:
|
2021-06-04 12:41:39 -04:00
|
|
|
print ('%s%sPrevious QSOs with %s%s' \
|
|
|
|
% (attr('underlined'), qrz.hlcol, call, attr('reset')))
|
|
|
|
qrz.print_table(qrz.get_qso_table(result))
|
2021-06-03 18:47:49 -04:00
|
|
|
|
2022-07-31 04:26:10 -04:00
|
|
|
print ('%s%sEnter new QSO details below%s%s (enter \'c\' to cancel, \'d\' to fast forward)%s\n' \
|
2021-06-04 12:41:39 -04:00
|
|
|
% (attr('underlined'), qrz.hlcol, attr('reset'), qrz.hlcol, attr('reset'),))
|
2021-06-03 18:47:49 -04:00
|
|
|
|
|
|
|
done = False
|
|
|
|
|
|
|
|
# we now ask the user for QSO details until he/she is happy with the result
|
|
|
|
while not done:
|
|
|
|
# query QSO details from the user
|
2021-06-04 12:41:39 -04:00
|
|
|
qrz.qso = qrz.query_qso_data(qrz.qso)
|
2021-06-03 18:47:49 -04:00
|
|
|
# the user has answered all questions
|
2021-06-04 12:41:39 -04:00
|
|
|
if qrz.qso:
|
|
|
|
print ('\n%s%sPlease review your choices%s' \
|
|
|
|
% (attr('underlined'), qrz.hlcol, attr('reset')))
|
|
|
|
qrz.print_table(qrz.get_qso_detail_table(qrz.qso))
|
|
|
|
done = qrz.confirm_and_submit_qso(call)
|
2021-06-17 07:11:55 -04:00
|
|
|
'''
|
2021-06-08 16:18:57 -04:00
|
|
|
# add some of the QSO detail to the recent_qsos list
|
|
|
|
recent_qsos.append([call, qrz.qso["time_on"][1], qrz.qso["freq"][1]])
|
|
|
|
if len(recent_qsos)>qrz.recent_qso_limit:
|
|
|
|
recent_qsos.pop(0)
|
2021-06-17 07:11:55 -04:00
|
|
|
'''
|
2021-06-03 18:47:49 -04:00
|
|
|
# the user has entered 'c' during the QSO detail entering process
|
|
|
|
else:
|
|
|
|
done = True
|
2021-06-04 12:41:39 -04:00
|
|
|
qrz.qso = None
|
2021-06-03 18:47:49 -04:00
|
|
|
continue
|
2021-06-01 18:58:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-03 18:47:49 -04:00
|
|
|
try:
|
|
|
|
sys.exit(main())
|
|
|
|
except EOFError:
|
|
|
|
pass
|