def complete_signin(request_key, verifier, postgres_handle): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) session = TwitterSession.get_by_request_key(request_key, postgres_handle) if session.access_key: #this happens if we get multiple same exact responses from twitter #perhaps crazy clicking or back / forward browsing credentials = TwitterCredentials.get_by_access_key( session.access_key, postgres_handle) else: auth.set_request_token(request_key, session.request_secret) auth.get_access_token(verifier) # may have signed up already credentials = TwitterCredentials.get_by_access_key( auth.access_token.key, postgres_handle) if not credentials: credentials = TwitterCredentials.create(auth.access_token.key, auth.access_token.secret, postgres_handle) session.access_key = credentials.access_key if not credentials.twitter_user: #probably don't have the user in our db yet user = TwitterUser.upsert_from_api_user(credentials.api_handle.me(), postgres_handle) credentials.twitter_id = user.id credentials.save() #email screen_name = credentials.twitter_user.screen_name email_utils.send_email('*****@*****.**', ['*****@*****.**'], '%s signed up' % screen_name, 'smarttypes signup!') return session.save()
def complete_signin(request_key, verifier, postgres_handle): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) session = TwitterSession.get_by_request_key(request_key, postgres_handle) if session.access_key: #this happens if we get multiple same exact responses from twitter #perhaps crazy clicking or back / forward browsing credentials = TwitterCredentials.get_by_access_key(session.access_key, postgres_handle) else: auth.set_request_token(request_key, session.request_secret) auth.get_access_token(verifier) # may have signed up already credentials = TwitterCredentials.get_by_access_key(auth.access_token.key, postgres_handle) if not credentials: credentials = TwitterCredentials.create(auth.access_token.key, auth.access_token.secret, postgres_handle) session.access_key = credentials.access_key if not credentials.twitter_user: #probably don't have the user in our db yet user = TwitterUser.upsert_from_api_user(credentials.api_handle.me(), postgres_handle) credentials.twitter_id = user.id credentials.save() #email screen_name = credentials.twitter_user.screen_name email_utils.send_email('*****@*****.**', ['*****@*****.**'], '%s signed up' % screen_name, 'smarttypes signup!') return session.save()
def complete_signin(request_key, verifier, postgres_handle): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) session = TwitterSession.get_by_request_key(request_key, postgres_handle) auth.set_request_token(request_key, session.request_secret) auth.get_access_token(verifier) # may have signed up already credentials = TwitterCredentials.get_by_access_key(auth.access_token.key, postgres_handle) if not credentials: credentials = TwitterCredentials.create(auth.access_token.key, auth.access_token.secret, postgres_handle) session.access_key = credentials.access_key if not credentials.twitter_user: user = TwitterUser.upsert_from_api_user(credentials.api_handle.me(), postgres_handle) credentials.twitter_id = user.id credentials.save() screen_name = credentials.twitter_user.screen_name email_utils.send_email('*****@*****.**', ['*****@*****.**'], '%s signed up' % screen_name, 'smarttypes signup!') return session.save()
def load_user_and_the_people_they_follow(api_handle, user_id, postgres_handle, is_root_user=False): print "Attempting to load user %s." % user_id continue_or_exit(api_handle, user_id) try: api_user = api_handle.get_user(user_id=user_id) except TweepError, ex: print "Got a TweepError: %s." % ex return None model_user = TwitterUser.upsert_from_api_user(api_user, postgres_handle) postgres_handle.connection.commit() screen_name = model_user.screen_name if api_user.protected: print "\t %s is protected." % screen_name return model_user if not is_root_user and api_user.friends_count > MAX_FOLLOWING_COUNT: print "\t %s follows too many people, %s." % (screen_name, api_user.friends_count) model_user.save_following_ids([]) postgres_handle.connection.commit() return model_user print "Loading the people %s follows." % screen_name
if 'Sorry, that page does not exist' in str( ex) or 'User has been suspended' in str(ex): print 'setting caused_an_error' model_user = TwitterUser.get_by_id(user_id, postgres_handle) if not model_user: properties = {'id': user_id, 'screen_name': user_id} model_user = TwitterUser(postgres_handle=postgres_handle, **properties) model_user.save() postgres_handle.connection.commit() model_user.caused_an_error = datetime.now() model_user.save() postgres_handle.connection.commit() return None model_user = TwitterUser.upsert_from_api_user(api_user, postgres_handle) postgres_handle.connection.commit() screen_name = model_user.screen_name if api_user.protected: print "%s: %s is protected." % (root_user.screen_name, screen_name) return model_user following_ids = [] print "%s: loading the people %s follows." % (root_user.screen_name, screen_name) try: max_pages = 1 #5 if is_root_user else 1 following_id_pages = tweepy.Cursor(api_handle.friends_ids, user_id=user_id).pages(max_pages) for following_ids_page in following_id_pages:
except TweepError, ex: print "%s: api_handle.get_user(%s) got a TweepError %s" % (root_user.screen_name, user_id, ex) if 'Sorry, that page does not exist' in str(ex) or 'User has been suspended' in str(ex): print 'setting caused_an_error' model_user = TwitterUser.get_by_id(user_id, postgres_handle) if not model_user: properties = {'id': user_id, 'screen_name': user_id} model_user = TwitterUser(postgres_handle=postgres_handle, **properties) model_user.save() postgres_handle.connection.commit() model_user.caused_an_error = datetime.now() model_user.save() postgres_handle.connection.commit() return None model_user = TwitterUser.upsert_from_api_user(api_user, postgres_handle) postgres_handle.connection.commit() screen_name = model_user.screen_name if api_user.protected: print "%s: %s is protected." % (root_user.screen_name, screen_name) return model_user following_ids = [] print "%s: loading the people %s follows." % (root_user.screen_name, screen_name) try: max_pages = 1#5 if is_root_user else 1 following_id_pages = tweepy.Cursor(api_handle.friends_ids, user_id=user_id).pages(max_pages) for following_ids_page in following_id_pages: following_ids += [str(x) for x in following_ids_page] except TweepError, ex:
string.ljust(creds.email if creds.email else '', 30) ) if __name__ == "__main__": """ if no args, show all creds if args, first arg is creds_username, second is root_username """ if len(sys.argv) == 1: list_cred_details() elif len(sys.argv) == 2: creds_user = TwitterUser.by_screen_name(sys.argv[1], postgres_handle) creds = TwitterCredentials.get_by_twitter_id(creds_user.id, postgres_handle) creds.root_user_id = None creds.save() postgres_handle.connection.commit() else: creds_user = TwitterUser.by_screen_name(sys.argv[1], postgres_handle) root_user = TwitterUser.by_screen_name(sys.argv[2], postgres_handle) if not root_user: api_user = creds_user.credentials.api_handle.get_user(screen_name=sys.argv[2]) root_user = TwitterUser.upsert_from_api_user(api_user, postgres_handle) postgres_handle.connection.commit() creds = TwitterCredentials.get_by_twitter_id(creds_user.id, postgres_handle) creds.root_user_id = root_user.id creds.save() postgres_handle.connection.commit()
def load_user_and_the_people_they_follow(api_handle, screen_name): print "Attempting to load %s" % screen_name continue_or_exit(api_handle) try: api_user = api_handle.get_user(screen_name=screen_name) except TweepError, ex: print "Got a TweepError: %s." % ex if str(ex) == "Not found": print "Setting caused_an_error for %s " % screen_name model_user = TwitterUser.by_screen_name(screen_name) model_user.caused_an_error = datetime.now() model_user.save() return model_user model_user = TwitterUser.upsert_from_api_user(api_user) if api_user.protected: print "\t %s is protected" % screen_name return if api_user.friends_count > MAX_FOLLOWING_COUNT: print "\t %s follows too many people, %s" % (screen_name, api_user.friends_count) model_user.save_following_ids([]) return model_user print "Loading the people %s follows" % screen_name following_ids = [] try: api_following_list = list(tweepy.Cursor(api_handle.friends, screen_name).items()) except TweepError, ex: