I need to only get the followers I have not fetched before. Currently I can only get the top 200 items or given count but I don't want to get the same data more than once.
The only way I know how is to cycle through them and follow them if they haven't already been followed. I don't believe it's possible by looking at the API:
https://www.geeksforgeeks.org/python-api-followers-in-tweepy/
Make sure you have the third line for the API:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit = True)
Here's my snippet for following:
followers = tweepy.Cursor(api.get_followers).items()
for follower in followers:
if follower.id not in friends:
user = api.get_user(follower.id)
follower.follow()
followers = tweepy.Cursor(api.get_followers).items()
I just discovered that .items() can be passed a number. So you could do something like:
followers = tweepy.Cursor(api.get_followers).items(50)
Additionally, looking at the API documentation, API.get_followers() method, you can also set the number of followers to go through by passing a value to the count variable.
API.get_followers(*, user_id, screen_name, cursor, count, skip_status, include_user_entities)
API.get_followers(count=50)
The followers are returned in the number that they were added.
Related
I have the Academic Research access to Twitter's API and have been using Tweepy to access the API. My problem is I cannot retrieve the tweets from older tweets
This is the code attempting to retrieve the tweets using the conversation_id, from 2014
# https://twitter.com/NintendoAmerica/status/535462600294035456
start_time = '2014-11-01T00:00:00Z'
end_time = '2014-12-12T00:00:00Z'
tweets = client.search_all_tweets(query = 'conversation_id:535462600294035456', max_results = 500, start_time=start_time, end_time=end_time)
and the output is:
Response(data=[<Tweet id=535465221679489024 text='#NintendoAmerica #Pokemon [this was a link I had to remove]'>], includes={}, errors=[], meta={'newest_id': '535465221679489024', 'oldest_id': '535465221679489024', 'result_count': 1})
which is only one seemingly random tweet amongst many.
However, when I tried running the same code on a more recent tweet, it retrieved all the tweets. I do not have to specify a start/end time because it's a tweet from the past 30 days.
# https://twitter.com/380kmh/status/1545477360916373504
tweets = client.search_all_tweets(query = 'conversation_id:1545477360916373504', max_results = 500)
the output was complete (shortened, I removed the tweets):
Response(data=[...], meta={'newest_id': '1546465585093087235', 'oldest_id': '1545477768229670912', 'result_count': 18})
I followed Tweepy's documentation here, using Client.search_all_tweets:
https://docs.tweepy.org/en/stable/client.html#tweepy.Client.search_all_tweets
I also tried using Postman to retrieve the tweets but it came out empty, even though I followed the documentation here:
https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all#tab0
Query: https://api.twitter.com/2/tweets/search/all?query=conversation_id%3A537923834557771776&start_time=2014-11-01T00:00:00.000Z&end_time=2014-12-18T00:00:00.000Z&tweet.fields=in_reply_to_user_id,text
Output:
{
"meta": {
"result_count": 0
}
}
What am I doing wrong?
I'm trying to crawl comments of a given videoId with youtube API.
But the number of crawled comments is less than its actual number.
Do you have any idea about this? My code is like the below.
from googleapiclient.discovery import build
from typing import List
def get_comments(api, video_id: str, fields: str)-> List[List[str]]:
comments = list()
response = api.commentThreads().list(part='snippet', fields=fields, videoId=video_id, maxResults=50).execute()
all_comment_crawled = True
while all_comment_crawled:
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']
comments.append([comment['textOriginal'], comment['likeCount']])
if 'nextPageToken' in response:
response = api.commentThreads().list(part='snippet', videoId=video_id, fields=fields, pageToken=response['nextPageToken'], maxResults=50).execute()
else:
all_comment_crawled = False
return comments
api_key = "MY_API_KEY"
api_obj = build('youtube', 'v3', developerKey=api_key)
video_id = 'fgSvGLxanCo'
fields = 'items(snippet(totalReplyCount, topLevelComment(snippet(textOriginal, likeCount)))), nextPageToken'
comments = get_comments(api_obj, video_id, fields)
print(len(comments)) # returns 1,945 actually is over 2,000
There is a trap (and I announce another trap that it is easy to fall into) when listing comments on a YouTube video:
The comments count on YouTube counts all (not filtered) comments. The replies are included in this count and you haven't considered them in your algorithm. Have a look to CommentThreads: list
The replies when using commentThreads are given up to 5 replies.
If the message have more than 5 replies you have to use Comments: list to list them all.
I am using this rails gem to access the Mailchimp API.
https://bitbucket.org/mailchimp/mailchimp-api-ruby
mailchimp = Mailchimp::API.new(ENV['MAILCHIMP-API-KEY'])
My list has 59 members and I can confirm that from
newsletter_members = mailchimp.lists.members(listID)['total']
but when I try to access the data hash, it only returns 25 objects?
newsletter_members = mailchimp.lists.members(listID)['data'].size
Any ideas as to why?
I am basically trying to find if a email exists in my mail chimp list and this code is breaking because I am not getting all the members
mailchimp = Mailchimp::API.new(ENV['MAILCHIMP-API-KEY'])
listID = 'my-list-id'
email_array = []
newsletter_members = mailchimp.lists.members(listID)['data']
# the email array is cut short..not getting all members
newsletter_members.each do |member|
email_array << member['email']
end
#returns true or false
member_exists = email_array.include?(user_email)
Mailchimp's API defaults to return 25 items at a time. You can request more (though the limit is 100), or you can make multiple requests for "pages" of results.
To request 100 results:
mailchimp.lists.members(listID, 'subscribed', {limit: 100})
To request the next page of results (keep in mind the first page (results 1-25) is 0, second (26-50) is 1, etc.):
mailchimp.lists.members(listID, 'subscribed', {start: 1}) # Then to get 51-75 you'd start at 2
See the source code for an overview of the available options.
You can also look at Mailchimp's API docs for the lists/members endpoint to see available options, defaults, etc.
I am using twitter_timeline to get the user details.
It provides set of tweets including RTs. I am considering on retweets from all tweets.
Suppose I retweeted any tweet, which I can get using:
$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?trim_user=true&include_rts=true");
foreach ($tweets3 as $item)
{
$rt_reach = $item->retweet_count; //This is available
$text = $item->text; //This is available
$follower_count = $item->user->followers_count; //This is not available
echo "User location $item->user->location"; //This is not available
echo $follower_count = $item->user->screen_name; //This is not available
}
Link to document: https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
Why it does not provide last three value in above code?
Since you're using "trim_user=true", twitter strips the user record except for the user_id.
Please check trim_user parameter here.
The "trim_user" param is used by applications to stop the tweets data from getting bloated and it should be excluded if the app needs the full user record, which seems to be the case for you.
I could get users retweets and mention, but could not figure out how to get result for one month.
https://api.twitter.com/1.1/statuses/mentions_timeline.json
And
I am using php to find the number of retweets a status has gotten.
this is my current code:
//get account info
$connection->request('GET',$connection->url('1.1/statuses/retweets'),
array('id'=> '363332903113351168'));
//get http response code for request
$response_code = $connection->response['code'];
//convert the json response to an array
$response_data = json_decode($connection->response['response'],true);
if($code != 200)
{
print"ERROR: $response_code\n";
}
print_r($response_data);
To get mention and retweets.
Can someone tell me how retweet n mention result can shown for one month
Try impliment this logic.
First to make filter by ID of tweets and next step filter by "created_at" field. After you will can retweets resalt.