使用 Tweepy 检索特定对话
Retrieving specific conversations using Tweepy
我一直在尝试使用 Tweepy 检索对话线程,虽然该功能已添加到 Twitter api(conversation_id 是一个可选参数),但尚未添加到 Tweepy .我想知道是否有人对 Tweepy 足够熟悉以至于他们可能知道实现此目标的方法?
Tweepy 还不支持 API 的 v2,尽管有 plans for this in the coming year.
这是我获取 conversation_id 和下载对话的代码。希望它能帮助有类似问题的人。我只包含了所需的功能而不是整个文件,所以我没有列出请求和base64等所需的模块,但它们应该很明显。
获取不记名令牌并创建 header 的代码我从这里 with the Twitter API - how can I get authentication for the engagement endpoint using a bearer token 但为方便起见我在下面重新发布
# 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()
我一直在尝试使用 Tweepy 检索对话线程,虽然该功能已添加到 Twitter api(conversation_id 是一个可选参数),但尚未添加到 Tweepy .我想知道是否有人对 Tweepy 足够熟悉以至于他们可能知道实现此目标的方法?
Tweepy 还不支持 API 的 v2,尽管有 plans for this in the coming year.
这是我获取 conversation_id 和下载对话的代码。希望它能帮助有类似问题的人。我只包含了所需的功能而不是整个文件,所以我没有列出请求和base64等所需的模块,但它们应该很明显。
获取不记名令牌并创建 header 的代码我从这里 with the Twitter API - how can I get authentication for the engagement endpoint using a bearer token 但为方便起见我在下面重新发布
# 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()