Maybe the title is a bit confusing but my brain is stuck on this for like days now. So here is my "problem". I am redoing my Discord Bot and I have a small task that will check 2 JSON files (same format, different data) and there is a difference between them it will send a message.
This code needs to load 2 JSON files ("VATSIM_DATA_old.json & VATSIM_Data_new.json") and compare them and find differences. Now for simplicity, I reduced code so I just get if someone came online and if they are no longer online.
So my flow for this is something like this: -When bots start, fetch JSON data and save it to file so we can compare updated data with this file, -Now it is time to check if someone came online by fetching new data from the same link and saving it to the new file, -Load both files as JSON so we can find all items inside of them and compare if some items are present in both or only in one of them, -After comparing them and doing actions such as sending messages to the channel let's save that new data to our Old_Data.json file and go to sleep for X min (I set it to 5),
The problem I have is that it will send the same message every time a task is started. So my logic here was ok let's check if files are changing and they are but the code still somehow ignores part of the code that tells him if data is not changed here simply pass.
So here is my code:
atc = ["ADR_CTR", "ADR_W_CTR", "ADR_U_CTR", "ADR_E_CTR"] #this is short version of array which needs to be checked. This will most likely be moved to the config file so code is cleaner
@tasks.loop(minutes=5)
async def atcAnnoucements(self):
#Load old and new VATSIM Data for comparing!
with open("VATSIM_Data_old.json", encoding="utf-8") as f:
data = json.load(f)
file = open("VATSIM_Data_new.json", "w", encoding="utf-8")
file.write(requests.get('https://data.vatsim.net/vatsim-data.json').text)
file.close()
with open("VATSIM_Data_new.json", encoding="utf-8") as s:
data1 = json.load(s)
#Check if new VATSIM data contains some changes in online atc
#If callsign from atc array is present in VATSIM_Data_old it will check is same controller active or not
#If the controller is not changed it will skip, if new controller comes online it will send a message that the ATC station came online
for item in data1['clients']:
if item['callsign'] in data['clients']:
pass
else:
if item['callsign'] in atc:
time_start = item['time_logon']
time_logg = time_start[:19]
logon_start = datetime.strptime(time_logg, '%Y-%m-%dT%H:%M:%S')
time_logon = logon_start.strftime('%H:%M')
channel = self.bot.get_channel(781834455765483562)
embed = Embed(title="ATC Annoucement", description="`New controller just logged in!`", color=0x00ff4c)
embed.add_field(name='Position', value=f"`{item['callsign']}`", inline=False)
embed.add_field(name='Frequency', value=f"`{item['frequency']}`", inline=False)
embed.add_field(name='Controller', value=f"`{item['realname']}`", inline=False)
embed.set_footer(text=f"Logged on at: {time_logon}")
await channel.send(embed=embed)
if item['callsign'] in data['clients'] and atc and not data1['clients']:
channel = self.bot.get_channel(781834455765483562)
embed2 = Embed(title="ATC Logged off!", description=f"{item['callsign']} just logged off!", color=0xff8800)
await channel.send(embed=embed2)
file = open("VATSIM_Data_old.json", "w", encoding="utf-8")
file.write(requests.get('https://data.vatsim.net/vatsim-data.json').text)
file.close()
print("Saved new VATSIM Data!")
EDIT: In my first attempt to just get 1 message, I did something like the code below and it worked. But as soon as I changed to the code above it simply sends the same message all over again
json1 = open("VATSIM_Data_old.json", "r", encoding="utf-8").read()
xy = json.dumps(json1)
s1 = json.loads(xy)
t = requests.get('https://data.vatsim.net/vatsim-data.json').json()
xx = json.dumps(t)
s = json.loads(xx)
for item in s['clients']:
if item['callsign'] in atc:
if item['callsign'] in s1:
pass
else:
if item['callsign'] in atc:
time_start = item['time_logon']
time_logg = time_start[:19]
logon_start = datetime.strptime(time_logg, '%Y-%m-%dT%H:%M:%S')
time_logon = logon_start.strftime('%H:%M')
channel = self.bot.get_channel(781834455765483562)
embed = Embed(title="ATC Annoucement", description="`New controller just logged in!`", color=0x00ff4c)
embed.add_field(name='Position', value=f"`{item['callsign']}`", inline=False)
embed.add_field(name='Frequency', value=f"`{item['frequency']}`", inline=False)
embed.add_field(name='Controller', value=f"`{item['realname']}`", inline=False)
embed.set_footer(text=f"Logged on at: {time_logon}")
await channel.send(embed=embed)```