Python Twitter - Get tweet URLS - python

I am using the python-twitter module to get my most recent twitter status updates:
for i in range(0, limit):
tweet = twitter.Api().GetUserTimeline(TWITTER_USERNAME)[i]
tweet.date = datetime.strptime( tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y" )
tweets.append(tweet)
It's working fine, but the tweet.text property does not wrap the URL in a tags so they are clickable. I had a look through the python-twitter documentation but it doesn't give any specific advise on getting a status with the URLs as clickable urls.
I'm sure there is a function in the library to do this, does anyone know how?

You'll likely have to parse the text to find and mark up the URLs yourself. Here's a question about that:
What's the cleanest way to extract URLs from a string using Python?

Related

Tweepy - get expanded url using paginator (elevated access level)

I'm collecting full urls from tweets (not t.co ones), for that I needed to use option tweet_mode="extended" which you get in elevated access level (that I have).
I want to get full urls using Paginator.
I don't know how to do this except collecting tweet ids first and then calling api.get_status like this:
for sq in search_q:
for tweet in tweepy.Paginator(client.search_recent_tweets,sq).flatten(limit=5):
tweet_ids.append(tweet.id)
for tid in tweet_ids:
status = api.get_status(tid, tweet_mode="extended")
full_urls.append(status.entities['urls'][0]['expanded_url'])
which seems awfully inefficient.
Any help is appreciated.
Adding a tweet_fields for entities solves this.
for sq in search_q:
for tweet in tweepy.Paginator(client.search_recent_tweets,sq,tweet_fields=["entities"]).flatten(limit=5):
tweet_ids.append(tweet.data["entities"]['urls'][0]['expanded_url'])

Excluding link at the end while pulling tweets in tweepy Streaming

I am pulling text or extended_text using tweepy streaming, but when I pull these tweets, there is always a t.co/randomletters link at the end that leads to nowhere. What is it and how do I get rid of it?
Here is an example:
"text": "To make room for more expression, we will now count all emojis as equal—including those with gender‍‍‍ ‍‍and skin tone modifiers https://t.co(forward slash)MkGjXf9aXm"
Please help
As far as my experience with twitter and tweepy goes, these URL's are included in a tweet's text whenever there is a URL of some sort in the actual tweet, so we can't really avoid getting them.
You could remove them after you get them, this is a simple regex that replaces the pattern of these URL's with a blank string.
import re
re.sub(r' https://t.co/\w{10}', '', tweet_text)

Python Tweepy 280 Character Statuses

I'm trying to scrape the full 280 character tweets off of twitter but I can't get them to not trail off with '...' after 140 chars. Here's my code:
import tweepy
import datetime
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)
end_date = datetime.datetime.utcnow() - datetime.timedelta(days=0)
for status in api.user_timeline(targer_user):
print(status.text)
if status.created_at > end_date:
break
I've read that adding text_mode=extendedto the function will solve this, but it's making no difference for me. If I use another suggested argument tweet_mode='extended', text is no longer an attribute of status.
How can I fix this?
It seems you need to use full_text now to get the 280 char tweet. Try something along the lines of:
print(status.extended_tweet['full_text'])
The tweet_mode='extended' can be used in user_timeline if you want, in which case you would just use below:
print(status.full_text)
This looks a bit nicer to me.
It might also be worth pointing out that - from what I've read - this might not work for a retweet (Twitter streaming API not return full tweets) but there are separate bits of the api you can use for that, so be sure to check before you print.
Twitter docs, in case you want a closer look at the update: https://developer.twitter.com/en/docs/tweets/tweet-updates.html

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)

how to display tweets of particular time period using twitter api

my requirement is to display tweets of particular time period using twitter api. I am using getsearch method of api to get all tweets i have tried the following code to display tweets of particular time period.
Three_days_ago = datetime.datetime.utcnow()-datetime.timedelta(days = 3)
for tweet in tweets:
if tweet.created_at > Three_days_ago:
print tweet
but i am getting error 'can't compare datetime.datetime to unicode'
How to do this please suggest me..
You can use dateutil.
from dateutil import parser
Three_days_ago = datetime.datetime.utcnow()-datetime.timedelta(days = 3)
for tweet in tweets:
tweeted_datetime = parser.parse(tweet.created_at)
if tweeted_datetime > Three_days_ago:
print tweet, tweeted_datetime.strftime("%Y-%m-%d %H:%M:%S %p")
I'm not sure that you'll be able to get the milliseconds as it's not passed while parsing. For more strftime options refer here.
As #jamylak says, tweet.created_at is text not a datetime.
You can convert it to a datetime according to the rules given here: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Categories

Resources