How to store found data when starting script?

Question:

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?

Asked By: ProtractorNewbie

||

Answers:

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):
                                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)
Answered By: guin0x
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.