Why is Tweepy saying a retweet is a quote? - python

Here's a simplified version of the code to reproduce the problem:
import tweepy
from tweepy import OAuthHandler
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_secret = '...'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
tweepy_api = tweepy.API(auth)
status_id = 999280226994671616
status = tweepy_api.get_status(status_id, tweet_mode='extended')
print(status.user.screen_name) # Prints 'VP' as it should
if status.retweeted: # Is false, even though it's a retweet
print('RETWEET:', status.retweeted_status.full_text)
elif status.is_quote_status: # Is true
qstatus = tweepy_api.get_status(status.quoted_status_id, tweet_mode='extended')
print(status.full_text, '\nQUOTE:', qstatus.full_text) # Doesn't print full text for either status
else:
print(status.full_text)
I'm wondering if the reason is_quote_status is true is because the retweet contains a quote. How do I detect whether the status is a retweet, then?
This is a secondary problem, but as my comments point out, it's also not displaying the full text (it does just fine when the status isn't a retweet or quote). What am I doing wrong?

A retweet is a tweet, and a tweet may quote another tweet, so a retweet can quote a tweet.
In your case :
The tweet id 999280226994671616 is from #vp.
It is a retweet of the tweet id 999275458796687360 from #SecondLady (https://twitter.com/SecondLady/status/999275458796687360).
And this tweet is quoting the tweet id 999013390528937991 from #SenEvanBayh (https://twitter.com/SenEvanBayh/status/999013390528937991)
To know if a tweet is a RT, check if the status has the attribute retweeted_status.
This :
status_id = 999280226994671616
status = tweepy_api.get_status(status_id, tweet_mode='extended')
print ('status.id_str:' + status.id_str)
print ('status.user.screen_name:#' + status.user.screen_name)
print()
if hasattr(status, 'retweeted_status'):
print ('status.retweeted_status.user.screen_name:' + status.retweeted_status.user.screen_name)
print ('status.retweeted_status.id_str:' + status.retweeted_status.id_str)
print()
qstatus = tweepy_api.get_status(status.retweeted_status.quoted_status_id, tweet_mode='extended')
print ('qstatus.id_str:' + qstatus.id_str)
print ('qstatus.user.screen_name:#' + qstatus.user.screen_name)
print()
will print :
status.id_str:999280226994671616
status.user.screen_name:#VP
status.retweeted_status.user.screen_name:SecondLady
status.retweeted_status.id_str:999275458796687360
qstatus.id_str:999013390528937991
qstatus.user.screen_name:#SenEvanBayh
Hope it helps.

Related

There is a problem using "since" in Tweety function to extract Covaxin related hashtag tweets from the starting time of Covid'19

It is saying unexpected parameter :since when running the below code
import tweepy
# Enter your own credentials obtained
# from your developer account
consumer_key = "wwww"
consumer_secret = "xxxx"
access_key = "yyyy"
access_secret = "zzzz"
# The above keys are mentioned correctly in the programming code
# Twitter authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# Creating an API object
api = tweepy.API(auth)
hashtag_tweets = tweepy.Cursor(api.search_tweets, q="#Covaxin", tweet_mode='extended').items(5)
date_tweets = tweepy.Cursor(api.search_tweets, q="#Covaxin", since="2020-03-31", until="2022-01-09",tweet_mode='extended').items(5)
list = []
for tweet in date_tweets:
text = tweet._json["full_text"]
#print(text)
refined_tweet = {'text' : text,
'favorite_count' : tweet.favorite_count,
'retweet_count' : tweet.retweet_count,
'created_at' : tweet.created_at}
list.append(refined_tweet)
#print(list)
import pandas as pd
df = pd.DataFrame(list)
print(df)
df.to_csv('refined_tweets.csv')
It is saying unexpected parameter :since when running the code
I was trying to get the output for all tweets satisfying date query for the particular hashtag Covaxin from the starting of Covid days till now.

How to specify more than one coordinates for geocode parameter of API.search in Tweepy

I want to search tweets based on more than one coordinates. So, I tried this but it doesn't return any results:
total = 0
for status in tweepy.Cursor(api.search, q='cricket', lang="en",
geocode="24.8607,67.0011,25mi OR 40.7128,74.0060,20mi"
).items(10):
total+=1
print(total)
It's been a while since #Tayyap Mazhar shared the post, but just in case, the code below will work as expected. Just remember, do not put comma while declaring geoc variable!
import tweepy
import pandas as pd
CONSUMER_KEY = "?"
CONSUMER_SECRET = "?"
OAUTH_TOKEN = "?"
OAUTH_TOKEN_SECRET = "?"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
auth.secure = True
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
if (not api):
print ("Can’t Authenticate")
sys.exit(-1)
tweet_lst=[]
geoc=[('41.0,28.9499962,1km'),('41.1062629083,29.0264182277,1km'),('41.072833042,29.022833242,1km'),('41.05,28.91,1km')]
for geocode in geoc:
for tweet in tweepy.Cursor(api.search,geocode=geocode).items(1000):
tweetDate = tweet.created_at.date()
if(tweet.coordinates !=None):
tweet_lst.append([tweetDate,tweet.id,tweet.
coordinates['coordinates'][0],
tweet.coordinates['coordinates'][1],
tweet.user.screen_name,
tweet.user.name, tweet.text,
tweet.user._json['geo_enabled']])
tweet_df = pd.DataFrame(tweet_lst, columns=['tweet_dt', 'id', 'long','lat','username', 'name', 'tweet','geo'])```

Using Twython or Tweepy to check if a Tweet is a reply?

Is there a way, given a tweet ID, to check if a tweet is a reply rather than an original tweet? If so, is there a way to get the ID of the tweet that the original tweet is in reply to?
Looking at Twitter Documentation you see that a tweet object has
in_reply_to_status_id
Nullable. If the represented Tweet is a reply, this field will contain the integer representation of the original Tweet’s ID.
Example: "in_reply_to_status_id":114749583439036416
Using tweepy you can do something like this:
user_tweets = constants.api.user_timeline(user_id=user_id, count=100)
for tweet in user_tweets:
if tweet.in_reply_to_status_id is not None:
# Tweet is a reply
is_reply = True
else:
# Tweet is not a reply
is_reply = False
If you're looking for a specific tweet and you have the id then you want to use get_status like this:
tweet = constants.api.get_status(tweet_id)
if tweet.in_reply_to_status_id is not None:
# Tweet is a reply
is_reply = True
else:
# Tweet is not a reply
is_reply = False
Where api is:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

I want to have all the tweets of a userid not the latest 200 from twitter in python

I am using the python-twitter module to get the tweets of my friends. I used the following code:
import twitter
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
count = 0
for tweets in twitter_api.statuses.user_timeline(screen_name="thekiranbedi",count=500):
try:
print tweets['text']
count += 1
except Exception:
pass
print count
But as the result says, the value of count remains 200, so I am getting only the 200 recent tweets from the id with screen_name='thekiranbedi. But I want all the tweets. How can that be done?
That is a limitation of Twitter API, not of python-twitter module:
https://dev.twitter.com/rest/reference/get/statuses/user_timeline
count - Specifies the number of tweets to try and retrieve, up to a maximum of 200 per distinct request.
So as I understood you have to use 'since_id' and 'max_id' arguments to collect next portion of tweets.

Python tweepy find user of search

import time
import tweepy
from auth import consumer_key, consumer_secret, access_token, access_token_secret
string_to_search = 'new car ideas'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
past_status_ids = set()
while True:
tweets = api.search(q=string_to_search)
tweet_id_list = set([tweet.id for tweet in tweets])
new_tweet_ids = tweet_id_list - past_status_ids
past_status_ids = tweet_id_list | past_status_ids
for tweet_id in new_tweet_ids:
print "Retweeting " + str(tweet_id)
api.retweet(tweet_id)
#username = tweets.user.screen_name
#api.create_friendship(username)
#print "Followed " + str(username)
limits = api.rate_limit_status()
remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
print("Limit left is " + str(remain_search_limits))
print("")
time.sleep(150)
In a nutshell I am trying to find a string, then take that string retweet on my feed and follow that person.
My issue is how am I to find the Twitter ID to follow them via the search string?
I have been searching all around and can't find too many examples in Python tweepy for this.
If you want the User ID of the user who tweeted the tweet with status id tweet_id.
Use this
tweet = api.get_status(tweet_id)
user_id = tweet.user.id
# Now follow that user
api.create_friendship(user_id)

Categories