Ejemplo n.º 1
0
	def getResponse(self, reset_cache=True, q = config.SEARCH):
		'''
		Handles interaction with Twitter module and manages memcache.
		Returns response JSON or None as well as a tuple with memcache info.
		'''
		# Initialize Twitter Search
		twsearch = TwitterSearch(keys.CONSUMER_KEY, keys.CONSUMER_SECRET)

		# See what and when the last status we fetched was
		fetched_on = memcache.get('last_status_fetched_on')

		# Delete last fetched status ID if it's too old (or doesn't exist) to prevent loading 1000's of statuses
		if (not fetched_on or time.time() - fetched_on > config.LASTFETCHEDTIMEOUT) and reset_cache:
			memcache.delete('last_status_fetched_id')

		# Get results till you hit this guy
		fetch_until = memcache.get('last_status_fetched_id')
		
		# Get JSON response
		response = twsearch.search(q,fetch_until=fetch_until,count=config.SEARCHCOUNT)

		# Update memcache
		if response and reset_cache:
			memcache.set('last_status_fetched_id', response.get('statuses')[0].get('id'),time=config.LASTFETCHEDTIMEOUT*2)
			memcache.set('last_status_fetched_on', time.time(),time=config.LASTFETCHEDTIMEOUT*2)

		return (response,memcache.get('last_status_fetched_id'),memcache.get('last_status_fetched_on'),fetch_until)