def test_gettweets(self, apikeys, monkeypatch, mockurlresponse): self._set_environ(apikeys, monkeypatch) import urllib2 monkeypatch.setattr(urllib2, 'urlopen', mockurlresponse) number_of_tweets = 1 service = TwitterService() # parsed tweets result = service.get_latest_tweets(limit=number_of_tweets) assert type(result) == list assert len(result) == number_of_tweets assert type(result[0]) == dict assert result[0]['data']['index'] == (169, 'AQI') assert result[0]['data']['concentration'] == 90.0 assert result[0]['reading_id'] == 431929188598042624L import datetime expected = datetime.datetime(2014, 2, 8, 7, 0) assert result[0]['type']['hour_from'] == expected # raw tweets result = service.get_latest_tweets(limit=number_of_tweets, raw=True) from twitter import TwitterResponse assert isinstance(result, TwitterResponse) assert isinstance(result, list) assert len(result) == number_of_tweets assert result.rate_limit_limit == 20 assert result.rate_limit_reset == 4 assert type(result[0]) == dict assert 'data' not in result[0] assert 'created_at' in result[0] assert result[0]['id'] == 431929188598042624L
def poll(): from airquality.feeds.twitter import TwitterService from airquality import backends from contextlib import closing # --dev #hardcoded = lambda: ('127.0.0.1:44444', '/tmp') #settings = get_settings(hardcoded, db='test') # --production settings = get_settings(get_metadata) print "\tPolling twitter .." service = TwitterService(twitter_user='******') tweets = service.get_latest_tweets(limit=6) with closing(backends.Repository(settings)) as repo: repo.save(tweets) print "done\n"