twitterapi.users.follower module
user.py: Implementation of Twitter Follower lookup endpoint.
“The follows lookup endpoints enable you to explore and analyze relationships between users, which is sometimes called network analysis. Specifically, there are two REST endpoints that return user objects representing who a specified user is following, or who is following a specified user.” (Source: https://developer.twitter.com/en/docs/twitter-api/users/follows/introduction)
Find the Open API Spec under: https://api.twitter.com/2/openapi.json
Examples
Get followers by id:
import os
os.environ["BEARER_TOKEN"] = "xxxxxxxxxxx"
from sparta.twitterapi.users.follower import get_followers_by_id
async for user in get_followers_by_id('1422600096324231168'):
print(user.model_dump_json())
Get following by id:
import os
os.environ["BEARER_TOKEN"] = "xxxxxxxxxxx"
from sparta.twitterapi.users.follower import get_following_by_id
async for user in get_following_by_id('1422600096324231168'):
print(user.model_dump_json())
- async sparta.twitterapi.users.follower.get_followers_by_id(id: str, max_results: int = 1000, raise_exception_on_http_503: bool = False, raise_exception_on_model_validation_error: bool = False, sleep_time_on_not_ok: int = 10, num_tries_on_not_ok: int = -1) AsyncGenerator[User, None]
Returns Users who are followers of the specified User ID.
- Parameters:
id (str) – The ID of the User to lookup.
max_results (int, optional) – The maximum number of results. Defaults to 1000.
raise_exception_on_http_503 (bool, optional) – Whether to raise an exception on HTTP 503 errors. If False, then wait until HTTP 503 resolves, which is better suitable in case of live streaming. True, is more suitable for batch processing. Defaults to False. NOTE: In some cases, the Twitter API returns HTTP 503 errors during pagination, which may be transient, but still block live streaming.
raise_exception_on_model_validation_error (bool, optional) – Whether to raise an exception on model validation errors. If False, then only logs warnings, which is better suitable in case of live streaming. True, is more suitable for batch processing. Defaults to False.
sleep_time_on_not_ok (int, optional) – The sleep time in seconds to wait before retrying after an HTTP error. Defaults to 10.
num_tries_on_not_ok (int, optional) – The number of tries to attempt after an HTTP error. If -1, then unlimited. Defaults to -1.
- Raises:
Exception – Cannot get the search result due to an http error.
Exception – User not found error.
Exception – HTTP 503 error.
Exception – Model validation error.
- Returns:
AsyncGenerator that yields Twitter User objects.
- Return type:
AsyncGenerator[User, None]
- Yields:
Iterator[AsyncGenerator[User, None]] – A Twitter User object.
- async sparta.twitterapi.users.follower.get_following_by_id(id: str, max_results: int = 1000, raise_exception_on_http_503: bool = False, raise_exception_on_model_validation_error: bool = False, sleep_time_on_not_ok: int = 10, num_tries_on_not_ok: int = -1) AsyncGenerator[User, None]
Returns Users that are being followed by the provided User ID.
- Parameters:
id (str) – The ID of the User to lookup.
max_results (int, optional) – The maximum number of results. Defaults to 1000.
raise_exception_on_http_503 (bool, optional) – Whether to raise an exception on HTTP 503 errors. If False, then wait until HTTP 503 resolves, which is better suitable in case of live streaming. True, is more suitable for batch processing. Defaults to False. NOTE: In some cases, the Twitter API returns HTTP 503 errors during pagination, which may be transient, but still block live streaming.
raise_exception_on_model_validation_error (bool, optional) – Whether to raise an exception on model validation errors. If False, then only logs warnings, which is better suitable in case of live streaming. True, is more suitable for batch processing. Defaults to False.
sleep_time_on_not_ok (int, optional) – The sleep time in seconds to wait before retrying after an HTTP error. Defaults to 10.
num_tries_on_not_ok (int, optional) – The number of tries to attempt after an HTTP error. If -1, then unlimited. Defaults to -1.
- Raises:
Exception – Cannot get the search result due to an http error.
Exception – User not found error.
Exception – HTTP 503 error.
Exception – Model validation error.
- Returns:
AsyncGenerator that yields Twitter User objects.
- Return type:
AsyncGenerator[User, None]
- Yields:
Iterator[AsyncGenerator[User, None]] – A Twitter User object.