From ba214182b28bb1bc61047853f52b392ae88e40e0 Mon Sep 17 00:00:00 2001 From: Sanjay B Date: Wed, 22 Jan 2014 13:44:53 +0530 Subject: [PATCH] forgot to add utils.py --- itf/frontpage/utils.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 itf/frontpage/utils.py diff --git a/itf/frontpage/utils.py b/itf/frontpage/utils.py new file mode 100644 index 0000000..094edf7 --- /dev/null +++ b/itf/frontpage/utils.py @@ -0,0 +1,29 @@ +from django.conf import settings +from django.core.cache import cache +from twython import Twython, TwythonError + +#from myproject.utils.twitter_parser import Parser + +def get_latest_tweets(): + """ + Gets latest tweet from the Twitter user specified in settings. + Caches latest tweet for 10 minutes to reduce API calls + """ + #latest_tweet = cache.get('latest_tweet') + latest_tweet = None + if not latest_tweet: + #parser = Parser() + + twitter = Twython(settings.TWITTER_CONSUMER_KEY, + settings.TWITTER_CONSUMER_SECRET, + settings.TWITTER_OAUTH_TOKEN, + settings.TWITTER_OAUTH_TOKEN_SECRET) + try: + user_timeline = twitter.get_user_timeline(screen_name=settings.TWITTER_USER) + except TwythonError as e: + return {"latest_tweet": e} + + latest_tweets = user_timeline[0:5] + #latest_tweet['text'] = parser.parse(latest_tweet['text']).html + #cache.set('latest_tweet', latest_tweet, settings.TWITTER_TIMEOUT) + return latest_tweets \ No newline at end of file