I have currently written a script where I am doing a sort of monitoring on my own web API. The point of my script is that I want to make a sort of monitor where I check every hour to see if there is a new data that has been applied into my API and whenever there is a new data then I would like it to print out.
class Monitor:
def __init__(self, url: str) -> None:
self.url = url
self.stored_id: set = set()
def doRequest(self) -> None:
while True:
try:
response = requests.get(
self.url,
timeout=12,
)
if response.status_code == 200:
parser = response.json()
if parser.get('configurations', {}):
for configuration in parser['configurations']:
if configuration['configuration']['id'] not in self.stored_id
upload_to_discord({
'art-number': configuration['art'],
...
})
self.stored_id.add(configuration['configuration']['id'])
time.sleep(3600)
except Exception as e:
print(e)
However my problem is that whenever I run this script, It will always print out to my discord (upload_to_discord(dict)
) and that means whenever I restart the script, it will always print it out. I wonder how I can skip the first iteration and instead of printing out in the first loop, it should instead store the found data and then after the first iteration it will start to "monitor" to see if there has been aynthing added and print out whenever there is a new data?
CodePudding user response:
something like this:
class Monitor:
def __init__(self, url: str) -> None:
self.url = url
self.stored_id: set = set()
def doRequest(self) -> None:
cnt = False
while True:
try:
response = requests.get(
self.url,
timeout=12,
)
if response.status_code == 200:
parser = response.json()
if parser.get('configurations', {}):
for configuration in parser['configurations']:
if (configuration['configuration']['id'] not in self.stored_id) and (cnt != 0):
upload_to_discord({
'art-number': configuration['art']
...
})
self.stored_id.add(configuration['configuration']['id'])
cnt = True
else:
cnt = True
time.sleep(3600)
except Exception as e:
print(e)