I am really new to twitter api, and I've been trying to get a list of IDs of everyone that retweeted a specific tweet. After several attempts i can't get the 'api.get_retweeter_ids' to get every id. It always seems to get a few. I know there is a limit of 100 per request, but the function just ends there after getting around 50-90 IDs on a tweet with 30k retweets or so.
Here is my code
def get_user_ids_by_retweets(tweetid):
retweeters_ids = []
for i, _id in enumerate(tweepy.Cursor(api.get_retweeter_ids, id=tweetid).items()):
retweeters_ids.append(_id)
print(i, _id)
df = pd.DataFrame(retweeters_ids)
# print(df)
return retweeters_ids
https://twitter.com/Nike/status/1582388225049780230/retweets
import tweepy
import json
def get_user_ids_by_retweets(tweet_id):
# get client with token
bearer_token ="*************************"
client = tweepy.Client(bearer_token=bearer_token)
listUser = []
# get first paging retweet users
retweeters = client.get_retweeters(id=tweet_id)
for retweeter in retweeters.data:
listUser.append({
"name": retweeter.name,
"id": retweeter.id,
"username": retweeter.username
})
next_token = retweeters.meta['next_token']
# get til end of paging retweet users
while next_token != None:
retweeters = client.get_retweeters(id=tweet_id, pagination_token=next_token)
if retweeters.data is not None:
for retweeter in retweeters.data:
listUser.append({
"name": retweeter.name,
"id": retweeter.id,
"username": retweeter.username
})
next_token = retweeters.meta['next_token']
else:
next_token = None
return listUser
def obj_dict(obj):
return obj.__dict__
tweet_id="1582388225049780230"
listUser = get_user_ids_by_retweets(tweet_id)
print(json.dumps(listUser, indent=4, default=obj_dict))
[
{
"name": "valmig",
"id": 1594136795905593344,
"username": "AngelVa00615402"
},
{
"name": "Wyatt Jones",
"id": 764734669434871808,
"username": "TheGhostZeus"
},
{
"name": "Prime Projects",
"id": 1603887705242435584,
"username": "PrimeProjects4"
},
... removed
{
"name": "Ryan Maldonado",
"id": 1419009007688224768,
"username": "RyanMal87509518"
},
{
"name": "Jimmy Daugherty",
"id": 20888017,
"username": "JimmyDaugherty"
},
{
"name": "Nike Basketball",
"id": 5885732,
"username": "nikebasketball"
}
]
Get tweeter API return limited number of tweeters with next_token
.
It can be next paging's tweeter by assign to pagination_token
.
It can be all of tweeter until 'next_token` is null.
So #1 and #2 get two tweeters with next_token
, those sum tweeters are same as #3 tweeters.
import tweepy
bearer_token ="*************************"
client = tweepy.Client(bearer_token=bearer_token)
tweet_id="1582388225049780230"
print("#1 -------- Get first two tweeter -------------------------")
retweeters = client.get_retweeters(id=tweet_id, max_results=2)
print("#2 -------- Show Meta --------------------")
print(retweeters.meta)
print(" ")
print("#3 -------- print two -------------------------")
for retweeter in retweeters.data:
print(retweeter.name, " -> ",retweeter.id,",",retweeter.username)
print(" ")
print("#4 ---------Get Next two tweeter ---------------------------")
retweeters = client.get_retweeters(id=tweet_id, pagination_token=retweeters.meta['next_token'] ,max_results=2)
print(retweeters.meta)
print(" ")
print("#5 -------- print two -------------------------")
for retweeter in retweeters.data:
print(retweeter.name, " -> ",retweeter.id,",",retweeter.username)
print(" ")
print("#6 --- Get First four tweeter == are same #1 + #2 ---------")
retweeters = client.get_retweeters(id=tweet_id, max_results=4)
print(" ")
print("#7 -------- print four -------------------------")
for retweeter in retweeters.data:
print(retweeter.name, " -> ",retweeter.id,",",retweeter.username)
$ python retweet.py
#1 -------- Get first two tweeter -------------------------
#2 -------- Show Meta --------------------
{'result_count': 2, 'next_token': '7140dibdnow9c7btw4827c3yb0pfg7mg4qq12dn59ot9s'}
#3 -------- print two -------------------------
valmig -> 1594136795905593344 , AngelVa00615402
Wyatt Jones -> 764734669434871808 , TheGhostZeus
#4 ---------Get Next two tweeter ---------------------------
{'result_count': 2, 'next_token': '7140dibdnow9c7btw4827c3nilr9nqckqkuxdzj3u7pkn', 'previous_token': '77qpymm88g5h9vqkluxdnrmaxhecakrtbzn80cd5hizht'}
#5 -------- print two -------------------------
Prime Projects -> 1603887705242435584 , PrimeProjects4
Joshua Paul Hudson -> 847275330 , JoshswiftJoshua
#6 --- Get First four tweeter == are same #1 + #2 ---------
#7 -------- print four -------------------------
valmig -> 1594136795905593344 , AngelVa00615402
Wyatt Jones -> 764734669434871808 , TheGhostZeus
Prime Projects -> 1603887705242435584 , PrimeProjects4
Joshua Paul Hudson -> 847275330 , JoshswiftJoshua