How to Retweet my Main twitter acc using Twython? - python

I created another twitter acc to help promote my main, so I was wondering how do you retweet ones account using twython are there any examples?
I found a few but I'm still a little confused? thanks!
What I am trying is:
user_timeline=twitter.getUserTimeline(sreen_name="slaughdaradio", count = 100,)
to tweet in user_timeline:
print tweet ['text']
It keeps giving me a syntax error for 'tweet'.

print tweet ['text']
change to
print(tweet ['text'])
I'm assume your python version is Python3. In Python3, print have changed to print().

Related

Twitter: quoted tweet has not a quoted_status nor a quoted_status_id

I am converting some tweet IDs into tweet object with twython (I use python 2.7 on ubuntu 14.04).
As you can see here, a tweet has a boolean variable is_quote_status with obvious (I guess) meaning. Also, there are the variables quoted_status and quoted_status_id. About these two variables you can find on the above link that "This field only surfaces when the Tweet is a quote Tweet" so i guess they should exist whenever is_quote_status is True.
But the first time in the dataset I find a tweet with is_quote_status is True, this is what I get:
crazy_ID = XXXXXXXXXXXXXXX
twt = twitter.show_status(id = crazy_ID)
print twt['is_quote_status']
>>True
print twt['quoted_status']
>> KeyError: 'quoted_status'
print twt['quoted_status_id']
>> KeyError: 'quoted_status_id'
and I really don't know what to think about it. A direct check (i.e. print twt) shows me that effectively is_quoted_status is True but quote_status and quote_status_id are not contained in the tweet.
Let me note that the tweet was created in 2011 and I am not even sure quoted existed at that time, but if it is the case I am still wondering why is_quoted_status is True
So here is the question: how is that possible that a tweet has is_quoted_status = True but quote_status and quote_status_id are not contained in the tweet?

Tweepy: extended mode with api.search

I've written a simple script to get the most trending 300 tweets containing a specific hashtag.
for self._tweet in tweepy.Cursor(self._api.search,q=self._screen_name,count=300, lang="en").items(300):
self._csvWriter.writerow([self._tweet.created_at, self._tweet.text.encode('utf-8')])
It works well and it save the result to CSV but the tweets are truncated.
I modified the code like this, adding the twitter_mode=extended parameter:
for self._tweet in tweepy.Cursor(self._api.search,q=self._screen_name,count=300, lang="en", tweet_mode="extended").items(300):
self._csvWriter.writerow([self._tweet.created_at, self._tweet.text.encode('utf-8')])
But I got this exception:
AttributeError: 'Status' object has no attribute 'text
My question is: how can I save an complete tweet using a Cursor? (complete = not truncated)
Thanks in advance (and sorry, I'm a Tweepy newbie trying to learn as much as possible)
You're really close, do this instead:
for self._tweet in tweepy.Cursor(self._api.search,q=self._screen_name,count=300, lang="en", tweet_mode="extended").items(300):
self._csvWriter.writerow([self._tweet.created_at, self._tweet.full_text.encode('utf-8')])
Notice that I used full_text in self._tweet.full_text.encode('utf-8'), rather than just text. The text property is null when you use tweet_mode='extended' and the tweet appears in full_text instead.

Tweepy tweets missing from search

This is an example of how my code in tweepy looks like:
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
random = random.randint(1,1000)
for tweet in tweepy.Cursor(api.search, q='twitter', lang='en', result_type='recent').items():
if not (tweet.retweeted) and 'RT #' not in (tweet.text):
api.update_status('#' + tweet.user.screen_name + ' ' + str(random) + ': test', in_reply_to_status_id = tweet.id_str)
print('Replied to the tweet!')
sleep (900)
The code works but for some reason, after a while of running the code, my tweets go missing from the search. Before it goes missing from the search, it goes missing from the tweet I replied to. I don't really know why this is happening.
The Twitter search function is optimized to show recent tweets (and probably especially so when you have the arg result_type='recent'). The tweet still exists but it is not showing up in the search because it is no longer recent enough. If you go into the twitter browser I am sure the tweets and replies are still there (navigate to the user's timeline to find it easiest). Or, try removing the result_type='recent'.
Hope this helps.
Tweets go missing from search when you are either tweeting too much or someone reports your tweets too often. Twitter is just based off of algorithms that detect if you are tweeting the same thing over and over (spamming), following / unfollowing too quickly or retweeting too much. Give it a rest it should clear after 72 hours max. I believe the term is called “shadow banned”.

tweepy: finding the original author of a retweet

I'm kind of a python newb and stuck with tweepy here.
What I'm trying to do is bring in a bunch of user and tweet objects into a neo4j database with tweet and retweet relationships. My problem is in determining if a given status object is a retweet and if so the screen_name and id_str of the original author.
I can see the data if I print out tweet.retweets but I can't figure out how to get to it. tweepy's docs mention something about object models and for more information check out ModelsReference but google isn't helping me much here.
any help would be great, even just pointing me in the right direction. Thanks
Sample code
tweets=api.get_timeling(1234556)
twitter_user=api.get_user(123456)
for tweet in tweets:
neo4j_create_tweet_node(tweet)
if tweet.user.id == twitter_user.id:
create_tweet_relationship(twitter_user,tweet)
elif tweet.user.id != twitter_user.id:
create_retweet_relationship(twitter_user,tweet)
Suppose tweet is a retweet,
originalAuthorID = tweet.retweeted_status.user.id_str;
According to the Twitter API documentation:
Retweets can be distinguished from typical Tweets by the existence of a retweeted_status attribute. This attribute contains a representation of the original Tweet that was retweeted. Note that retweets of retweets do not show representations of the intermediary retweet, but only the original tweet.
I would use hasattr() to look for the presence of the retweeted_status atttibute in each Tweetpy tweet object.
The following code (where create_tweet_relationship() and create_retweet_relationship() are functions you have defined as in your example) seems like it should would work:
for tweet in tweets:
if hasattr(tweet, 'retweeted_status'):
create_tweet_relationship(tweet.retweeted_status.author, tweet)
else:
create_retweet_relationship(tweet.author, tweet)
If the tweet is a retweet of another, the original retweeted status is included in the JSON object in the field "retweeted_status". You'd get the user information there under the "user" field.

How to get tweet IDs (since_id, max_id) in tweepy (python)?

I want to know a way of getting tweet IDs to keep a check on what tweets have been displayed in the timeline of user in the python app I am making using tweepy.
There doesn't seem to be a way I get extract the tweet IDs or keep track of them. The parameter to keep check is since_id. Please if anyone could help.
The tweepy library follows the twitter API closely. All attributes returned by that API are available on the result objects; so for status messages you need to look at the tweet object description to see they have an id parameter:
for status in api.user_timeline():
print status.id
Store the most recent id to poll for updates.
The max_id and since_id are parameters for the api.user_timeline() method.
Using the tweepy.Cursor() object might look something like this:
tweets = []
for tweet in tweepy.Cursor(api.user_timeline,
screen_name=<twitter_handle>,
since_id = <since_id>
).items(<count>):
tweets.append(tweet)

Categories