Get all tweets from a user using python-twitter - python

I'm trying to retrieve all the tweets from a user in python with this lib.
The method provided in python-twitter to do this is defined as :
def GetUserTimeline(self,
user_id=None,
screen_name=None,
since_id=None,
max_id=None,
count=None,
include_rts=None,
trim_user=None,
exclude_replies=None):
Though if the count is limited to 200.
Documentation:
count:
Specifies the number of statuses to retrieve. May not be
greater than 200. [Optional]
My question now is: Is it possible to retrieve all the tweets from a user with this lib ? And is there an alternative solution ?

Short answer: no.
Twitter API explicitly says that you cannot retrieve more then 200 tweets from a user per request so this is not a limitation of this particular library you're using but a limitation imposed by Twitter. But keep in mind that this is per request, if you will send multiple requests you can get up to 3200 tweets in total. But this still means that you can't get all tweets, only 3200.
Here is official Twitter API statuses/user_timeline endpoint documentation.

Related

get replys to a specific tweet with twitter api v2

anyone knows how to get a the relys for a specific tweet by it's ID maybe? i been trying but can't find any on the tweepy docs not discord's, can't seem to find anything about how to do it with twitter api v2, also is there a way to requests latest tweets from multiple users in bulk?
import tweepy
twitter_client = tweepy.Client(bearer_token=bearer)
all_tweets = twitter_client.get_users_tweets(id=(1334299956241461248,1346973288040263680,154449778,1516418305455763466))
print(all_tweets)
this is how i try it, but it's returning me an error
The `id` query parameter value [(1334299956241461248, 1346973288040263680, 154449778, 1516418305455763466)] is not valid
any help appreiciated, and thank you
and i found the answer at the end
def check_replys(tweet_ID):
query = f"conversation_id:{tweet_ID} is:reply"
replys= twitter_client.search_recent_tweets(query= query )
return replys
you can find more info about making a query at https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query
You're asking two questions:
How to get replies by Tweet ID
How to lookup the latest Tweets for several users
For 1 - you can search for Tweets by conversation ID. The conversation ID is the ID of the original Tweet that led to the replies. The only wrinkle here is that by default (v2 Essential access) you can only search for Tweets within the past 7 days, so if the Tweet you want replies for is older than that, you'll need Academic access for full archive search.
For 2 - you cannot pass multiple values to the id parameter all at once. If you look at the Tweepy documentation and at the Twitter API docs, you'll see that id gets substituted into the URL path for the call, so you have to call the API multiple times, one for each user ID. That should be possible using a loop.

How to get tweets in real time from a user's timeline using Tweepy

I'm trying to pull tweets from a user's timeline in real-time. I then want to do some analysis on those tweets. Having read the docs it looks like I will need to use tweepy.Stream for this use case. I've done the following:
stream.filter(follow='25073877')
But Twitter's filter API states the following:
Tweets created by the user.
Tweets which are retweeted by the user.
Replies to any Tweet created by the user.
Retweets of any Tweet created by the user.
Manual replies, created without pressing a reply
button (e.g. “#twitterapi I agree”).
It seems that this will return a huge volume of tweets that aren't relevant to my use case. Do I have to use this approach and then filter by screen name to get only tweets by the real user? This doesn't seem right at all.
The alternative seems to be the api.user_timeline class but that isn't a streaming API. Do I therefore use this API and hit it every second? I can't seem to find suitable examples of how best to accomplish my use case.
Yes, you'll need to filter either by screen_name or maybe you can check if it's a retweet or not.
I wouldn't recommend the second approach since you'll be getting an even bigger amount of tweets since you'll have to filter out the tweets you already received in previous requests plus you may hit the API querying limits if you don't time ti properly.
That's the signature of the filter function:
def filter(self, follow=None, track=None, is_async=False, locations=None,
stall_warnings=False, languages=None, encoding='utf8', filter_level=None)
Which maps to this Twitter API request.
And here the explanation of the parameters.

How can I get only responses tweets?

I need to retrieve specific data from twitter.
I'd like to get all the responses tweets received by a specific user (which is not the authenticating user of the program). Is there a way to achieve this? Right now I'm thinking about using the search function and see if the 'in_reply_to_user_id_str' matches the id of the user I want.
But this means that I need to filter a lot of data to find the one I want
Edit: I'm using the Python-Twitter-Tools
If it is the authenticating user, you can directly get the response tweets using the 'mentions timeline'. As the user is not the authenticating user, you have two options here.
Streaming API
Use the filter endpoint along with the 'follow' parameter. Pass the required 'user_id' to the follow paramenter and it will return the followings. You will have to check the 'in_reply_to_user_id_str' in order to isolate the replies(responses).
Tweets created by the user.
Tweets which are retweeted by the user.
Replies to any Tweet created by the user.
Retweets of any Tweet created by the user.
Manual replies, created without pressing a reply button.
Python-Twitter-Tools supports Streaming API. Streaming API is realtime and better than Search API considering the completeness.
Search API
Every response tweet contains the "#username" tag. You can searching using "#username" tag and then filter the tweets using 'in_reply_to_user_id_str' as you have mentioned.
Considering the two options, Streaming API will help you to get what you need easily and reliably.

Failing to pull a sampling of Twitter users

I am doing a project for which I need a fairly small number of random Twitter users (about 20 for now) with a near-identical number of followers (within 45000 - 55000 for example). I brushed up on my Python, got Twython set up, and did some test runs and successfully queried the Twitter API for the list of a users followers and such.
Then I tried to just randomly search for users. I thought that I would just search for users where (q = " "), loop through them, and stop upon reaching my goal of 20 unique users.
However, I came across an error saying "Twitter API returned a 403 (Forbidden), Tour credentials do not allow access to this resource." When looking around for the issue, it appears that it has to do with the Authentication not having a user-context.
Below is my sample code to tell if I am pulling names, but I have a few questions:
What is the difference (in Twitter's context) in Oauth 1 and 2?
for someone not looking to develop an app but rather just pull some data, why would I need a user-context? I am not posting anything on anyone's behalf!
How do I go about fixing the issue (or is there a better way of doing this)?
Thanks!
from twython import Twython
APP_KEY = 'redacted'
APP_SECRET = 'redacted'
twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()
twitter = Twython(APP_KEY,access_token=ACCESS_TOKEN)
print "hello twitterQuery\n"
search = twitter.search_users(q=' ')
for user in search['ids']:
print twitter.show_user(user_id=user)['screen_name']
print "\ndone"
I can't really answer everything you have asked, but I'll try here:-
In OAuth1 in twitter, access codes were for a lifetime, i.e., would never expire. In OAuth2, there are refresh codes, that are used for new access codes, after previous ones expire. There are many other differences, but this is what really stuck out to me in a Twitter context. You could look at these links to get more details.
How is OAuth 2 different from OAuth 1?
http://hueniverse.com/2010/05/introducing-oauth-2-0/
http://net.tutsplus.com/tutorials/oauth-2-0-the-good-the-bad-the-ugly/
This, I guess, is just Twitter trying to be more secure. I cannot think of any other reason for this.
I don't know about a solution, but maybe as a workaround, you could try and gather users from the followers list of some popular account, like Twitter's official account. You will get a wide range of users to choose from. Just retrieve the list and choose random users from it.

Count of retweets with tweepy with RESTful API?

So I am doing fairly trivial task of retrieving tweets from a particular user. What I really want to do is to count how many times each of his tweets have been retweeted. I have looked at python's tweepy's API, and I have not been able to find a clear answer. Is there a way to do this in python?
tweepy, might not have method for every endpoint in the twitter API.
tweepy should allow you to call an arbitrary endpoint, though. When in doubt, you can check out the official twitter API to see what is offered.
https://dev.twitter.com/docs/api/1.1
It appears that you might only be able to get 100 retweets for a given tweet_id
https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid
https://dev.twitter.com/docs/api/1.1/get/statuses/retweeters/ids

Categories