I’m using Twitter’s real-time filtered stream API (for the first time!) in Python, and I’m basically trying to recreate my real-time timeline in-terminal: I want to get a response every time someone I follow tweets, retweets, etc. I’m doing this in two steps:
- GETting an id list of people I follow (my “friends”) from
api.twitter.com/1.1/friends/ids.json?screen_name=my_username
- Iterating over the returned list ids and creating a “from:” follow rule for each:
for f_id in friends_list_response['ids']:
rule_val = "from:" + str(f_id)
filter_rules.append({"value": rule_val, "tag": str(f_id)},)
I have this working for up to 10 test friends—they tweet, I get terminal printout in real-time. However I very quickly hit a cap, since I follow 500+ accounts, and am thus creating 500+ distinct from:
rules for the filtered stream.
(HTTP 422): {"detail":"Rule creation request exceeds account’s current rule limits. Please remove or update existing rules to proceed.","title":"RulesCapExceeded","type":"https://api.twitter.com/2/problems/rule-cap"
I don’t really understand what to do about hitting my cap (that "type:" url doesn’t offer guidance). Whether I need higher permissions to the API v2, or if I need to do something with Power Track. How can I increase my rule limit, ideally tracking 1000+ accounts? I believe I have the basic developer authorization. Or, is there a workaround to a filter rule for tweets from my friends? I wish there was an endpoint that filtered tweets by everyone followed by a user.
Again, I’m a new to this API, so I appreciate any insight!
2
Answers
Nevermind, Tweepy's "home_timeline" function is exactly what I needed, almost a direct answer to "I wish there was an endpoint that filtered tweets by everyone followed by a user."
In Twitter API v2, you can use filtered stream to follow a list of users. You can build filtered stream from rules. The limits of rules are there:
So, you can add 25 concurrent rules with 512 characters for each rule. If the only filter will be users accounts or IDs, you need to calculate the number of users that fit within the character limitations.
The average character length of the id of twitter accounts could be between 8 (older) and 18 (newest accounts). Note that you will need to add
'sfrom:'
for each user, this means 6 more characters for each user account.If we take into account that, the average length of ID is 13, adding the 6 predecessor characters:
512 limit characters / 19 avg characters per user ID = ~26 ID per rule
If we have 25 rules:
25 rules * 26 IDs per rule = ~650 IDs in total
Note that this is approximate and you can use the account name instead of the ID.