Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce API query if not needed #2

Open
Klausoleum opened this issue Jan 17, 2024 · 1 comment
Open

Reduce API query if not needed #2

Klausoleum opened this issue Jan 17, 2024 · 1 comment

Comments

@Klausoleum
Copy link

I had another idea to reduce the API access to the tgtg server side and not to provoke the 403 error (capture).

Currently the tgtg api is queried every 60 seconds (default) regardless of the settings in Telegram (/settings).
Before you start the loop, you could query the settings whether information is desired. If no information is required, the API is not queried and tgtg does not notice the bot.

Here is an untested draft:

def get_available_items_per_user(self):
    while True:
        try:
            if any(val == 1 for user_settings in self.users_settings_data.values() for val in user_settings.values()):
                # Führe die Abfragen nur aus, wenn mindestens eine Einstellung aktiviert ist
                temp_available_items = {}
                for key in self.users_login_data.keys():
                    self.connect(key)
                    time.sleep(1)
                    available_items = self.get_favourite_items()
                    for item in available_items:
                        status = "null"
                        item_id = item['item']['item_id']
                        # ... (Code davor)
                        if not status == "null":
                            temp_available_items[item_id] = status
                        self.available_items_favorites[item_id] = item
                        if item_id in temp_available_items and \
                                self.users_settings_data[key][temp_available_items[item_id]] == 1:
                            # ... (Code davor)
                            self.send_message_with_link(key, text, item_id)
                self.save_available_items_favorites_to_txt()
            time.sleep(60)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")```
@Klausoleum
Copy link
Author

Or this version:

def get_available_items_per_user(self):
    while any(val == 1 for user_settings in self.users_settings_data.values() for val in user_settings.values()):
        try:
            temp_available_items = {}
            for key in self.users_login_data.keys():
                self.connect(key)
                time.sleep(1)
                available_items = self.get_favourite_items()
                for item in available_items:
                    status = "null"
                    item_id = item['item']['item_id']
                    # ... (Code davor)
                    if not status == "null":
                        temp_available_items[item_id] = status
                    self.available_items_favorites[item_id] = item
                    if item_id in temp_available_items and \
                            self.users_settings_data[key][temp_available_items[item_id]] == 1:
                        # ... (Code davor)
                        self.send_message_with_link(key, text, item_id)
            self.save_available_items_favorites_to_txt()
            time.sleep(60)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")

Carleslc added a commit to Carleslc/TooGoodToGo-TelegramBot that referenced this issue Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant