Search code examples
pythontwittertweepy

Retrieving specific conversations using Tweepy


I have been trying to retrieve conversation threads using Tweepy, and although the functionality has been added to the Twitter api (conversation_id is an optional parameter), it has not been added to Tweepy. I was wondering if anyone was familiar enough with Tweepy that they might know a way to achieve this?


Solution

  • Here is my code to get the conversation_id and also to download the conversations. Hopefully it helps people with similar issues. I have only included the required functions not the whole files, so I haven't listed the modules required like requests and base64, but they should be quite obvious.

    The code to get the bearer token and create the header I got from here with the Twitter API - how can I get authentication for the engagement endpoint using a bearer token but I reposted below for convenience

    # returns a bearer_header to attach to requests to the Twitter api v2 enpoints which are 
    # not yet supported by tweepy 
    def get_bearer_header():
       uri_token_endpoint = 'https://api.twitter.com/oauth2/token'
       key_secret = f"{twitter_creds.consumer_key}:{twitter_creds.consumer_key_secret}".encode('ascii')
       b64_encoded_key = base64.b64encode(key_secret)
       b64_encoded_key = b64_encoded_key.decode('ascii')
    
       auth_headers = {
           'Authorization': 'Basic {}'.format(b64_encoded_key),
           'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
           }
    
       auth_data = {
           'grant_type': 'client_credentials'
           }
    
       auth_resp = requests.post(uri_token_endpoint, headers=auth_headers, data=auth_data)
       bearer_token = auth_resp.json()['access_token']
    
       bearer_header = {
           'Accept-Encoding': 'gzip',
           'Authorization': 'Bearer {}'.format(bearer_token),
           'oauth_consumer_key': twitter_creds.consumer_key 
       }
       return bearer_header
    
    # Returns the conversation_id of a tweet from v2 endpoint using the tweet id
    def getConversationId(id):
       uri = 'https://api.twitter.com/2/tweets?'
    
       params = {
           'ids':id,
           'tweet.fields':'conversation_id'
       }
       
       bearer_header = get_bearer_header()
       resp = requests.get(uri, headers=bearer_header, params=params)
       return resp.json()['data'][0]['conversation_id']
    
    # Returns a conversation from the v2 enpoint  of type [<original_tweet_text>, <[replies]>]
    def getConversation(conversation_id):
       uri = 'https://api.twitter.com/2/tweets/search/recent?'
    
       params = {'query': f'conversation_id:{conversation_id}',
           'tweet.fields': 'in_reply_to_user_id', 
           'tweet.fields':'conversation_id'
       }
       
       bearer_header = twitter_auth.get_bearer_header()
       resp = requests.get(uri, headers=bearer_header, params=params)
       return resp.json()