Search code examples
twittertwython

How to fetch all the direct messages using Twython?


I am trying to fetch all the direct messages in my account. But I tried to use cursor for fetching. But I'm getting an error like

TypeError: cursor() missing 1 required positional argument: 'function'

My code is below.

from twython import Twython

APP_KEY = 'KEY'
APP_SECRET = 'SECRET'
ACCESS_TOKEN = 'TOKEN'
ACCESS_SECRET = 'SECRET'

twitter = Twython(APP_KEY,APP_SECRET,ACCESS_TOKEN,ACCESS_SECRET)

for tweet in Twython.cursor(twitter.get_direct_messages(),since_id=1,count=100):
print (tweet)

Please help on this.


Solution

  • Pretty sure that you need to invoke the .cursor method on the twitter object, not on the Twython class.

    So try

    for tweet in twitter.cursor(twitter.get_direct_messages(),since_id=1,count=100):
        print (tweet)
    

    Specifically, this works for me:

    results = twitter.cursor(twitter.get_direct_messages,count=2)
    for result in results:
         print result['text']