-
Notifications
You must be signed in to change notification settings - Fork 0
/
VATSIM-Python-Notifier.py
170 lines (155 loc) · 7.19 KB
/
VATSIM-Python-Notifier.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Script created to evaluate VATSIM network logins and logouts to relay to Discord bots
# Written by: Aaron Albertson
# Copywritten - 2020 Aaron Albertson
# Version: 1.0 - 3/10/2020
# Version: 1.1 - 10/14/2020 - Adjusted parsing code for new format from new kafka feed to work again.
# Version: 1.2 - 10/28/2020 - Added filter to not display _OBS callsigns in open or close messages.
import json
import os
from dotenv import load_dotenv
from datetime import datetime
from json import loads
from kafka import KafkaConsumer
from kafka.errors import BrokerNotAvailableError, NoBrokersAvailable
from discord_webhook import DiscordWebhook, DiscordEmbed
# Variables
version = "v1.2"
load_dotenv()
k_topic = os.getenv('KTOPIC')
k_servers = os.getenv('KSERVERS')
token = str(os.getenv('DISCORD_TOKEN'))
dischannel = os.getenv('TEXT_CHANNEL')
webhookurl = os.getenv('WEBHOOK_URL')
global rating_long
# Check message filter here to meet first 4 characters of what to eval in add_clients / remove_clients
messagefilter = ['DCA_','IAD_','BWI_','PCT_','ADW_','DC_C','RIC_','ROA_','ORF_','ACY_','NGU_',
'NTU_','NHK_','RDU_','CHO_','HGR_','LYH_','EWN_','LWB_','ISO_','MTN_','HEF_',
'MRB_','PHF_','SBY_','NUI_','FAY_','ILM_','NKT_','NCA_','NYG_','DAA_','DOV_',
'POB_','GSB_','WAL_','CVN_','DC_0','DC_1','DC_2','DC_3','DC_5','DC_N',
'DC_S','DC_E','DC_W','DC_I','JYO_']
# Defs
def discord_webhook(callsign, name, cid, rating_long, server, status):
webhook = DiscordWebhook(url=webhookurl)
if status == "online":
embed = DiscordEmbed(title=callsign + " - Online", description=callsign + ' is now online on the VATSIM network.', color=65290)
embed.set_footer(text='ZDC VATSIM Notify Bot ' + version, icon_url='https://vzdc.org/photos/discordbot.png')
embed.set_thumbnail(url='https://vzdc.org/photos/logo.png')
embed.set_timestamp()
embed.add_embed_field(name='Name', value=name)
embed.add_embed_field(name='Rating', value=rating_long)
embed.add_embed_field(name='CID', value=cid)
embed.add_embed_field(name='Callsign', value=callsign)
embed.add_embed_field(name='Server', value=server)
webhook.add_embed(embed)
webhook.execute()
webhook.remove_embed(0)
else:
embed = DiscordEmbed(title=callsign + " - Offline", description=callsign + ' is now offline on the VATSIM network.', color=16711683)
embed.set_footer(text='ZDC VATSIM Notify Bot ' + version, icon_url='https://vzdc.org/photos/discordbot.png')
embed.set_thumbnail(url='https://vzdc.org/photos/logo.png')
embed.set_timestamp()
#embed.add_embed_field(name='Name', value=name)
#embed.add_embed_field(name='Rating', value=rating_long)
#embed.add_embed_field(name='CID', value=cid)
#embed.add_embed_field(name='Callsign', value=callsign)
#embed.add_embed_field(name='Server', value=server)
webhook.add_embed(embed)
webhook.execute()
webhook.remove_embed(0)
def vatsim_rating_checker(rating):
global rating_long
if rating == 1:
rating_long = "OBS"
if rating == 2:
rating_long = "S1"
if rating == 3:
rating_long = "S2"
if rating == 4:
rating_long = "S3"
if rating == 5:
rating_long = "C1"
if rating == 6:
rating_long = "C2"
if rating == 7:
rating_long = "C3"
if rating == 8:
rating_long = "I1"
if rating == 9:
rating_long = "I2"
if rating == 10:
rating_long = "I3"
if rating == 11:
rating_long = "SUP"
if rating == 12:
rating_long = "ADM"
else:
pass
def vatsim_notifier():
# Call data from VATSIM Kafka Servers
consumer = KafkaConsumer(k_topic,
bootstrap_servers=k_servers,
security_protocol='SASL_PLAINTEXT',
sasl_mechanism='PLAIN',
sasl_plain_username='datafeed-reader',
sasl_plain_password='datafeed-reader',
auto_offset_reset='latest',
value_deserializer=lambda m: loads(m.decode('utf-8')))
# Notify Console script started
timestamp = str(datetime.now())
print("[" + timestamp + "] - Notifer " + version + " Started!")
# Evaluate results for callsign sign in and outs. FOR LOOP
for message in consumer:
message = message.value
msgtype = message['$type']
# print (message)
if msgtype == 'VATSIM.Network.Dataserver.Dtos.AddClientDto, VATSIM.Network.Dataserver':
callsign = message['callsign']
# strip callsign to first 4 characters for comparing / filtering
strippedcall = callsign[:4]
# strip callsign to last 4 characters to filter out ATIS
atischecker = callsign[-4:]
# print("DEBUG: STRIPPED CALL: " + strippedcall)
# print("DEBUG: ATISCHECKER: " + atischecker)
if strippedcall in messagefilter and atischecker != "ATIS" and atischecker != "_OBS":
timestamp = str(datetime.now())
cid = str(message['cid'])
name = message['real_name']
callsign = message['callsign']
rating = message['rating']
vatsim_rating_checker(rating)
server = message['server']
status = "online"
# DEBUG
# prettyprint = json.dumps(message, indent=4, separators=(',',':'))
# print(prettyprint)
# print(data)
print("[" + timestamp + "] - " + name + "[" + rating_long + "] (" + cid + ") has opened " + callsign + " on VATSIM.")
discord_webhook(callsign, name, cid, rating_long, server,status)
else:
pass
else:
pass
if msgtype == 'VATSIM.Network.Dataserver.Dtos.RemoveClientDto, VATSIM.Network.Dataserver':
# print (message)
callsign = message['callsign']
# strip callsign to first 4 characters for comparing / filtering
strippedcall = callsign[:4]
# strip callsign to last 4 characters to filter out ATIS
atischecker = callsign[-4:]
# print("DEBUG: STRIPPED CALL: " + strippedcall)
# print("DEBUG: ATISCHECKER: " + atischecker)
if strippedcall in messagefilter and atischecker != "ATIS" and atischecker != "_OBS":
timestamp = str(datetime.now())
callsign = message['callsign']
status = "offline"
# DEBUG
# prettyprint = json.dumps(message, indent=4, separators=(',',':'))
# print(prettyprint)
print("[" + timestamp + "] - " + callsign + " has closed.")
discord_webhook(callsign, None, None, None, None, status)
else:
pass
else:
pass
# RUN
vatsim_notifier()