From b1c5abe1f99b24cefb6ed876f82b7a2e447ada55 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 9 Aug 2022 02:17:28 +0200 Subject: [PATCH] reduce spam of twitter API --- main.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 1b1ddf3..0eeb3d7 100644 --- a/main.py +++ b/main.py @@ -331,14 +331,24 @@ def start(): stream = Listener(keys["BEARER_TOKEN"], client) - # Clean rules - old_rules = stream.get_rules() - if (old_rules.data): - stream.delete_rules([rule.id for rule in old_rules.data]) + # Gathering rules + rules = [rule for rule in create_rules(tracked_users)] + + # Check if rules already exists + old_rules = stream.get_rules().data + old_rules_values = [rule.value for rule in old_rules] + # Same amount of rules + if len(old_rules_values) == len(rules): + for rule in rules: + # Check if Twitter doesn't know the rule and change rules if needed + if rule not in old_rules_values: + # Clean old rules + stream.delete_rules([rule.id for rule in old_rules]) + + # Add new rules + stream.add_rules([StreamRule(rule) for rule in rules]) + break - # Add new rules - stream.add_rules([StreamRule(rule) - for rule in create_rules(tracked_users)]) stream.filter(threaded=True, tweet_fields="author_id")