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
Related
I'm recieving date in PMS message something like this |GA090616|GD090617|
which means Guest Arrival is at 09-06-16 and Guest Deprature is at 09-06-17
I wanted to parse it as date using python.
I've also visited stack oveflow[1, 2 ...] for this but as solution I've found
from datetime import datetime
self.DATE_FROMAT='%d/%m/%y'
arrival_date=datetime.strptime('90616', self.DATE_FROMAT).date()
print(arrival_date)
and it's not possible to parse it like this due to its unclear format.
I'm not sure if 09 is a month or a date, but from what I've seen in documents and PDFs, it appears to be a month.
Is there any better solution for this kind of date parsing? or suggestions for my expectations.
09-06-16,
09-06-17
Note:
Please Just take the date from the string 090617 and parse it as a date. That will be helpful.
You can do this with regex matching, you can either split the string with msg.split("|") or not, but that depends on your use case.
import re
from datetime import datetime
msg = "GA090616|GD090617|"
DATE_FORMAT='%d%m%y'
ar = re.match("GA(\d{6})", msg)
dp = re.match("GD(\d{6})", msg)
guest_arrival = datetime.strptime(ar.group(1), DATE_FORMAT).date()
guest_departure = datetime.strptime(dp.group(1), DATE_FORMAT).date()
Although not fully tested, this should be a boilerplate as to how to retrieve the date from the message. Remember to remove the \ from the date format, as that is not included in the message.
I'm trying to make a Twitter bot that selects a quote from a list of quotes I give it to Tweet every 2 hours. I keep having a syntax error when I try to define the Tweet daily function and I'm not exactly sure what's wrong. I'm a beginner in coding so if it's obvious, I apologize; it's my first time doing this. I already had success in having the app Tweet with the api.update_status() command but I'm not sure how to have select from the list I'm creating. I did import random, time, from datetime, import datetime, timedelta as well as tweepy and everything else successfully.
random.choices(tweets)
last_tweeted = datetime.now()-timedelta(hours=2), random.choices(tweets)
tweets = ['insert, list, of tweets, I am adding']
where python says I have a syntax code
def tweet_daily(api, last_tweeted, random.choices(tweets):
if last_tweeted < datetime.now()-timedelta(hours=2):
logger.info(f"Tweeted {tweets, random.choices} at {datetime.now().strftime('%m/%d/%Y at %H:%M:%S')}")
return datetime.now()
else:
return last_tweeted
api.update_status('tweets(random.choice)')`
You're missing a close parenthesis in the function definition:
def tweet_daily(api, last_tweeted, random.choices(tweets):
Im struggling to retrieve the tweets associated with a particular search between two dates. I looked at the answer here and used that as below, but, as the answer mentions, the code only works for tweets which are 10-14 days old and as I need tweets from 2014, it results in tweets being an empty list.
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = []
company_name = '#' + 'Apple'
date_strng = " since:2014-10-11 until:2015-10-14"
for tweet in tweepy.Cursor(api.search,q=company_name + date_strng,count=10000,lang="en").items():
tweets.append(tweet)
Also tried the following, but it didnt work (tweets is again an empty list). But if I remove the until argument, I get the tweets since the start_date:
start_date = datetime.datetime(2014,10,11)
end_date = datetime.datetime(2015,10,14)
for tweet in tweepy.Cursor(api.search,q=company_name,count=10000,lang="en", since=start_date,until=end_date).items():
tweets.append(tweet)
Was wondering if there is a solution to this.
Thanks
Reason for the empty list is due to the fact that the standard search api retrieve only last 7 days of tweets . Since you have given the start and until dates it’s filtering the tweets as per dates. Obviously list will be empty.
Refer the below link for retrieving old tweets
https://stackoverflow.com/a/61737450/10703097
Also you are trying 1 year duration of tweet which is a huge corpus of tweets try to modify as per your needs.
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
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?