<s> import tweepy <EOL> from datetime import datetime <EOL> from django . shortcuts import redirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from causal . main . models import OAuth , RequestToken , AccessToken , UserService <EOL> def _oauth ( service , cust_callback_url = None ) : <EOL> current_site = Site . objects . get ( id = settings . SITE_ID ) <EOL> callback = cust_callback_url or reverse ( '<STR_LIT>' ) <EOL> callback = "<STR_LIT>" % ( current_site . domain , callback , ) <EOL> return tweepy . OAuthHandler ( <EOL> service . app . auth_settings [ '<STR_LIT>' ] , <EOL> service . app . auth_settings [ '<STR_LIT>' ] , <EOL> callback <EOL> ) <EOL> def user_login ( service , cust_callback_url = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> oauth = _oauth ( service , cust_callback_url ) <EOL> redirect_url = oauth . get_authorization_url ( ) <EOL> if not service . auth : <EOL> auth_handler = OAuth ( ) <EOL> else : <EOL> auth_handler = service . auth <EOL> new_rt = RequestToken ( ) <EOL> new_rt . oauth_token = oauth . request_token . key <EOL> new_rt . oauth_token_secret = oauth . request_token . secret <EOL> new_rt . save ( ) <EOL> auth_handler . request_token = new_rt <EOL> auth_handler . save ( ) <EOL> if not service . auth : <EOL> service . auth = auth_handler <EOL> service . save ( ) <EOL> except tweepy . TweepError : <EOL> return False <EOL> return redirect ( redirect_url ) <EOL> def get_api ( service ) : <EOL> oauth = _oauth ( service ) <EOL> if not service . auth : <EOL> return False <EOL> if not service . auth . access_token : <EOL> if not service . auth . request_token : <EOL> return False <EOL> oauth . set_request_token ( <EOL> service . auth . request_token . oauth_token , <EOL> service . auth . request_token . oauth_token_secret <EOL> ) <EOL> try : <EOL> oauth . get_access_token ( service . auth . request_token . oauth_verify ) <EOL> except tweepy . TweepError : <EOL> return False <EOL> at = AccessToken . objects . get_or_create ( <EOL> oauth_token = auth . access_token . key , <EOL> oauth_token_secret = auth . access_token . secret <EOL> ) <EOL> service . auth . access_token = at <EOL> service . auth . save ( ) <EOL> else : <EOL> oauth . set_access_token ( <EOL> service . auth . access_token . oauth_token , <EOL> service . auth . access_token . oauth_token_secret <EOL> ) <EOL> return tweepy . API ( oauth ) <EOL> def get_user ( service ) : <EOL> service_auth = _oauth ( service ) <EOL> return tweepy . API ( service_auth ) . get_user ( '<STR_LIT>' ) </s>
