2022-05-19 08:15:19 -04:00
|
|
|
#!/usr/bin/env python3
|
2022-05-19 09:59:26 -04:00
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# pylint: disable=W1401,C0303
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
+---------------------------------------------------------------------+
|
|
|
|
| |
|
|
|
|
| ___ _ ___ _ |
|
|
|
|
| / __|___| |___ _ _/ __|_ __ ___| |_ |
|
|
|
|
| | (__/ _ \ / _ \ '_\__ \ '_ \/ _ \ _| |
|
|
|
|
| \___\___/_\___/_| |___/ .__/\___/\__| |
|
|
|
|
| -= DK1MI =- |_| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| A colorful cli DX cluster client with an LotW integration |
|
|
|
|
| |
|
|
|
|
| Author: Michael Clemens, DK1MI (colorspot@qrz.is) |
|
|
|
|
| |
|
|
|
|
| Documentation: Please see the README.md file |
|
|
|
|
| License: Please see the LICENSE file |
|
|
|
|
| Repository: https://git.qrz.is/clemens/colorspot |
|
|
|
|
| |
|
|
|
|
+---------------------------------------------------------------------+
|
|
|
|
"""
|
2022-05-19 09:59:26 -04:00
|
|
|
|
|
|
|
# Infos on colors: https://pypi.org/project/colored/
|
|
|
|
|
2022-05-18 17:39:02 -04:00
|
|
|
import sys
|
|
|
|
import csv
|
|
|
|
import re
|
2022-05-19 09:59:26 -04:00
|
|
|
import random
|
2022-05-20 11:12:55 -04:00
|
|
|
import os
|
2022-05-20 08:45:09 -04:00
|
|
|
from os.path import exists
|
2022-05-20 11:12:55 -04:00
|
|
|
import configparser
|
2022-05-20 08:45:09 -04:00
|
|
|
import zipfile
|
2022-05-20 11:12:55 -04:00
|
|
|
from telnetlib import Telnet
|
|
|
|
import requests
|
|
|
|
from colored import fg, bg, attr
|
2022-05-18 17:39:02 -04:00
|
|
|
|
|
|
|
class ColorSpot():
|
2022-05-20 11:12:55 -04:00
|
|
|
"""ColorSpot class"""
|
2022-05-18 17:39:02 -04:00
|
|
|
|
|
|
|
def __init__(self):
|
2022-05-20 11:12:55 -04:00
|
|
|
"""initialize things"""
|
|
|
|
|
2022-05-18 17:39:02 -04:00
|
|
|
self.version = "0.1.0"
|
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
self.print_banner()
|
|
|
|
|
2022-05-18 17:39:02 -04:00
|
|
|
self.config = configparser.ConfigParser()
|
|
|
|
self.config_file = os.path.expanduser('~/.colorspot.ini')
|
|
|
|
self.read_config(self.config, self.config_file)
|
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
self.check_files()
|
2022-05-20 09:08:00 -04:00
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
if self.check_lotw_confirmed:
|
|
|
|
self.confirmed_entities = self.get_confirmed_entities()
|
2022-05-18 17:39:02 -04:00
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
if self.check_cty:
|
2022-05-20 11:12:55 -04:00
|
|
|
#self.cty = list(csv.reader(open(self.config['files']['cty'], "r"), delimiter=","))
|
|
|
|
with open(self.config['files']['cty'], encoding='us-ascii') as csvfile:
|
|
|
|
self.cty = list(csv.reader(csvfile, delimiter=','))
|
2022-05-20 08:45:09 -04:00
|
|
|
|
|
|
|
|
2022-05-19 09:59:26 -04:00
|
|
|
|
|
|
|
def print_banner(self):
|
|
|
|
"""print an awesome banner"""
|
|
|
|
print(fg(self.rnd_col())+" ___ _ ___ _ ")
|
|
|
|
print(fg(self.rnd_col())+" / __|___| |___ _ _/ __|_ __ ___| |_ ")
|
|
|
|
print(fg(self.rnd_col())+" | (__/ _ \ / _ \ '_\__ \ '_ \/ _ \ _|")
|
|
|
|
print(fg(self.rnd_col())+" \___\___/_\___/_| |___/ .__/\___/\__|")
|
|
|
|
print(fg(self.rnd_col())+" -= DK1MI =- |_| ")
|
2022-05-20 08:45:09 -04:00
|
|
|
print("")
|
2022-05-19 09:59:26 -04:00
|
|
|
print(attr('reset'))
|
2022-05-19 08:15:19 -04:00
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
|
2022-05-18 17:39:02 -04:00
|
|
|
@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"""
|
|
|
|
if os.path.isfile(file_name):
|
|
|
|
config.read(file_name)
|
|
|
|
else:
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config['cluster'] = {
|
|
|
|
'host': 'dxc.nc7j.com',
|
|
|
|
'port': '7373',
|
2022-05-19 08:15:19 -04:00
|
|
|
'user': 'N0CALL',
|
2022-05-18 17:39:02 -04:00
|
|
|
'timeout': '100'}
|
2022-05-20 08:45:09 -04:00
|
|
|
config['files'] = {
|
|
|
|
'cty': 'cty.csv',
|
2022-05-20 09:08:00 -04:00
|
|
|
'cty_url': 'https://www.country-files.com/bigcty/download/bigcty.zip',
|
2022-05-20 08:45:09 -04:00
|
|
|
'lotw_confirmed': 'lotw.adi',
|
2022-05-20 09:08:00 -04:00
|
|
|
'lotw_activity': 'lotw-user-activity.csv',
|
|
|
|
'lotw_activity_url': 'https://lotw.arrl.org/lotw-user-activity.csv'}
|
2022-05-19 11:40:17 -04:00
|
|
|
config['lotw'] = {
|
|
|
|
'user': 'N0CALL',
|
2022-05-20 11:12:55 -04:00
|
|
|
'password': 'CHANGEME',
|
2022-05-19 11:40:17 -04:00
|
|
|
'mode': 'ssb'}
|
2022-05-19 08:15:19 -04:00
|
|
|
config['band_colors'] = {
|
2022-05-18 17:39:02 -04:00
|
|
|
"145": "white",
|
|
|
|
"144": "white",
|
|
|
|
"50": "white",
|
|
|
|
"29": "yellow",
|
|
|
|
"28": "yellow",
|
|
|
|
"24": "red",
|
|
|
|
"21": "orchid",
|
|
|
|
"18": "green",
|
|
|
|
"14": "steel_blue_3",
|
|
|
|
"10": "orange_1",
|
|
|
|
"7": "cyan",
|
|
|
|
"5": "white",
|
|
|
|
"3": "light_cyan",
|
|
|
|
"1": "white",
|
|
|
|
'alert_bg': 'indian_red_1a',
|
|
|
|
'alert_fg': 'white',
|
|
|
|
'default_bg': 'black'}
|
2022-05-19 08:15:19 -04:00
|
|
|
config['cont_colors'] = {
|
2022-05-19 09:59:26 -04:00
|
|
|
"AF": "light_salmon_3b",
|
2022-05-19 08:15:19 -04:00
|
|
|
"AN": "white",
|
2022-05-19 09:59:26 -04:00
|
|
|
"AS": "orange_red_1",
|
2022-05-19 08:15:19 -04:00
|
|
|
"EU": "cyan",
|
|
|
|
"NA": "steel_blue_3",
|
|
|
|
"OC": "orchid",
|
|
|
|
"SA": "light_goldenrod_2a"}
|
|
|
|
config['colors']= {
|
|
|
|
'use_colors': 'yes',
|
|
|
|
'color_by' : 'continent',
|
|
|
|
'alert_bg': 'indian_red_1a',
|
|
|
|
'alert_fg': 'white',
|
|
|
|
'default_bg': 'black'}
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
with open(file_name, 'w', encoding='us-ascii') as configfile:
|
2022-05-18 17:39:02 -04:00
|
|
|
config.write(configfile)
|
|
|
|
print("\nNo configuration file found. A new configuration file has been created.")
|
|
|
|
print("\nPlease edit the file " + file_name + " and restart the application.\n" )
|
|
|
|
sys.exit()
|
|
|
|
return config
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def rnd_col():
|
|
|
|
"""generates a random color cod ein hex"""
|
|
|
|
rand = lambda: random.randint(0,255)
|
|
|
|
return'#%02X%02X%02X' % (rand(),rand(),rand())
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def download_file(url, local_filename):
|
|
|
|
"""downloads a file via HTTP and saves it to a defined file"""
|
|
|
|
with requests.get(url, stream=True) as request:
|
|
|
|
request.raise_for_status()
|
|
|
|
with open(local_filename, 'wb') as file:
|
|
|
|
for chunk in request.iter_content(chunk_size=8192):
|
|
|
|
file.write(chunk)
|
|
|
|
return local_filename
|
|
|
|
|
|
|
|
|
|
|
|
def check_files(self):
|
|
|
|
"""Checks if all necessary files are in the file system.
|
|
|
|
Downloads all files and unzips them (if necessary)"""
|
|
|
|
# check for lotw qsl information file
|
|
|
|
self.check_lotw_confirmed = exists(self.config['files']['lotw_confirmed'])
|
|
|
|
if not self.check_lotw_confirmed:
|
|
|
|
print("The file " + self.config['files']['lotw_confirmed'] + " is missing.")
|
|
|
|
user = self.config['lotw']['user']
|
|
|
|
password = self.config['lotw']['password']
|
|
|
|
mode = self.config['lotw']['mode']
|
|
|
|
url = "https://lotw.arrl.org/lotwuser/lotwreport.adi?login={}&password={}"\
|
|
|
|
"&qso_query=1&qso_qsl=yes&qso_mode={}&qso_qsldetail=yes&"\
|
|
|
|
"qso_qslsince=1970-01-01".format(user, password, mode)
|
|
|
|
print("Trying to download " + url)
|
|
|
|
self.download_file(url, self.config['files']['lotw_confirmed'])
|
|
|
|
self.check_lotw_confirmed = exists(self.config['files']['lotw_confirmed'])
|
|
|
|
if self.check_lotw_confirmed:
|
|
|
|
print("File successfully downloaded")
|
|
|
|
else:
|
|
|
|
print("something went wrong while downloading " + url)
|
|
|
|
|
|
|
|
# check for cty.csv file
|
|
|
|
self.check_cty = exists(self.config['files']['cty'])
|
|
|
|
if not self.check_cty:
|
|
|
|
url = self.config['files']['cty_url']
|
|
|
|
print("The file " + self.config['files']['cty'] + " is missing.")
|
|
|
|
print("Trying to download " + url)
|
|
|
|
zip_name = self.download_file(url, "bigcty.zip" )
|
|
|
|
with zipfile.ZipFile(zip_name, 'r') as zip_ref:
|
|
|
|
zip_ref.extract("cty.csv")
|
|
|
|
os.remove(zip_name)
|
|
|
|
self.check_cty = exists(self.config['files']['cty'])
|
|
|
|
if self.check_cty:
|
|
|
|
print("File successfully downloaded and extracted.")
|
|
|
|
else:
|
|
|
|
print("something went wrong while downloading " + url)
|
|
|
|
|
|
|
|
# check for lotw user activity file
|
|
|
|
self.check_lotw_activity = exists(self.config['files']['lotw_activity'])
|
|
|
|
if not self.check_lotw_activity:
|
|
|
|
url = self.config['files']['lotw_activity_url']
|
|
|
|
print("The file " + self.config['files']['lotw_activity'] + " is missing.")
|
|
|
|
print("Trying to download " + url)
|
|
|
|
self.download_file(url, self.config['files']['lotw_activity'])
|
|
|
|
self.check_lotw_activity = exists(self.config['files']['lotw_activity'])
|
|
|
|
if self.check_lotw_activity:
|
|
|
|
print("File successfully downloaded")
|
|
|
|
else:
|
|
|
|
print("something went wrong while downloading " + url)
|
|
|
|
|
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
def get_confirmed_entities(self):
|
2022-05-20 11:12:55 -04:00
|
|
|
"""Reads the file downlaoded from LotW with all confirmed QSOs,
|
|
|
|
extracts all confirmed DXCCs and puts them into a list"""
|
2022-05-18 17:39:02 -04:00
|
|
|
ret = []
|
2022-05-20 11:12:55 -04:00
|
|
|
with open(self.config['files']['lotw_confirmed'], encoding='us-ascii') as file:
|
|
|
|
for row in file:
|
|
|
|
if re.search("<DXCC:", row):
|
|
|
|
dxcc = row.partition(">")[2].lower().rstrip()
|
|
|
|
if dxcc not in ret:
|
|
|
|
ret.append(dxcc)
|
2022-05-18 17:39:02 -04:00
|
|
|
return ret
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
def check_lotw(self, call):
|
2022-05-20 11:12:55 -04:00
|
|
|
"""Reads the LotW user activity file and returns the date
|
|
|
|
of the last upload date if a specific call sign"""
|
2022-05-18 17:39:02 -04:00
|
|
|
ret = ""
|
2022-05-20 11:12:55 -04:00
|
|
|
#csv_file = csv.reader(open(self.config['files']['lotw_activity'], "r"), delimiter=",")
|
|
|
|
with open(self.config['files']['lotw_activity'], encoding='us-ascii') as csvfile:
|
|
|
|
csv_file = csv.reader(csvfile, delimiter=',')
|
|
|
|
#loop through the csv file
|
|
|
|
for row in csv_file:
|
|
|
|
if call == row[0]:
|
|
|
|
ret = row[1]
|
|
|
|
return ret
|
2022-05-18 17:39:02 -04:00
|
|
|
return ret
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
def get_cty_row(self, call):
|
2022-05-20 11:12:55 -04:00
|
|
|
"""Parses all CTY records, tries to find the DXCC entity of a
|
|
|
|
specific call sign and returns the line as a list of strings"""
|
2022-05-18 17:39:02 -04:00
|
|
|
done = False
|
|
|
|
while not done:
|
2022-05-20 08:45:09 -04:00
|
|
|
for row in self.cty:
|
|
|
|
entities = row[9].replace(";", "").replace("=", "").split(" ")
|
2022-05-20 11:12:55 -04:00
|
|
|
# TODO: Check if it is a speciall call (=) and mark it in the list
|
2022-05-20 08:45:09 -04:00
|
|
|
for prefix in entities:
|
|
|
|
if call == prefix:
|
|
|
|
return row
|
2022-05-18 17:39:02 -04:00
|
|
|
call = call[:-1]
|
2022-05-19 08:15:19 -04:00
|
|
|
if call == "":
|
2022-05-20 08:45:09 -04:00
|
|
|
return ["-", "-", "-", "-", "-", "-", "-"]
|
2022-05-18 17:39:02 -04:00
|
|
|
return None
|
|
|
|
|
2022-05-20 08:45:09 -04:00
|
|
|
|
2022-05-18 17:39:02 -04:00
|
|
|
def get_spots(self):
|
2022-05-20 11:12:55 -04:00
|
|
|
"""Connects to the specified telnet dx cluster, performs a login, grabs the
|
|
|
|
output row by row, enriches it with data and colorizes it depending on certain
|
|
|
|
paramaeters, e.g. by band or continent."""
|
2022-05-18 17:39:02 -04:00
|
|
|
with Telnet(self.config['cluster']['host'], int(self.config['cluster']['port']), \
|
|
|
|
int(self.config['cluster']['timeout'])) as telnet:
|
|
|
|
while True:
|
|
|
|
line_enc = telnet.read_until(b"\n") # Read one line
|
|
|
|
line = line_enc.decode('ascii')
|
2022-05-20 11:12:55 -04:00
|
|
|
# Enters the call sign if requested
|
2022-05-18 17:39:02 -04:00
|
|
|
if "enter your call" in line:
|
2022-05-19 11:40:17 -04:00
|
|
|
b_user = str.encode(self.config['cluster']['user']+"\n")
|
|
|
|
telnet.write(b_user)
|
2022-05-20 11:12:55 -04:00
|
|
|
# Detects the beginning of the stream and generates a header line
|
2022-05-19 11:40:17 -04:00
|
|
|
elif " Hello " in line:
|
|
|
|
print(fg("grey_27") + line + attr("reset"))
|
|
|
|
foreground = "white"
|
|
|
|
background = "grey_27"
|
|
|
|
sep = fg("grey_27")+'|'+fg(foreground)
|
|
|
|
row = ["DE", sep, "Freq", sep, "DX", \
|
|
|
|
sep, "Country", sep, "C", sep, "L", sep, "Comment", sep, "Time"]
|
|
|
|
print(bg(background) + fg(foreground) + \
|
2022-05-20 11:12:55 -04:00
|
|
|
'{:9.9} {:<1} {:>7.7} {:<1} {:<10.10} {:<1} '\
|
|
|
|
'{:<16.16} {:<1} {:<2.2} {:<1} {:<1.1} {:<1} {:<30.30} '\
|
|
|
|
'{:<1} {:<4.4}'.format(*row) + attr("reset"))
|
2022-05-19 11:40:17 -04:00
|
|
|
b_cmd = str.encode("sh/dx/50 @\n")
|
|
|
|
telnet.write(b_cmd)
|
2022-05-20 11:12:55 -04:00
|
|
|
# This is true for every line representing a spot
|
2022-05-19 11:40:17 -04:00
|
|
|
elif "DX de" in line or "Dx de" in line:
|
2022-05-18 17:39:02 -04:00
|
|
|
try:
|
2022-05-20 11:12:55 -04:00
|
|
|
# Extract all necessary fields from the line and store them
|
|
|
|
# into different variables.
|
2022-05-19 11:40:17 -04:00
|
|
|
call_de = re.search('D(X|x) de (.+?): ', line).group(2)
|
2022-05-18 17:39:02 -04:00
|
|
|
freq = re.search(': +(.+?) ', line).group(1)
|
|
|
|
call_dx = re.search(freq + ' +(.+?) ', line).group(1)
|
|
|
|
time = re.search('[^ ]*$', line).group(0)[0:4]
|
|
|
|
comment = re.search(call_dx + ' +(.+?) +' + time, line).group(1)
|
2022-05-20 08:45:09 -04:00
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# If the CTY file is available, further information will be
|
|
|
|
# gathered from it, e.g. continent, country, dxcc ID
|
2022-05-20 08:45:09 -04:00
|
|
|
if self.check_cty:
|
|
|
|
cty_details = self.get_cty_row(call_dx)
|
|
|
|
else:
|
|
|
|
cty_details = ["-","-","-","-","-","-","-","-","-","-"]
|
|
|
|
|
|
|
|
areaname = cty_details[1]
|
|
|
|
continent = cty_details[3]
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# If the LotW user activity file is available and the call
|
|
|
|
# sign in question is actually a LotW user, a checkmark is
|
|
|
|
# displayed in a dedicated column of the output
|
2022-05-20 08:45:09 -04:00
|
|
|
if self.check_lotw_activity and self.check_lotw(call_dx):
|
2022-05-19 11:40:17 -04:00
|
|
|
lotw = "✓"
|
2022-05-20 08:45:09 -04:00
|
|
|
else:
|
|
|
|
lotw = ""
|
2022-05-20 11:12:55 -04:00
|
|
|
|
|
|
|
# Depending on the user's choice, the row will be color coded
|
|
|
|
# depending on the band or the DX station's continent
|
2022-05-18 17:39:02 -04:00
|
|
|
try:
|
2022-05-19 08:15:19 -04:00
|
|
|
if self.config['colors']['color_by'] == "band":
|
|
|
|
foreground = self.config['band_colors'][freq[:-5]]
|
|
|
|
elif self.config['colors']['color_by'] == "continent":
|
|
|
|
foreground = self.config['cont_colors'][continent]
|
|
|
|
else:
|
|
|
|
foreground = "white"
|
2022-05-18 17:39:02 -04:00
|
|
|
except Exception:
|
2022-05-19 08:15:19 -04:00
|
|
|
foreground = "white"
|
2022-05-18 17:39:02 -04:00
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# Removes the trailing .0 from a frequency for better readability
|
2022-05-18 17:39:02 -04:00
|
|
|
freq = freq.replace('.0', '')
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# If the DX station's entity hasn't been worked/confirmed via
|
|
|
|
# LotW yet, the row's background will be color coded.
|
|
|
|
if self.check_lotw_confirmed and \
|
|
|
|
cty_details[2] not in self.confirmed_entities:
|
2022-05-18 17:39:02 -04:00
|
|
|
background = self.config['colors']['alert_bg']
|
2022-05-19 08:15:19 -04:00
|
|
|
foreground = self.config['colors']['alert_fg']
|
2022-05-18 17:39:02 -04:00
|
|
|
else:
|
|
|
|
background = self.config['colors']['default_bg']
|
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# color of the table separator
|
2022-05-19 08:15:19 -04:00
|
|
|
sep = fg("grey_27")+'|'+fg(foreground)
|
2022-05-18 17:39:02 -04:00
|
|
|
|
2022-05-20 11:12:55 -04:00
|
|
|
# Contructs the row that will be printed
|
2022-05-18 17:39:02 -04:00
|
|
|
row = [call_de, sep, freq, sep, call_dx, \
|
2022-05-19 11:40:17 -04:00
|
|
|
sep, areaname, sep, continent, sep, lotw, sep, comment, sep, time]
|
2022-05-19 08:15:19 -04:00
|
|
|
print(bg(background) + fg(foreground) + \
|
2022-05-20 11:12:55 -04:00
|
|
|
'{:9.9} {:<1} {:>7.7} {:<1} {:<10.10} {:<1} '\
|
|
|
|
'{:<16.16} {:<1} {:<2.2} {:<1} {:<1.1} {:<1} {:<30.30} '\
|
|
|
|
'{:<1} {:<4.4}'.format(*row) + attr("reset"))
|
2022-05-18 17:39:02 -04:00
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
def main():
|
2022-05-20 11:12:55 -04:00
|
|
|
"""main routine"""
|
2022-05-20 08:45:09 -04:00
|
|
|
try:
|
|
|
|
color_spot = ColorSpot()
|
|
|
|
color_spot.get_spots()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0) # or 1, or whatever
|
2022-05-18 17:39:02 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
sys.exit(main())
|
|
|
|
except EOFError:
|
|
|
|
pass
|