semiflex
semiflex

Reputation: 1246

Python - Tweepy, retrieving the created_at

I'm trying to retrieve tweets and the dates as to when they were created. This is what my code looks like so far:

import tweepy
import json
import urllib
import sys
import datetime

from tweepy import OAuthHandler

user = "billgates"
count = 1

def twitter_fetch(screen_name = user,maxnumtweets=count):

    consumer_token = 'INSERT CONSUMER TOKEN'
    consumer_secret = 'INSERT CONSUMER SECRET'
    access_token = 'INSERT ACCESS TOKEN'
    access_secret = 'INSERT ACCESS SECRET'

    auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
    auth.set_access_token(access_token,access_secret)

    api  = tweepy.API(auth)

    for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count):
        print status.text+'\n'

if __name__ == '__main__':
    twitter_fetch(user,count)

I know that I presumably need to call the date using "created_at", but I'm not exactly sure where to put this in order to retrieve it. How can I do this?

Upvotes: 2

Views: 4796

Answers (2)

semiflex
semiflex

Reputation: 1246

As Wander Nauta said, changing the lines:

for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count):
    print status.text + '\n'

to:

for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count):
    print status.text + ' ' + str(status.created_at) + '\n'

should print out the tweet along with the time and date of the creation of the tweet.

Upvotes: 2

FooBar
FooBar

Reputation: 334

I am not sure whether this is exactly what you are looking for, but this code should work:

import tweepy
import json
import urllib
import sys
import datetime

from tweepy import OAuthHandler

user = "billgates"
count = 1

def twitter_fetch(screen_name = user,maxnumtweets=count):


    consumer_token = 'INSERT CONSUMER TOKEN' 
    consumer_secret = 'INSERT CONSUMER SECRET'
    access_token = 'INSERT ACCESS TOKEN'
    access_secret = 'INSERT ACCESS SECRET'

    auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
    auth.set_access_token(access_token,access_secret)

    api  = tweepy.API(auth)


    for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count): 
            print status.text+'\n'
            print status.created_at


if __name__ == '__main__':
    twitter_fetch(user,count)

I just added the line "print status.created_at" to your code, which will print the date and the time the tweets were created at (type is datetime.datetime).

Upvotes: 1

Related Questions