Skip to content

Instantly share code, notes, and snippets.

@qqpann
Last active December 7, 2020 15:45
Show Gist options
  • Save qqpann/b2fe310287022af1fc32132ecb82ec22 to your computer and use it in GitHub Desktop.
Save qqpann/b2fe310287022af1fc32132ecb82ec22 to your computer and use it in GitHub Desktop.
[Get Tweet Replies] Get Tweet Replies with Twitter API #twitter #python #tweepy
# Author: Qiushi Pan (@qqhann)
# This is a code snippet with sample usage, to get replies to a specific tweet.
# =====
import tweepy
import time
import os
from collections import defaultdict
from dotenv import load_dotenv, find_dotenv
env = load_dotenv(find_dotenv(), override=True)
# Save your tokens in .env file
con_key = os.getenv('TWITTER_API_KEY')
con_sec = os.getenv('TWITTER_API_SECRET_KEY')
acc_tok = os.getenv('TWITTER_ACCESS_TOKEN')
acc_sec = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
# Initialize tweepy
auth = tweepy.OAuthHandler(con_key, con_sec)
auth.set_access_token(acc_tok, acc_sec)
api = tweepy.API(auth)
# Sample teets.
# Tweet 1 (refer as T1)
# https://twitter.com/bozu_108/status/1090475718335578112
# Tweet 2 (refer as T2)
# https://twitter.com/bozu_108/status/1090501639625072640
# In this sample, we try to get replies to T1,
# since T1 until T2.
# Preparation
TARGET_TWEET_ID = 1090475718335578112 # T1
oldest_id = 1090501639625072640 # T2
search_term = '@bozu_108' # Target tweet's user name
no_match, not_reply, match = 'No Match', 'Not Reply', 'Match'
counts = {no_match: 0, not_reply: 0, match: 0}
matched_texts = defaultdict(int)
# Repeat the api multiple times to try to get all replies
# You may want to change the repetition times, or stop it mannualy (if using jupyter notebook)
for i in range(1000):
print('loop', i, counts)
time.sleep(5) # API Limit: 180req / 15min(900sec)
result_tweets = api.search(search_term, count=100, since_id=TARGET_TWEET_ID, max_id=oldest_id)
for tweet in result_tweets:
# Reflesh oldest_id
oldest_id = min(tweet.id, oldest_id)
if tweet.in_reply_to_status_id == TARGET_TWEET_ID:
counts[match] += 1
matched_texts[tweet.text] += 1
elif tweet.in_reply_to_status_id is None:
counts[not_reply] += 1
else:
counts[no_match] += 1
# Show results
for text, count in sorted(matched_texts.items(), key=lambda x: x[1], reverse=True):
print('{:>4}\t{}'.format(count, text))
# sample results output (top 5) =>
# 8 @bozu_108 じゃんけん
# 7 @bozu_108 スペランカー
# 6 @bozu_108 MOTHERシリーズ
# 6 @bozu_108 Vainglory
# 5 @bozu_108 学園ハンサム
@girish783
Copy link

@qqhann Thank you for the code. Even I am facing the same issue.

loop 0 {'No Match': 0, 'Not Reply': 0, 'Match': 0}
Traceback (most recent call last):

File "", line 4, in
result_tweets = api.search(search_term, count=100, since_id=TARGET_TWEET_ID, max_id=oldest_id)

File "F:\Python\lib\site-packages\tweepy\binder.py", line 250, in _call
return method.execute()

File "F:\Python\lib\site-packages\tweepy\binder.py", line 192, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])

File "F:\Python\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb)

File "F:\Python\lib\site-packages\tweepy\binder.py", line 190, in execute
proxies=self.api.proxy)

File "F:\Python\lib\site-packages\requests\sessions.py", line 498, in request
prep = self.prepare_request(req)

File "F:\Python\lib\site-packages\requests\sessions.py", line 441, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),

File "F:\Python\lib\site-packages\requests\models.py", line 313, in prepare
self.prepare_auth(auth, url)

File "F:\Python\lib\site-packages\requests\models.py", line 544, in prepare_auth
r = auth(self)

File "F:\Python\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 88, in call
unicode(r.url), unicode(r.method), None, r.headers)

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849_init_.py", line 314, in sign
('oauth_signature', self.get_oauth_signature(request)))

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849_init_.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849_init_.py", line 211, in _render
request.oauth_params, request.headers, realm=realm)

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)

File "F:\Python\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
'Got %r of type %s.' % (u, type(u)))

TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.

@qqpann
Copy link
Author

qqpann commented Mar 29, 2019

@girish783 Hi
Have you set up your credentials correctly?
The error says you have NoneType somewhere. Please debug what you are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment