In [5]:
import ConfigParser
from TwitterAPI import TwitterAPI

In [6]:
def get_twitter(config_file):
    """ Read the config_file and construct an instance of TwitterAPI.
    Args:
      config_file ... A config file in ConfigParser format with Twitter credentials
    Returns:
      An instance of TwitterAPI.
    """
    config = ConfigParser.ConfigParser()
    config.read(config_file)
    twitter = TwitterAPI(
                   config.get('twitter', 'consumer_key'),
                   config.get('twitter', 'consumer_secret'),
                   config.get('twitter', 'access_token'),
                   config.get('twitter', 'access_token_secret'))
    return twitter

In [7]:
def robust_request(twitter, resource, params, max_tries=5):
    """ If a Twitter request fails, sleep for 15 minutes.
    Do this at most max_tries times before quitting.
    Args:
      twitter .... A TwitterAPI object.
      resource ... A resource string to request.
      params ..... A parameter dictionary for the request.
      max_tries .. The maximum number of tries to attempt.
    Returns:
      A TwitterResponse object, or None if failed.
    """
    for i in range(max_tries):
        request = twitter.request(resource, params)
        if request.status_code == 200:
            return request
        else:
            print >> sys.stderr, 'Got error:', request.text, '\nsleeping for 15 minutes.'
            sys.stderr.flush()
            time.sleep(60 * 15)

twitter = get_twitter('twitter.cfg')

In [21]:
request = robust_request(twitter, 'search/tweets', {'q': "Hiring, software, developer, chicago", 'count': '10'})
tweets = [t for t in request]
print 'fetched %d tweets' % len(tweets)


fetched 10 tweets

In [22]:
# Print 10 tweets.
text = [t['text'] for t in tweets]
print '\n\n'.join(text[:1000])


Want to work in #Chicago, IL? View our latest opening: https://t.co/QTREeyDCUh #IT #DePaulU #Job #Jobs #Hiring https://t.co/ImMJJdZat4

Can you recommend anyone for this #IT #job? https://t.co/FxLOGOClTV #Chicago, Illinois #Hiring #CareerArc

See our latest #Chicago, IL #job and click to apply: Software Developer - https://t.co/Utf5MPPqli #IT #Hiring #CareerArc

See our latest #Chicago, IL #job and click to apply: Software Developer - https://t.co/DbYQTjFmJe #IT #Hiring #CareerArc

Now hiring for: Ruby Software Developer in Chicago, IL https://t.co/wIQx7CaRWO #job

We're #hiring! Read about our latest #job opening here: Software Developer - https://t.co/hmI9PtvVQD #RHTechJobs #Chicago, IL #IT

Want to work in #Chicago, IL? View our latest opening: https://t.co/K2lHZZGmOX #IT #Job #Jobs #Hiring #CareerArc

RT https://t.co/WPP0q3FUHv tmj_chi_it : Want to work at Oracle? We're #hiring in #Chicago, IL! Click for details: https://t.co/5XXZnsbfR0…

Want to work at Oracle? We're #hiring in #Chicago, IL! Click for details: https://t.co/bVv1QfvZ9S #IT #Job #Jobs #CareerArc

Software Developers (Remote): Location : Chicago, IL US Company: CGI… https://t.co/w4fDyR9QIP #Hiring #Careers

In [ ]: