def register(): form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): #user = User(form.uname.data, form.pword.data, #form.phone.data) #if form.uname.data not in credential_dictionary: if not check_user_exists(form.uname.data): #flash('Thanks for registering') username = form.uname.data.replace('<','').replace('>','') password = form.pword.data password_hash = hashlib.sha256((password + salt).encode()).hexdigest() #credential_dictionary[username] = [password_hash, form.phone.data] #print (credential_dictionary[form.uname.data][0], credential_dictionary[form.uname.data][1]) print ("username: "******"\npassword: "******"\nphone: ", form.phone.data) new_user = db_init.User(username=username, password = password_hash, phone = form.phone.data) db_init.db_session.add(new_user) db_init.db_session.commit() success = 'success' response = make_response(render_template('register.html', form=form, success = success)) secure_response(response) return response else: success = 'failure' response = make_response(render_template('register.html', form=form, success = success)) secure_response(response) return response #return redirect(url_for('login')) success = 'failure' response = make_response(render_template('register.html', form=form, success = success)) secure_response(response) return response response = make_response(render_template('register.html', form=form)) secure_response(response) return response
def create_admin_account(): admin_password_hash = hashlib.sha256( (admin_credential['password'] + salt).encode()).hexdigest() admin = db_init.User(username=admin_credential['username'], password=admin_password_hash, phone=admin_credential['phone']) db_init.db_session.add(admin) db_init.db_session.commit()
def save_data(json_data): id = None if 'id' in json_data: id = json_data['id'] if not db_init.Tweet.exists(id=id): total_words = 0 id_str = json_data['id_str'] in_reply_to_user_id = json_data['in_reply_to_user_id'] if 'in_reply_to_user_id' in json_data else None created_at = parser.parse(json_data['created_at']) #USER id_user = json_data['user']['id'] id_str_user = json_data['user']['id_str'] if 'id_str' in json_data['user'] else None screen_name = json_data['user']['screen_name'] if 'screen_name' in json_data['user'] else None name = json_data['user']['name'] if 'name' in json_data['user'] else None truncated = json_data['truncated'] #created_at = parser.parse(json_data['user']['created_at']) if 'created_at' in json_data['user'] else None #is_translator = json_data['user']['is_translator'] if 'is_translator' in json_data['user'] else False #name = json_data['user']['name'] if 'name' in json_data['user'] else None text = json_data['text'] if truncated : text_full = json_data['extended_tweet']['full_text'] else: text_full = text #contributors = json_data['contributors'] if 'contributors' in json_data else None #retweet_count = json_data['retweet_count'] if 'retweet_count' in json_data else None in_reply_to_status_id = json_data['in_reply_to_status_id'] if 'in_reply_to_status_id' in json_data else None #filter_level = json_data['filter_level'] if 'filter_level' in json_data else None #quote_count = json_data['quote_count'] if 'quote_count' in json_data else None #geo = json_data['geo'] if 'geo' in json_data else None source = json_data['source'] if 'source' in json_data else None #possibly_sensitive = json_data['possibly_sensitive'] if 'possibly_sensitive' in json_data else False in_reply_to_screen_name = json_data['in_reply_to_screen_name'] if 'in_reply_to_screen_name' in json_data else None #is_quoted_status = json_data['is_quoted_status'] if 'is_quoted_status' in json_data else False #coordinates = json_data['coordinates'] if 'coordinates' in json_data else None #reply_count = json_data['reply_count'] if 'reply_count' in json_data else None #lang = json_data['lang'] if 'lang' in json_data else None retweet_from_tweet_id = json_data['retweeted_status']['id'] if 'retweeted_status' in json_data else None if(id_str_user in follow ): location = json_data['user']['location'] if 'location' in json_data['user'] else None total_followers = json_data['user']['followers_count'] if 'followers_count' in json_data['user'] else None total_following = json_data['user']['friends_count'] if 'friends_count' in json_data['user'] else None total_posts = json_data['user']['statuses_count'] if 'statuses_count' in json_data['user'] else None #id_user # name #screen_name if not db_init.User.exists(id=id_user): db_init.User( id = id_user, screen_name_usuario = screen_name, name_usuario = name, total_followers = total_followers, total_following = total_following, total_posts = total_posts, location = location ) else: user_db = db_init.User[id_user] user_db.set( total_followers = total_followers, total_following = total_following, total_posts = total_posts ) tweet_type = "ORIGINAL" if(retweet_from_tweet_id): tweet_type = "ORIGINAL - RETWEET" elif(in_reply_to_status_id or in_reply_to_screen_name or in_reply_to_user_id ): tweet_type = "ORIGINAL - REPLY" elif(in_reply_to_status_id or in_reply_to_screen_name or in_reply_to_user_id ): tweet_type = "REPLY" line = ' '.join(re.sub("([@#][A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",text_full).split()) total_words = len(re.findall(r'\w+', line)) #print(in_reply_to_status_id," reply ID") elif(retweet_from_tweet_id): tweet_type ="RETWEET" else: tweet_type ="OTHER" if retweet_from_tweet_id: retweet_from_user_id = json_data['retweeted_status']['user']['id'] if 'user' in json_data['retweeted_status'] else None retweet_from_screen_name = json_data['retweeted_status']['user']['screen_name'] if 'user' in json_data['retweeted_status'] else None else: retweet_from_user_id = None retweet_from_screen_name = None if (tweet_type == "RETWEET"): if retweet_from_tweet_id: tw_id = retweet_from_tweet_id if db_init.Tweet.exists(id=tw_id): save = True else: print("tweet original nao existe") save = False else: print("retweet sem id original") save = False elif (tweet_type == "REPLY" ): if in_reply_to_status_id: tw_id = in_reply_to_status_id if db_init.Tweet.exists(id=tw_id): save = True else: print("tweet original nao existe") save = False else: print("reply sem id original") save = False else: save = True if save: tweet = db_init.Tweet( id_usuario = id_user, id_str_usuario = id_str_user, screen_name_usuario =screen_name, name_usuario = name, id = id, id_str = id_str, created_at = created_at, text_full = text_full, text = text, total_words = total_words, source = source, in_reply_to_user_id = in_reply_to_user_id, in_reply_to_status_id = in_reply_to_status_id, in_reply_to_screen_name = in_reply_to_screen_name, retweet_from_tweet_id = retweet_from_tweet_id, retweet_from_user_id = retweet_from_user_id, retweet_from_screen_name = retweet_from_screen_name, tweet_type = tweet_type )
def save_data(json_data): ### USER ### if 'user' in json_data: id_user = json_data['user']['id'] if not db_init.User.exists(id=id_user): description = json_data['user'][ 'description'] if 'description' in json_data['user'] else None followers_count = json_data['user'][ 'followers_count'] if 'followers_count' in json_data[ 'user'] else None friends_count = json_data['user'][ 'friends_count'] if 'friends_count' in json_data[ 'user'] else None id_str_user = json_data['user']['id_str'] if 'id_str' in json_data[ 'user'] else None screen_name = json_data['user'][ 'screen_name'] if 'screen_name' in json_data['user'] else None created_at = parser.parse( json_data['user'] ['created_at']) if 'created_at' in json_data['user'] else None is_translator = json_data['user'][ 'is_translator'] if 'is_translator' in json_data[ 'user'] else False name = json_data['user']['name'] if 'name' in json_data[ 'user'] else None statuses_count = json_data['user'][ 'statuses_count'] if 'statuses_count' in json_data[ 'user'] else None verified = json_data['user'][ 'verified'] if 'verified' in json_data['user'] else False location = json_data['user']['location'] if json_data['user'][ 'location'] else None geo_enabled = json_data['user'][ 'geo_enabled'] if 'geo_enabled' in json_data['user'] else False protected = json_data['user'][ 'protected'] if 'protected' in json_data['user'] else False lang = json_data['user']['lang'] if 'lang' in json_data[ 'user'] else None user = db_init.User(id=id_user, description=description, followers_count=followers_count, friends_count=friends_count, id_str=id_str_user, screen_name=screen_name, created_at=created_at, is_translator=is_translator, name=name, statuses_count=statuses_count, verified=verified, location=location, geo_enabled=geo_enabled, protected=protected, lang=lang) else: user = db_init.User[id_user] ### TWEET ### id = json_data['id'] if not db_init.Tweet.exists(id=id): id_str = json_data['id_str'] in_reply_to_user_id = json_data[ 'in_reply_to_user_id'] if 'in_reply_to_user_id' in json_data else None created_at = parser.parse(json_data['created_at']) favourite_count = json_data[ 'favourite_count'] if 'favourite_count' in json_data else None truncated = json_data['truncated'] text = json_data['text'] if truncated: text_full = json_data['extended_tweet']['full_text'] else: text_full = None contributors = json_data[ 'contributors'] if 'contributors' in json_data else None retweet_count = json_data[ 'retweet_count'] if 'retweet_count' in json_data else None in_reply_to_status_id = json_data[ 'in_reply_to_status_id'] if 'in_reply_to_status_id' in json_data else None filter_level = json_data[ 'filter_level'] if 'filter_level' in json_data else None quote_count = json_data[ 'quote_count'] if 'quote_count' in json_data else None #geo = json_data['geo'] if 'geo' in json_data else None source = json_data['source'] if 'source' in json_data else None possibly_sensitive = json_data[ 'possibly_sensitive'] if 'possibly_sensitive' in json_data else False in_reply_to_screen_name = json_data[ 'in_reply_to_screen_name'] if 'in_reply_to_screen_name' in json_data else None is_quoted_status = json_data[ 'is_quoted_status'] if 'is_quoted_status' in json_data else False #coordinates = json_data['coordinates'] if 'coordinates' in json_data else None reply_count = json_data[ 'reply_count'] if 'reply_count' in json_data else None lang = json_data['lang'] if 'lang' in json_data else None retweet_from_tweet_id = json_data['retweeted_status'][ 'id'] if 'retweeted_status' in json_data else None tweet = db_init.Tweet( user=user, id=id, id_str=id_str, in_reply_to_user_id=in_reply_to_user_id, created_at=created_at, favourite_count=favourite_count, truncated=truncated, text_full=text_full, text=text, contributors=contributors, retweet_count=retweet_count, in_reply_to_status_id=in_reply_to_status_id, filter_level=filter_level, quote_count=quote_count, #geo = geo, source=source, possibly_sensitive=possibly_sensitive, in_reply_to_screen_name=in_reply_to_screen_name, is_quoted_status=is_quoted_status, #coordinates = coordinates, reply_count=reply_count, lang=lang, retweet_from_tweet_id=retweet_from_tweet_id) else: tweet = db_init.Tweet[id] ### HASHTAGS ### for hashtag in json_data['entities']['hashtags']: db_init.Hashtag(tweet=tweet, text=hashtag['text']) ### URL ### for url in json_data['entities']['urls']: db_init.Url(tweet=tweet, expanded_url=url['expanded_url'], url=url['url'], display=url['display_url']) ### USER MENTION ### for mention in json_data['entities']['user_mentions']: db_init.UserMention(tweet=tweet, id_user=mention['id'], name=mention['name'], screen_name=mention['screen_name']) ### PLACE ### if (json_data['place']): full_name = json_data['place'][ 'full_name'] if 'full_name' in json_data['place'] else None place_type = json_data['place'][ 'place_type'] if 'place_type' in json_data['place'] else None country = json_data['place']['country'] if 'country' in json_data[ 'place'] else None country_code = json_data['place'][ 'country_code'] if 'country_code' in json_data[ 'place'] else None name = json_data['place']['name'] if 'name' in json_data[ 'place'] else None db_init.Place(tweet=tweet, full_name=full_name, name=name, place_type=place_type, country=country, country_code=country_code)
def save_data(json_data): #id = None if 'id' in json_data: id = json_data['id'] if not db_init.Tweet.exists(id=id): id_str = json_data['id_str'] created_at = parser.parse(json_data['created_at']) truncated = json_data['truncated'] if truncated : text_full = json_data['extended_tweet']['full_text'] else: text_full = json_data['text'] ### To check if it is a retweet retweet_from_tweet_id = json_data['retweeted_status']['id'] if 'retweeted_status' in json_data else None ### To check if it is a reply in_reply_to_user_id = json_data['in_reply_to_user_id'] if 'in_reply_to_user_id' in json_data else None in_reply_to_status_id = json_data['in_reply_to_status_id'] if 'in_reply_to_status_id' in json_data else None in_reply_to_screen_name = json_data['in_reply_to_screen_name'] if 'in_reply_to_screen_name' in json_data else None #USER id_user = json_data['user']['id'] id_str_user = json_data['user']['id_str'] if 'id_str' in json_data['user'] else None screen_name = json_data['user']['screen_name'] if 'screen_name' in json_data['user'] else None name = json_data['user']['name'] if 'name' in json_data['user'] else None if(id_str_user in follow ): ### SAVE TWEET NA BASE DE ORIGINAL TWEETS if(not retweet_from_tweet_id and not(in_reply_to_status_id or in_reply_to_screen_name or in_reply_to_user_id)): ## SAVE USER if not db_init.User.exists(id=id_user): db_init.User( id = id_user, screen_name_usuario = screen_name, name_usuario = name ) ## SAVE TWEET tweet = db_init.Tweet( id = id, id_str = id_str, id_usuario = id_user, id_str_usuario = id_str_user, created_at = created_at, name_usuario = name, text_full = text_full, ) print("ORIGINAL -> ",text_full) ### URL ### if truncated: urls = json_data['extended_tweet']['entities']['urls'] else: urls = json_data['entities']['urls'] for url in urls: db_init.Url(tweet = tweet, expanded_url = url['expanded_url'], url = url['url'], display = url['display_url']) #print(url['expanded_url']) #print(url['url']) #print(url['display_url']) #print("--------") #with open("temp_data/"+str(id)+'.json', 'w') as outfile: # json.dump(json_data, outfile) elif(in_reply_to_status_id or in_reply_to_screen_name or in_reply_to_user_id ): ### SAVE TWEET NA BASE DE REPLIES if in_reply_to_status_id: tw_id = in_reply_to_status_id if db_init.Tweet.exists(id=tw_id): ### USER if not db_init.User.exists(id=id_user): db_init.User( id = id_user, screen_name_usuario = screen_name, name_usuario = name ) ### TWEET_REPLY if not db_init.Tweet_Reply.exists(id=id): db_init.Tweet_Reply( id = id, id_str = id_str, id_usuario = id_user, id_str_usuario = id_str_user, created_at = created_at, text_full = text_full, in_reply_to_user_id = in_reply_to_user_id, in_reply_to_status_id = in_reply_to_status_id ) print("REPLY -> ",id_user," - ", text_full) #timeline.find_user_tweets(id_user) else: print("tweet original nao existe")