Skip to content

Instantly share code, notes, and snippets.

@glarrain
Created October 17, 2012 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glarrain/3905363 to your computer and use it in GitHub Desktop.
Save glarrain/3905363 to your computer and use it in GitHub Desktop.
Retrieve a user's last tweets
from urllib2 import urlopen
from django.utils.simplejson import loads
from oauth2 import Token
from social_auth.backends.utils import build_consumer_oauth_request
from social_auth.backends.twitter import TwitterAuth
def get_last_tweets(user):
try:
social_auth = user.social_auth.filter(provider='twitter')[0]
except IndexError:
return []
request = build_consumer_oauth_request(
TwitterAuth,
Token.from_string(social_auth.extra_data['access_token']),
'https://api.twitter.com/1/statuses/home_timeline.json'
)
response = '\n'.join(urlopen(request.to_url()).readlines())
return loads(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment