How to use from parameter in search api of Tweepy - python

I am using tweepy python library for fetching data from twitter. I want to get tweet from user1 tweeted to user3 containing word1, word2, #hashText1 etc.
query = "word1 word2 #hashtext1"
max_tweets = 10
searched_tweets = [status for status in tweepy.Cursor(api.search, q=query, from='user2' to='user2').items(max_tweets)]
The problem I am facing is that I am not able to use from in the function as it is keyword in python. When removed from parameter query works fine.
I have looked into source code of the tweepy here. It says from as valid parameter.
Please help.

Related

python tweeter api search any tweets

I'd like to search tweet without a matching word but only with a condition. i.e. search all tweet that has hastags. I'm using twitter api according to the doc here. I built my query like so:
endpoint = "https://api.twitter.com/2/tweets/search/all?query=has%3Ahashtags"
And I received an error:
"message": "There were errors processing your request: is/has/lang cannot be used as a standalone operator (at position 1)"
How do I search query any tweets that contains a hashtag?
Thanks.

Tweepy lookup of extended tweets for multiple tweets at a time?

I'm using tweepy to access a large number of tweets. Many tweets are truncated, so I want to get the full text of some tweets, which I have the id for.
My problem is: The tweepy api instance has one method of downloading multiple tweets at once (api.statuses_lookup), but this returns truncated tweets.
It also has a method that includes the full tweet text (api.get_status), but which afaik only takes one tweet at a time.
Is there way of getting the full text for multiple tweets at once?
import tweepy
consumer_key = "XXX"
secret = "XXX"
auth = tweepy.AppAuthHandler(consumer_key, secret)
auth.secure = True
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
ids = [1108360183586140161, 1108474125486641153]
# Finds tweets (up to 100 at a time), but doesn't contain extended text
foo = api.statuses_lookup(ids)
# Returns tweet, including extended text, but only for one at a time
bar = api.get_status(1108449077937635328, tweet_mode='extended')
As pointed out by Andy Piper, the issue was fixed in a recent update of the Tweepy library, so running
pip install tweepy --upgrade
solves this.

How to get many returned tweets from Twitter search API

I'm looking into the Twitter Search API, and apparently, it has a count parameter that determines "The number of tweets to return per page, up to a maximum of 100." What does "per page" mean, if I'm for example running a python script like this:
import twitter #python-twitter package
api = twitter.Api(consumer_key="mykey",
consumer_secret="mysecret",
access_token_key="myaccess",
access_token_secret="myaccesssecret")
results = api.GetSearch(raw_query="q=%23myHashtag&geocode=59.347937,18.072433,5km")
print(len(results))
This will only give me 15 tweets in results. I want more, preferably all tweets, if possible. So what should I do? Is there a "next page" option? Can't I just specify the search query in a way that gives me all tweets at once? Or if the number of tweets is too large, some maximum number of tweets?
Tweepy has a Cursor object that works like this:
for tweet in tweepy.Cursor(api.search, q="#myHashtag&geocode=59.347937,18.072433,5km", lang='en', tweet_mode='extended').items():
# handle tweets here
You can find more info in the Tweepy Cursor docs.
With TwitterAPI you would access pages this way:
pager = TwitterPager(api,
'search/tweets',
{'q':'#myHashtag', 'geocode':'59.347937,18.072433,5km'})
for item in pager.get_iterator():
print(item['text'] if 'text' in item else item)
A complete example is here: https://github.com/geduldig/TwitterAPI/blob/master/examples/page_tweets.py

how to return all tweets without providing a search query

I am working on a text mining project that deals with analyzing tweets.
I would like to get all tweets tweeted in english during a short period of time(not more than a day), without having the result matching to a specific search query, trend, or user criteria.
I am familiar with tweepy's api.search function. However, when i try to run it without a search query:
api.search(count=remaining_tweets, since_id=str(since_id),lang='en', max_id=str(max_id-1))
i get the following message: "exception raised, waiting 15 minutes".
You can get all the tweet by giving * to the query parameter:
tweets = api.search(q='*', lang='en', count=200, since_id=since_id, max_id=max_id)
If you need a specific number of tweets, you could use tweepy Cursor like this:
# Get 1000 English tweets from max_id
tweets = [tweet for tweet in tweepy.Cursor(
api.search, q='*', lang='en', count=200, max_id=max_id).items(1000)]

How to use Tweepy to retweet with a comment

So i am stuck trying to figure out how to retweet a tweet with a comment, this was added to twitter recently.
this is when you click retweet and add a comment to the retweet and retweet it.
basically this is what i am talking about :
i was looking at the api and count find a method dedicated to this. And even the retweet method does not have a parameter where i can pass text.
So i was wondering is there a way to do this?
Tweepy doesn't have functionality to retweet with your own text, but what you can do is make a url like this https://twitter.com/<user_displayname>/status/<tweet_id> and include it with the text you want comment. It's not a retweet but you are embedding the tweet in your new tweet.
user_displayname - display name of person, whose tweet you are retweeting
tweet_id - tweet id of tweet you are retweeting
September 2021 Update
Tweepy does have the functionality to quote retweet. Just provide the url of the tweet you want to quote into attachment_url of the API.update_status method.
Python example:
# Get the tweet you want to quote
tweet_to_quote_url="https://twitter.com/andypiper/status/903615884664725505"
# Quote it in a new status
api.update_status("text", attachment_url=tweet_to_quote_url)
# Done!
In the documentation, there is a quote_tweet_id parameter in create_tweet method.
You can create a new tweet with the tweet ID of the tweet you want to quote.
comment = "Yep!"
quote_tweet = 1592447141720780803
client = tweepy.Client(bearer_token=access_token)
client.create_tweet(text=comment, quote_tweet_id=quote_tweet, user_auth=False)

Categories

Resources