fix rules
This commit is contained in:
parent
f595dcd83d
commit
1eb5f84cc0
1 changed files with 15 additions and 8 deletions
23
main.py
23
main.py
|
@ -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'])
|
||||||
|
|
Reference in a new issue