diff --git a/nexus/utils.py b/nexus/utils.py index 852482b..28cfc68 100644 --- a/nexus/utils.py +++ b/nexus/utils.py @@ -14,6 +14,8 @@ load_dotenv() class Utils: + MAX_TWEETS = 3200 # Maximum allowed tweets per Twitter API (user timeline) + def __init__(self): api_key = os.getenv("API_KEY") api_secret = os.getenv("API_SECRET") @@ -32,6 +34,13 @@ def user_lookup_tweepy(self, user: str, quantity: int): :param quantity: amount of tweets you want to retrieve """ + # Validate and cap quantity to prevent denial of service + if not isinstance(quantity, int) or quantity <= 0: + raise ValueError("Quantity must be a positive integer") + if quantity > self.MAX_TWEETS: + logger.info(f"Requested quantity {quantity} exceeds limit; capping to {self.MAX_TWEETS}.") + quantity = self.MAX_TWEETS + query: List[Dict] = [] tweets = tweepy.Cursor( @@ -133,6 +142,4 @@ def create_topics(self, documents: List[Dict], id_to_cluster_label: Dict, id_fie bot = Utils() lookup = bot.user_lookup_sns("JoeBiden", 5000) print(len(lookup)) - print(lookup[-1]) - - + print(lookup[-1]) \ No newline at end of file