Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions nexus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,21 @@ def user_lookup_sns(self, user: str, quantity: int):
:param quantity: last x tweets needed, chronologically
:param user: twitter handle of user
"""
MAX_TWEETS = 5000 # hard limit to prevent resource abuse
if not isinstance(quantity, int):
raise ValueError("quantity must be an integer")
if quantity <= 0:
raise ValueError("quantity must be > 0")
if quantity > MAX_TWEETS:
raise ValueError(f"Requested quantity {quantity} exceeds allowed maximum ({MAX_TWEETS})")

query: List[Dict] = []

logger.info(f"Pulling {user}'s tweets")
# Sanitize user input before logging to prevent log injection
sanitized_user = user.replace('\n', ' ').replace('\r', ' ')
logger.info(f"Pulling {sanitized_user}'s tweets (max {quantity})")
for idx, tweet in tqdm(enumerate(sntwitter.TwitterSearchScraper(f'from:{user}').get_items())):
if idx > quantity:
if idx >= quantity:
break
query.append({"full_text": tweet.content, "tweet_link": f"https://twitter.com/{tweet.user.username}/status/{tweet.id}" , "created_at": tweet.date, "tweet_id": tweet.id, "user": tweet.user.username})

Expand Down Expand Up @@ -133,6 +143,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])