@propertydefengagement_rate(self)->float:"""Calculate engagement rate."""@propertydeftotal_engagement(self)->int:"""Sum of all engagement metrics."""@propertydefhas_media(self)->bool:"""Check if tweet has media."""@propertydefis_thread(self)->bool:"""Check if part of a thread."""
fromxeepyimportXeepyfromcollectionsimportCounterasyncdefpopular_hashtags(username:str):asyncwithXeepy()asx:tweets=awaitx.scrape.tweets(username,limit=200)all_hashtags=[]fortweetintweets.items:all_hashtags.extend(tweet.hashtags)counter=Counter(all_hashtags)print("Most used hashtags:")fortag,countincounter.most_common(10):print(f" #{tag}: {count}")asyncio.run(popular_hashtags("username"))
fromxeepyimportXeepyasyncdeffilter_tweets(username:str):asyncwithXeepy()asx:tweets=awaitx.scrape.tweets(username,limit=100)# Tweets with linkswith_links=[tfortintweets.itemsift.urls]print(f"With links: {len(with_links)}")# Tweets with mentionswith_mentions=[tfortintweets.itemsift.mentions]print(f"With mentions: {len(with_mentions)}")# Long tweetslong_tweets=[tfortintweets.itemsiflen(t.text)>200]print(f"Long tweets: {len(long_tweets)}")asyncio.run(filter_tweets("username"))
fromxeepyimportXeepyimportjsonasyncdefexport_tweet(tweet_url:str):asyncwithXeepy()asx:# Get single tweettweet=awaitx.scrape.tweet(tweet_url)# Convert to dictdata=tweet.to_dict()# Save as JSONwithopen("tweet.json","w")asf:json.dump(data,f,indent=2,default=str)asyncio.run(export_tweet("https://x.com/user/status/123"))
fromxeepyimportXeepyasyncdefget_threads(username:str):asyncwithXeepy()asx:tweets=awaitx.scrape.tweets(username,limit=100)# Group by conversationconversations={}fortweetintweets.items:iftweet.conversation_id:iftweet.conversation_idnotinconversations:conversations[tweet.conversation_id]=[]conversations[tweet.conversation_id].append(tweet)# Find threads (2+ tweets in conversation by same author)threads=[tweetsfortweetsinconversations.values()iflen(tweets)>=2]print(f"Found {len(threads)} threads")asyncio.run(get_threads("username"))