162 lines
5.8 KiB
Python
162 lines
5.8 KiB
Python
import sys
|
|
import csv
|
|
import re
|
|
import json
|
|
import os
|
|
from telnetlib import Telnet
|
|
from colored import fg, bg, attr
|
|
import configparser
|
|
|
|
class ColorSpot():
|
|
|
|
def __init__(self):
|
|
self.version = "0.1.0"
|
|
|
|
self.config = configparser.ConfigParser()
|
|
self.config_file = os.path.expanduser('~/.colorspot.ini')
|
|
self.read_config(self.config, self.config_file)
|
|
|
|
self.country_data = {}
|
|
#TODO: download file and/or tell user what to do
|
|
with open('cty.json', "r") as json_file:
|
|
self.country_data = json.load(json_file)
|
|
|
|
self.qsl_countries = self.get_confirmed_countries()
|
|
|
|
@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',
|
|
'timeout': '100'}
|
|
config['colors'] = {
|
|
'use_colors': 'yes',
|
|
"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'}
|
|
with open(file_name, 'w') as configfile:
|
|
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
|
|
|
|
@staticmethod
|
|
def get_confirmed_countries():
|
|
ret = []
|
|
#TODO: download file and/or tell user what to do
|
|
file = open("lotwreport.adi", "r")
|
|
for row in file:
|
|
if re.search("COUNTRY", row):
|
|
country = row.partition(">")[2].lower().rstrip()
|
|
if country not in ret:
|
|
ret.append(country)
|
|
return ret
|
|
|
|
@staticmethod
|
|
def check_lotw(call):
|
|
ret = ""
|
|
#TODO: download file and/or tell user what to do
|
|
csv_file = csv.reader(open('lotw-user-activity.csv', "r"), delimiter=",")
|
|
#loop through the csv file
|
|
for row in csv_file:
|
|
if call == row[0]:
|
|
ret = row[1]
|
|
return ret
|
|
return ret
|
|
|
|
@staticmethod
|
|
def get_country(call, data):
|
|
done = False
|
|
while not done:
|
|
for number in data:
|
|
for country in data[number]:
|
|
if call == country["prefix"]:
|
|
return country["areaname"]
|
|
call = call[:-1]
|
|
return None
|
|
|
|
def get_spots(self):
|
|
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')
|
|
if "enter your call" in line:
|
|
my_str_as_bytes = str.encode(self.config['cluster']['user']+"\n")
|
|
telnet.write(my_str_as_bytes)
|
|
elif "DX de" in line:
|
|
try:
|
|
band_col = ""
|
|
call_de = re.search('DX de (.+?): ', line).group(1)
|
|
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)
|
|
areaname = self.get_country(call_dx, self.country_data)
|
|
lotw_date = self.check_lotw(call_dx)
|
|
|
|
lotw = ""
|
|
if lotw_date:
|
|
lotw = "[LotW] "
|
|
|
|
try:
|
|
band_col = self.config['colors'][freq[:-5]]
|
|
except Exception:
|
|
band_col = "white"
|
|
|
|
freq = freq.replace('.0', '')
|
|
|
|
if areaname.lower() not in self.qsl_countries:
|
|
background = self.config['colors']['alert_bg']
|
|
band_col = self.config['colors']['alert_fg']
|
|
else:
|
|
background = self.config['colors']['default_bg']
|
|
|
|
sep = fg("grey_27")+'|'+fg(band_col)
|
|
|
|
row = [call_de, sep, freq, sep, call_dx, \
|
|
sep, areaname, sep, lotw + comment, sep, time]
|
|
|
|
print(bg(background) + fg(band_col) + \
|
|
'{:9.9} {:<1} {:>7.7} {:<1} {:<10.10} {:<1} {:<16.16} {:<1} {:<30.30} {:<1} {:<4.4}'.format(*row) + attr("reset"))
|
|
|
|
except AttributeError:
|
|
print(line)
|
|
elif "login: " in line:
|
|
print(line)
|
|
|
|
#####################################################
|
|
# Main Routine #
|
|
#####################################################
|
|
def main():
|
|
"""the main routine"""
|
|
color_spot = ColorSpot()
|
|
color_spot.get_spots()
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
sys.exit(main())
|
|
except EOFError:
|
|
pass
|