def callback(request): request_token = request.session.get(SESSION_TOKEN_KEY, None) if request_token is None: return render_to_response('callback.html', { 'token': True }) request_token = oauth.OAuthToken.from_string(request_token) if request_token.key != request.GET.get('oauth_token', 'no-token'): return render_to_response('callback.html', { 'mismatch': True }) request.user.twitter_api.token = request.user.twitter_api.get_access_token(request_token) # Actually login credentials = request.user.twitter_api.verify_credentials() if credentials is None: return render_to_response('callback.html', { 'username': True }) from twitterauth import login, authenticate user, created = User.objects.get_or_create(username=credentials['screen_name']) if created or (user.key != request.user.twitter_api.token.key or user.secret != request.user.twitter_api.token.secret): user.key = request.user.twitter_api.token.key user.secret = request.user.twitter_api.token.secret user.save() # Call authenticate to verify and add backend to the user object. user = authenticate(key=user.key, secret=user.secret) login(request, user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() del request.session[SESSION_TOKEN_KEY] return HttpResponseRedirect(request.session[SESSION_LOGIN_REDIRECT_KEY])
def callback(request): request_token = request.session.get(SESSION_TOKEN_KEY, None) if request_token is None: return render_to_response('callback.html', { 'token': True }) request_token = oauth.OAuthToken.from_string(request_token) if request_token.key != request.GET.get('oauth_token', 'no-token') and not settings.DEBUG: return render_to_response('callback.html', { 'mismatch': True }) if not settings.DEBUG: request.user.twitter_api.token = request.user.twitter_api.get_access_token(request_token) else: form = PinCodeForm(request.POST) if form.is_valid(): pin_code = "%s" % form.cleaned_data['pin_code'] request.user.twitter_api.token = request.user.twitter_api.get_access_token(request_token, verifier=pin_code) else: return HttpResponseRedirect(reverse('auth_login')) # Actually login credentials = request.user.twitter_api.verify_credentials() if credentials is None: return render_to_response('callback.html', { 'username': True }) from twitterauth import login, authenticate user, created = User.objects.get_or_create(username=credentials['screen_name']) if created or (user.key != request.user.twitter_api.token.key or user.secret != request.user.twitter_api.token.secret): user.key = request.user.twitter_api.token.key user.secret = request.user.twitter_api.token.secret user.thumbnail = credentials['profile_image_url'] user.name = credentials['name'] user.save() # Call authenticate to verify and add backend to the user object. user = authenticate(key=user.key, secret=user.secret) #print 'vamos' #print 'Aqui', user login(request, user) #print 'request.user: '******'...................' if request.session.test_cookie_worked(): request.session.delete_test_cookie() del request.session[SESSION_TOKEN_KEY] return HttpResponseRedirect(request.session[SESSION_LOGIN_REDIRECT_KEY])
def callback(request): request_token = request.session.get(SESSION_TOKEN_KEY, None) if request_token is None: return render_to_response('callback.html', {'token': True}) request_token = oauth.OAuthToken.from_string(request_token) if request_token.key != request.GET.get('oauth_token', 'no-token') and not settings.DEBUG: return render_to_response('callback.html', {'mismatch': True}) if not settings.DEBUG: request.user.twitter_api.token = request.user.twitter_api.get_access_token( request_token) else: form = PinCodeForm(request.POST) if form.is_valid(): pin_code = "%s" % form.cleaned_data['pin_code'] request.user.twitter_api.token = request.user.twitter_api.get_access_token( request_token, verifier=pin_code) else: return HttpResponseRedirect(reverse('auth_login')) # Actually login credentials = request.user.twitter_api.verify_credentials() if credentials is None: return render_to_response('callback.html', {'username': True}) from twitterauth import login, authenticate user, created = User.objects.get_or_create( username=credentials['screen_name']) if created or (user.key != request.user.twitter_api.token.key or user.secret != request.user.twitter_api.token.secret): user.key = request.user.twitter_api.token.key user.secret = request.user.twitter_api.token.secret user.thumbnail = credentials['profile_image_url'] user.name = credentials['name'] user.followers_count = credentials['followers_count'] user.save() # Call authenticate to verify and add backend to the user object. user = authenticate(key=user.key, secret=user.secret) #print 'vamos' #print 'Aqui', user login(request, user) #print 'request.user: '******'...................' if request.session.test_cookie_worked(): request.session.delete_test_cookie() del request.session[SESSION_TOKEN_KEY] return HttpResponseRedirect(request.session[SESSION_LOGIN_REDIRECT_KEY])
def main(): # Remove first command line arg (it's the name of this file) sys.argv.pop(0) # Constants N = 3 # Value of N in N-gram M = 100 # Number of tweets to generate R = 1 # Number of requests to make per user (fetches 200 tweets per request) try: N = int(sys.argv.pop(0)) M = int(sys.argv.pop(0)) R = int(sys.argv.pop(0)) except ValueError: print >> sys.stderr, "N, M, or R was NaN" sys.stderr.flush() sys.exit(0) generated_tweets = generate_tweets(N, M, R, authenticate(), sys.argv) for tweet in generated_tweets: print tweet print("<ENDOFOUTPUT>") sys.stdout.flush()
date_file = open('data/lastupdate.txt', 'r+') date_file.seek(0) date_file.write(str(new_update_time)) date_file.truncate() date_file.close() # Constants BOT_NAME = 'tweetfuser' # Read last update time from file last_update = get_last_update_time() print("LAST UPDATE: " + str(last_update)) # Get a Tweepy API object api = authenticate() def get_new_tweets(): new_tweets = api.search(q='@' + BOT_NAME, rpp=100, show_user=1, include_entities=1) new_tweets[:] = [ x for x in new_tweets if x.created_at > last_update and x.user.screen_name != BOT_NAME ] return new_tweets # Save new "last updated" time to save to file at end