fix rules

This commit is contained in:
Mylloon 2022-08-08 09:41:09 +02:00
parent f595dcd83d
commit 1eb5f84cc0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

23
main.py
View file

@ -286,22 +286,24 @@ def createClient(consumer_key, consumer_secret, access_token, access_token_secre
def create_rules(tracked_users: list[str]) -> list[str]: def create_rules(tracked_users: list[str]) -> list[str]:
"""Create rules for tracking users, by respecting the twitter API policies""" """Create rules for tracking users, by respecting the twitter API policies"""
rules = [] rules = []
buffer = ""
# Repeating rules
repeat = "-is:retweet"
# Buffer
buffer = repeat
# Track users # Track users
for user in tracked_users: for user in tracked_users:
# Check if the rule don't exceeds the maximum length of a rule (512) # Check if the rule don't exceeds the maximum length of a rule (512)
# 5 is len of "from:" # 5 is len of "from:"
if len(buffer) + len(user) + 5 > 512: if len(buffer) + len(user) + 5 > 512:
rules.append(buffer[:-4]) rules.append(buffer)
buffer = "" buffer = repeat
buffer += f'from:{user} OR ' buffer += f' OR from:{user}'
if len(buffer) > 0: if len(buffer) > 0:
rules.append(buffer[:-4]) rules.append(buffer)
# Ignore retweets
rules.append("-is:retweet")
if len(rules) > 25: if len(rules) > 25:
raise BufferError("Too much rules.") raise BufferError("Too much rules.")
@ -328,6 +330,11 @@ def start():
stream = Listener(keys["BEARER_TOKEN"], client) stream = Listener(keys["BEARER_TOKEN"], client)
# Clean rules
for rule in stream.get_rules().data:
stream.delete_rules(rule.id)
# Add new rules
for rule in create_rules(tracked_users): for rule in create_rules(tracked_users):
stream.add_rules(StreamRule(rule)) stream.add_rules(StreamRule(rule))
stream.filter(threaded=True, tweet_fields=['author_id']) stream.filter(threaded=True, tweet_fields=['author_id'])