skip to Main Content

I’m trying to do a search for tweets in Twitter’s premium API. I copied the format in the TwitterAPI documentation into my current code, but I get the following error. What am I missing? I am fairly new to Python. I’m using Twitter’s sandbox premium search API to search full history. I probably have a gap in knowledge on how get_iterator() is supposed to be utilized. Thanks in advance!

api = TwitterAPI(key, secretkey, token, secrettoken)

#search tweets #added edit to pass api through Twitter Pager
tweets = TwitterPager(api, 'tweets/search/%s/:%s' % (PRODUCT, LABEL), 
                      {'query': 'pizza',
                       'lang': 'en'
                    })

for item in tweets.get_iterator():
    print(item['text'] if 'text' in item else item)

And I get the following error:

---------------------------------------------------------------------------
TwitterRequestError                       Traceback (most recent call last)
<ipython-input-71-85784cf972fe> in <module>
----> 1 for item in tweets.get_iterator():
      2     print(item)

~AppDataLocalContinuumanaconda3libsite-packagesTwitterAPITwitterPager.py in get_iterator(self, wait, new_tweets)
     45                 start = time.time()
     46                 r = self.api.request(self.resource, self.params)
---> 47                 it = r.get_iterator()
     48                 if new_tweets:
     49                     it = reversed(list(it))

~AppDataLocalContinuumanaconda3libsite-packagesTwitterAPITwitterAPI.py in get_iterator(self)
    204         """
    205         if self.response.status_code != 200:
--> 206             raise TwitterRequestError(self.response.status_code)
    207 
    208         if self.stream:

TwitterRequestError: Twitter request failed (422)

My code works with basic ‘api.request(…)’ method using the same search parameters. but I need to use TwitterPager and get_iterator() in order to return more results.

2

Answers


  1. I am not familiar with the particular package you are using, but my guess is that you are not actually passing it the api object you have created. Therefore it is trying to perform its actions on your first argument(a string), which should actually be the api instance. Try this:

    api = TwitterAPI(key, secretkey, token, secrettoken)
    
    #search tweets
    tweets = TwitterPager(api, 'tweets/search/%s/:%s' % (PRODUCT, LABEL), # added the api as first arg
                          {'query': 'pizza',
                           'lang': 'en'
                        })
    
    for item in tweets.get_iterator():
        print(item['text'] if 'text' in item else item)
    

    Edit

    It looks like in these docs that you must send the api as the first argument to the pager.

    Login or Signup to reply.
  2. See https://geduldig.github.io/TwitterAPI/paging.html

    The example given shows:

    r = TwitterPager(api, 'search/tweets', {'q':'pizza', 'count':100})
    for item in r.get_iterator():
        if 'text' in item:
            print item['text']
        elif 'message' in item and item['code'] == 88:
            print 'SUSPEND, RATE LIMIT EXCEEDED: %sn' % item['message']
    

    You’ll have to adjust the endpoint (i.e. search/tweets) and parameter dictionary (i.e., {'q':'pizza', 'count':100}) to fit what you want to call with the premium api

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search