|
from typing import Dict |
|
|
|
from agent.tools.data_providers.RapidDataProviderBase import RapidDataProviderBase, EndpointSchema |
|
|
|
|
|
class TwitterProvider(RapidDataProviderBase): |
|
def __init__(self): |
|
endpoints: Dict[str, EndpointSchema] = { |
|
"user_info": { |
|
"route": "/screenname.php", |
|
"method": "GET", |
|
"name": "Twitter User Info", |
|
"description": "Get information about a Twitter user by screenname or user ID.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"rest_id": "Optional Twitter user's ID. If provided, overwrites screenname parameter." |
|
} |
|
}, |
|
"timeline": { |
|
"route": "/timeline.php", |
|
"method": "GET", |
|
"name": "User Timeline", |
|
"description": "Get tweets from a user's timeline.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"rest_id": "Optional parameter that overwrites the screenname", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"following": { |
|
"route": "/following.php", |
|
"method": "GET", |
|
"name": "User Following", |
|
"description": "Get users that a specific user follows.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"rest_id": "Optional parameter that overwrites the screenname", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"followers": { |
|
"route": "/followers.php", |
|
"method": "GET", |
|
"name": "User Followers", |
|
"description": "Get followers of a specific user.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"search": { |
|
"route": "/search.php", |
|
"method": "GET", |
|
"name": "Twitter Search", |
|
"description": "Search for tweets with a specific query.", |
|
"payload": { |
|
"query": "Search query string", |
|
"cursor": "Optional pagination cursor", |
|
"search_type": "Optional search type (e.g. 'Top')" |
|
} |
|
}, |
|
"replies": { |
|
"route": "/replies.php", |
|
"method": "GET", |
|
"name": "User Replies", |
|
"description": "Get replies made by a user.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"check_retweet": { |
|
"route": "/checkretweet.php", |
|
"method": "GET", |
|
"name": "Check Retweet", |
|
"description": "Check if a user has retweeted a specific tweet.", |
|
"payload": { |
|
"screenname": "Twitter username without the @ symbol", |
|
"tweet_id": "ID of the tweet to check" |
|
} |
|
}, |
|
"tweet": { |
|
"route": "/tweet.php", |
|
"method": "GET", |
|
"name": "Get Tweet", |
|
"description": "Get details of a specific tweet by ID.", |
|
"payload": { |
|
"id": "ID of the tweet" |
|
} |
|
}, |
|
"tweet_thread": { |
|
"route": "/tweet_thread.php", |
|
"method": "GET", |
|
"name": "Get Tweet Thread", |
|
"description": "Get a thread of tweets starting from a specific tweet ID.", |
|
"payload": { |
|
"id": "ID of the tweet", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"retweets": { |
|
"route": "/retweets.php", |
|
"method": "GET", |
|
"name": "Get Retweets", |
|
"description": "Get users who retweeted a specific tweet.", |
|
"payload": { |
|
"id": "ID of the tweet", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
}, |
|
"latest_replies": { |
|
"route": "/latest_replies.php", |
|
"method": "GET", |
|
"name": "Get Latest Replies", |
|
"description": "Get the latest replies to a specific tweet.", |
|
"payload": { |
|
"id": "ID of the tweet", |
|
"cursor": "Optional pagination cursor" |
|
} |
|
} |
|
} |
|
base_url = "https://twitter-api45.p.rapidapi.com" |
|
super().__init__(base_url, endpoints) |
|
|
|
|
|
if __name__ == "__main__": |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
tool = TwitterProvider() |
|
|
|
|
|
user_info = tool.call_endpoint( |
|
route="user_info", |
|
payload={ |
|
"screenname": "elonmusk", |
|
|
|
} |
|
) |
|
print("User Info:", user_info) |
|
|
|
|
|
timeline = tool.call_endpoint( |
|
route="timeline", |
|
payload={ |
|
"screenname": "elonmusk", |
|
|
|
} |
|
) |
|
print("Timeline:", timeline) |
|
|
|
|
|
following = tool.call_endpoint( |
|
route="following", |
|
payload={ |
|
"screenname": "elonmusk", |
|
|
|
} |
|
) |
|
print("Following:", following) |
|
|
|
|
|
followers = tool.call_endpoint( |
|
route="followers", |
|
payload={ |
|
"screenname": "elonmusk", |
|
|
|
} |
|
) |
|
print("Followers:", followers) |
|
|
|
|
|
search_results = tool.call_endpoint( |
|
route="search", |
|
payload={ |
|
"query": "cybertruck", |
|
"search_type": "Top" |
|
|
|
} |
|
) |
|
print("Search Results:", search_results) |
|
|
|
|
|
replies = tool.call_endpoint( |
|
route="replies", |
|
payload={ |
|
"screenname": "elonmusk", |
|
|
|
} |
|
) |
|
print("Replies:", replies) |
|
|
|
|
|
check_retweet = tool.call_endpoint( |
|
route="check_retweet", |
|
payload={ |
|
"screenname": "elonmusk", |
|
"tweet_id": "1671370010743263233" |
|
} |
|
) |
|
print("Check Retweet:", check_retweet) |
|
|
|
|
|
tweet = tool.call_endpoint( |
|
route="tweet", |
|
payload={ |
|
"id": "1671370010743263233" |
|
} |
|
) |
|
print("Tweet:", tweet) |
|
|
|
|
|
tweet_thread = tool.call_endpoint( |
|
route="tweet_thread", |
|
payload={ |
|
"id": "1738106896777699464", |
|
|
|
} |
|
) |
|
print("Tweet Thread:", tweet_thread) |
|
|
|
|
|
retweets = tool.call_endpoint( |
|
route="retweets", |
|
payload={ |
|
"id": "1700199139470942473", |
|
|
|
} |
|
) |
|
print("Retweets:", retweets) |
|
|
|
|
|
latest_replies = tool.call_endpoint( |
|
route="latest_replies", |
|
payload={ |
|
"id": "1738106896777699464", |
|
|
|
} |
|
) |
|
print("Latest Replies:", latest_replies) |
|
|