Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making very specific time requests (to the second) on Twitter API, using Python Tweepy?

I would like to request tweets on a specific topic (for example: "cancer"), using Python Tweepy. But usually its time can only be specified by a specific day, for example.

startSince = '2014-10-01'
endUntil = '2014-10-02'

for tweet in tweepy.Cursor(api.search, q="cancer", 
    since=startSince, until=endUntil).items(999999999):

Is there a way to specify the time so I can collect "cancer" tweets between 2014-10-01 00:00:00 and 2014-10-02 12:00:00? This is for my academic research: I was able to collect cancer tweets for the last month, but the sudden burst of quantity of "breast cancer" tweets due to the cancer awareness month breaks my script and I have to collect them in different time segments, and I will not be able to retrieve the tweets for Oct 01, 2014 if I can't figure it out soon.

like image 995
KubiK888 Avatar asked Mar 19 '23 07:03

KubiK888


1 Answers

There is no way that I've found to specific a time using since/until.

You can hack your way around this using since_id & max_id.

If you can find a tweet made at around the times you want, you can restrict you search to those made after since_id and before max_id

import tweepy

consumer_key        = 'aaa'
consumer_secret     = 'bbb'
access_token        = 'ccc'
access_token_secret = 'ddd'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

results = api.search(q="cancer", since_id=518857118838181000, max_id=518857136202194000)

for result in results:
    print result.text
like image 161
Terence Eden Avatar answered Apr 25 '23 17:04

Terence Eden