2022-09-20 15:34:38 -07:00
|
|
|
from math import inf
|
2022-09-12 01:31:09 -07:00
|
|
|
import tweepy
|
|
|
|
|
|
|
|
|
|
import secrets
|
|
|
|
|
import util
|
|
|
|
|
|
|
|
|
|
class API:
|
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
API.instance = self
|
|
|
|
|
self.client = tweepy.Client(
|
|
|
|
|
bearer_token=secrets.bearer_token(),
|
2022-09-20 15:34:38 -07:00
|
|
|
consumer_key=secrets.api_key(), consumer_secret=secrets.api_secret(),
|
|
|
|
|
access_token=secrets.access_token(), access_token_secret=secrets.access_secret()
|
2022-09-12 01:31:09 -07:00
|
|
|
)
|
|
|
|
|
|
2022-09-20 15:34:38 -07:00
|
|
|
def get_user_tweets(self, id: int, count=inf):
|
2022-09-12 01:31:09 -07:00
|
|
|
posts = list()
|
|
|
|
|
|
2022-09-20 15:34:38 -07:00
|
|
|
retrieve_size = util.clamp(count, 5, 100)
|
2022-09-12 01:31:09 -07:00
|
|
|
retrieved_tweets = 0
|
|
|
|
|
pagination_token = None
|
|
|
|
|
|
2022-09-20 15:34:38 -07:00
|
|
|
# while retrieved_tweets < count: # or we haven't reached the end of user's tweets
|
|
|
|
|
resp = self.client.get_users_tweets(id, max_results=retrieve_size, media_fields=['url'], expansions=['entities.mentions.username', 'referenced_tweets.id.author_id'])
|
|
|
|
|
return resp
|