def create_user(): print("CREATING A NEW USER...") print("FORM DATA:", dict(request.form)) # todo: create a new user # return jsonify({"message": "CREATED OK (TODO)"}) if "username" in dict(request.form): name = request.form["username"] country = request.form["country"] db.session.add(User(name=name)) # db.session.add(User(country=country)) db.session.commit() return jsonify({"message": "CREATED OK", "name": name}) else: return jsonify({"message": "OOPS PLEASE SPECIFY A NAME!"})
def create_user(): print("FORM DATA:", dict(request.form)) # todo: store in database print(request.form["screen_name"]) api = api_client() twitter_user = api.get_user(request.form["screen_name"]) statuses = api.user_timeline( request.form["screen_name"], tweet_mode="extended", count=150) #, exclude_replies=True, include_rts=False) print("STATUSES COUNT:", len(statuses)) # STORE USER # get existing user from the db or initialize a new one: db_user = User.query.get(twitter_user.id) or User( id=twitter_user.id) #if not in DB add to db. db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #breakpoint() # STORE TWEETS all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_connection.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) # TODO: explore using the zip() function maybe... counter = 0 for status in statuses: print(status.full_text) print("----") #print(dir(status)) # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return redirect(f"/user_list")
def fetch_user(screen_name=None): print(screen_name) # FETCHING DATA FROM TWITTER API twitter_user = twitter_api_client.get_user(screen_name) # STORING TWITTER DATA IN THE DATABASE # get existing user from the db OR initialize a new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #return "OK" #breakpoint() # FETCH TWEETS tweets = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=150) print("TWEETS COUNT:", len(tweets)) # STORING TWITTER DATA IN THE DATABASE all_tweet_texts = [status.full_text for status in tweets] embeddings = list(basilica_api_client.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) for index, status in enumerate(tweets): print(index) print(status.full_text) print("----") #embedding = basilica_api_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet #print(len(embedding)) embedding = embeddings[index] # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return "OK" #return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets
def fetch_user_data(screen_name=None): print(screen_name) api = api_client() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150) print("Statuses count", len(statuses)) # # STORE USER # # get existing user from the db or initialize a new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() # # STORE TWEETS # all_tweet_texts = [status.full_text for status in statuses] embeddings = list( connection.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) # TODO: explore using the zip() function maybe... counter = 0 for status in statuses: print(status.full_text) print("----") #print(dir(status)) # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_connection.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return "OK"
def fetch_user(screen_name=None): print(screen_name) api = twitter_api() twitter_user = api.get_user(screen_name) # Get user from database if exists, if not initialaize new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count # store user in a database: db.session.add(db_user) db.session.commit() # Get tweets: basilica_api = basilica_api_client() tweets = api.user_timeline(screen_name, tweet_mode="extended", count=150) # exclude_replies=True, include_rts=False) all_tweet_texts = [status.full_text for status in tweets] embeddings = list( basilica_api.embed_sentences(all_tweet_texts, model="twitter")) print("Number Of Embeddings", len(embeddings)) for index, status in enumerate(tweets): print(index) print(status.full_text) print("----") embedding = embeddings[index] # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text # embedding = embeddings[counter] print(len(embeddings)) db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return "OK"
def store_twitter_user_data(screen_name): api = twitter_api_client() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=200, exclude_replies=True, include_rts=False) # return jsonify({"user": user._json, "tweets": [s._json for s in statuses]}) # store users db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #return "OKAY" # store basilica embedded tweets print("STATUS COUNT:", len(statuses)) basilica_api = basilica_api_client() all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_api.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) # TODO: explore using the zip() function maybe... counter = 0 for status in statuses: print(status.full_text) print("----") #print(dir(status)) # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return db_user, statuses
def get_user(screen_name=None): print(screen_name) api = twitter_api_client() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150, exclude_replies=True, include_rts=False) print("STATUSES COUNT:", len(statuses)) #return jsonify({"user": user._json, "tweets": [s._json for s in statuses]}) # get existing user from the db or initialize a new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() basilica_api = basilica_api_client() all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_api.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) # TODO: explore using the zip() function maybe... counter = 0 for status in statuses: print(status.full_text) print("----") # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, confirmed=False) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash( 'Congratulations, you are almost registered user! Just confirm your email now!' ) #connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672)) connection = pika.BlockingConnection( pika.ConnectionParameters('RabbitMQ', port=5672)) channel = connection.channel() channel.queue_declare(queue='RabbitMQ') serializer = URLSafeTimedSerializer(app.config['SECRET_KEY']) token = serializer.dumps(user.email, salt=app.config['SECURITY_PASSWORD_SALT']) confirm_url = url_for('confirm', token=token, _external=True) channel.basic_publish(exchange='', routing_key='RabbitMQ', body=pickle.dumps((user.email, confirm_url))) connection.close() return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def get_user(screen_name=None): print(screen_name) twitter_user = twitter_api_client.get_user(screen_name) # breakpoint() # find or create database user: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() return render_template("user.html", user=db_user)
def register(): if current_user.is_authenticated: return redirect(url_for('Home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() # flash(f'Account created for {form.username.data}!', 'success') flash('Your account is created and you can login now!', 'success') return redirect(url_for('login')) return render_template('register.html', titile='Register', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): hashed = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, name=form.name.data, password=hashed) db.session.add(user) db.session.commit() flash(f'Account created for {form.username.data}! You can now log in.', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def get_user(screen_name=None): print(screen_name) try: twitter_user = twitter_api_client.get_user(screen_name) # find or create database user: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #breakpoint() statuses = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=100, exclude_replies=True, include_rts=False) print("STATUS COUNT:", len(statuses)) all_tweet_texts = [status.full_text for status in statuses] embeddings = list(basilica_client.embed_sentences(all_tweet_texts, model="twitter")) # TODO: explore using the zip() function maybe... counter = 0 for status in statuses: print(status.full_text) print("----") #print(dir(status)) # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter+=1 db.session.commit() return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets except Exception as e: print(e) return jsonify({"message": "OOPS Something went wrong", "details": str(e)})
def store_twitter_user_data(screen_name): # Get the username from the twitter api and save the # user and tweets objects api = twitter_api_client() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150, exclude_replies=True, include_rts=False) # Check to see if the user already exists in the db user table and # if not then add it to the db user table db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() print("STATUS COUNT:", len(statuses)) # Use the basilica api to turn the tweets (statuses) into numeric lists basilica_api = basilica_api_client() all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_api.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) # Put all of the tweets in the db tweet table counter = 0 for status in statuses: print(status.full_text) print("----") # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return db_user, statuses
def get_user(screen_name=None): print(screen_name) twitter_user = twitter_api.get_user(screen_name) statuses = twitter_api.user_timeline(screen_name, tweet_mode="extended", count=150, exclude_replies=True, include_rts=False) #return jsonify({ # "user": user._json, # "tweets": [status.full_text for status in statuses] #}) # get existing user from the db or initialize a new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #return "OK" #breakpoint() all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_connection.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) counter = 0 for status in statuses: print(status.full_text) print("----") #print(dir(status)) # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_connection.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet db_tweet.embedding = embeddings[counter] db.session.add(db_tweet) counter += 1 db.session.commit() #breakpoint() return "OK"
def fetch_data(screen_name): print("FETCHING..", screen_name) # fetch user info user = twitter_api.get_user(screen_name) # store user info in database db_user = User.query.get(user.id) or User(id=user.id) db_user.screen_name = user.screen_name db_user.name = user.name db_user.location = user.location db_user.followers_count = user.followers_count db.session.add(db_user) db.session.commit() # fetch their tweets #statuses = twitter_api.user_timeline(screen_name, tweet_mode="extended", count=35, exclude_replies=True, include_rts=False) statuses = twitter_api.user_timeline(screen_name, tweet_mode="extended", count=150) print("STATUSES", len(statuses)) # fetch embedding for each tweet tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_conn.embed_sentences(tweet_texts, model="twitter")) print("EMBEDDINGS", len(embeddings)) # store tweets in db w embeddings for index, status in enumerate(statuses): print(status.full_text) print("----") #print(dir(status)) # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text #embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[index] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return f"FETCHED {screen_name} OK"
def get_user(screen_name=None): print(screen_name) twitter_user = twitter_api_client.get_user(screen_name) statuses = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=150) print("STATUSES COUNT:", len(statuses)) #return jsonify({"user": user._json, "tweets": [s._json for s in statuses]}) # get existing user from db or initilize a new one db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_counts = twitter_user.followers_count db.session.add(db.user) db.session.commit() #return "OK" #breakpoint() all_tweets_texts = [status.full_text for status in statuses] embeddings = list( basilica_api_client.embed_sentences(all_tweets_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) counter = 0 for status in statuses: print(status.full_text) print("-----") # get existing tweet from the db or initalize a new one db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return "OK" return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, isActive=False) db.session.add(user) db.session.commit() sendEmail(user) flash( 'Congratulations, message with further instructions will be send in one moment!' ) return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def fetch_user_data(screen_name): print('Fetching...', screen_name) # fetch user info user = twitter_api.get_user(screen_name) # store user in database db_user = User.query.get(user.id) or User(id=user.id) db_user.screen_name = user.screen_name db_user.name = user.name db_user.location = user.location db_user.followers_count = user.followers_count db.session.add(db_user) # fetch their tweets statuses = twitter_api.user_timeline(screen_name, tweet_mode='extended', count=100, exclude_replies=True, include_rts=False) print('Statuses:', len(statuses)) tweet_texts = [status.full_text for status in statuses] # fetch embedding for each tweet embeddings = list( basilica_connection.embed_sentences(tweet_texts, model="twitter")) print('Embeddings:', len(embeddings)) for index, status in enumerate(statuses): print(status.full_text) print("----") #print(dir(status)) # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = embeddings[index] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) # store tweets in database( w/embeddings) db.session.commit() return jsonify({'user': user._json, 'num_tweets': len(statuses)})
def create_user(): print("FORM DATA:", dict(request.form)) # return jsonify({ # "message": "USER CREATED OK", # "user": dict(request.form) # }) # INSERT INTO users ... (store data in the database) new_user = User( screen_name=request.form["screen_name"], name=request.form["name"], location=request.form["location"], followers_count=request.form["followers_count"]) db.session.add(new_user) db.session.commit() return redirect("/users")
def show_user(screen_name=None): print("SHOWING USER:"******"extended", count=50, exclude_replies=True, include_rts=False) for status in statuses: print(status.full_text) # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) print(db_tweet) # Update database tweet: db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = basilica_client.embed_sentence( status.full_text, model="twitter" ) # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return render_template("user_profile.html", user=db_user, tweets=db_user.tweets) except Exception as e: print(e) return jsonify( {"message": "OOPS THERE WAS AN ERROR. PLEASE TRY ANOTHER USER."})
def fetch_user_data(screen_name): print("FETCHING:", screen_name) #fetch user info user = twitter_api.get_user(screen_name) #store user in database db_user = User.query.get(user.id) or User(id=user.id) db_user.screen_name = user.screen_name db_user.name = user.name db_user.location = user.location db_user.followers_count = user.followers_count db.session.add(db_user) db.session.commit() #fetch their tweets #statuses = twitter_api.user_timeline(screen_name, tweet_mode="extended", count=35, exclude_replies=True, include_rts=False) statuses = twitter_api.user_timeline(screen_name, tweet_mode="extended", count=150) print("STATUSES:", len(statuses)) #fetch embeddings for each tweet tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_connection.embed_sentences(tweet_texts, model="twitter")) print("EMBEDDINGS:", len(embeddings)) #store their tweets in database (with embeding) for index, status in enumerate(statuses): print(status.full_text) print("----") db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id db_tweet.full_text = status.full_text embedding = embeddings[index] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return f"FETCHED {screen_name} OK"
def get_user(screen_name=None): print(screen_name) try: twitter_user = twitter_api_client.get_user(screen_name) # find or create database user: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #breakpoint() statuses = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=50, exclude_replies=True, include_rts=False) for status in statuses: print(status.full_text) print("----") #print(dir(status)) # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = basilica_client.embed_sentence( status.full_text, model="twitter" ) # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) db.session.commit() return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets except: return jsonify({"message": "OOPS User Not Found!"})
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('Home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your account password has been updated and you can login now!', 'success') return redirect(url_for('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def get_user(screen_name=None): print(screen_name) try: twitter_user = twitter_api_client.get_user(screen_name) # find or create database user: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() #breakpoint() statuses = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=50, exclude_replies=True, include_rts=False) db_tweets = [] for status in statuses: print(status.full_text) print("----") # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text with basilica.Connection(API_KEY) as c: embedding = list(c.embed_sentence(status.full_text, model = "twitter")) db_tweet.embedding = embedding db.session.add(db_tweet) db_tweets.append(db_tweet) db.session.commit() return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets except: return jsonify({"message": "OOPS User Not Found!"})
def new_user(): try: screen_name = request.form['screen_name'] twitter_api_client = twitter_api() twitter_user = twitter_api_client.get_user(screen_name) db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name.lower() db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db_user.profile = twitter_user.profile_image_url_https db.session.add(db_user) db.session.commit() statuses = twitter_api_client.user_timeline(screen_name, tweet_mode="extended", count=200, exclude_replies=True, include_rts=False) statuses_full_text = [status.full_text for status in statuses] embeddings = basilica_client.embed_sentences(statuses_full_text, model='twitter') embeddings_list = [embedding for embedding in embeddings] counter = 0 for status in statuses: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id db_tweet.full_text = status.full_text db_tweet.embedding = embeddings_list[counter] db.session.add(db_tweet) counter += 1 db.session.commit() flash(f'User "{twitter_user.name}" successfully added!') return redirect('/') except Exception as e: print(e) return redirect('/user_not_found')
def register(): form = RegForm() if request.method == 'GET': if User.query.count() > 0: return make_response('Нет такой страницы', 404) role = Role.query.filter_by(role='admin').one_or_none() if role is None: role = Role(id=1, role='admin') db.session.add(role) try: db.session.commit() except IntegrityError: db.session.rollback() raise InternalServerError( "failed to write admin role to the database") form.username.render_kw = {'value': 'admin'} choices = [(role.id, role.role)] # form.role.choices = [(role.id, role.role)] form.role.render_kw = {'value': choices[0][0]} form.phone.render_kw = {'value': '79272123456'} return render_template('login/register.html', form=form, choices=choices) if form.validate_on_submit(): user = User(username=Config.DEF_USER, id=1) user.set_password(Config.DEF_PASSWORD) user.role_id = int(form.role.data) user.phone_number = Config.PHONE db.session.add(user) try: db.session.commit() except IntegrityError: db.session.rollback() raise InternalServerError( "failed to write admin user to the database") login_user(user, remember=True) identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) return redirect(url_for('admin_app.index')) return form.errors return { 'user': form.username.data, 'password': form.password.data, 'password2': form.password2.data, 'role': form.role.data, 'phone': form.phone.data, 'choices': form.role.choices }
def store_twitter_user_data(screen_name): print(screen_name) api = twitter_api_client() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150) db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() print("STATUS COUNT:", len(statuses)) basilica_api = basilica_api_client() all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_api.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS", len(embeddings)) counter = 0 for status in statuses: print(status.full_text) print("----") # print(dir(status)) # Find or create database tweet: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text # embedding = basilica_client.embed_sentence(status.full_text, model="twitter") # todo: prefer to make a single request to basilica with all the tweet texts, instead of a request per tweet embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return db_user, statuses
def seed_db(): # print(type(db)) api = twitter_api_client() for screen_name in ['elonmusk','justinbieber','s2t2']: twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150) # print("STATUSES COUNT:", len(statuses)) # get existing user from the db or initialize a new one: db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() basilica_api = basilica_api_client() all_tweet_texts = [status.full_text for status in statuses] embeddings = list(basilica_api.embed_sentences(all_tweet_texts, model="twitter")) # print("NUMBER OF EMBEDDINGS", len(embeddings)) counter = 0 for status in statuses: # print(status.full_text) # print("----") # get existing tweet from the db or initialize a new one: db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id # or db_user.id db_tweet.full_text = status.full_text embedding = embeddings[counter] # print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter+=1 db.session.commit() return jsonify({"message": "DB SEEDED OK"})
def add_to_database(): user=request.form['name'] tweets = client.user_timeline(user, tweet_mode="extended", count=200, exclude_replies=True, include_rts=False) userid = client.get_user(user).id try: db.session.add(User(name=user, id=userid)) db.session.commit() for tweet in tweets: interactions = tweet.retweet_count + tweet.favorite_count embedded = c.embed_sentence(tweet.full_text, model='twitter') db.session.add(Tweet(user_id=userid, status=tweet.full_text, id=tweet.id, embedding=embedded, interactions=interactions)) db.session.commit() except IntegrityError or InvalidRequestError: db.session.rollback() existing_ids = Tweet.query.filter(Tweet.user_id == userid).all() for tweet in tweets: if tweet.id not in existing_ids: interactions = tweet.retweet_count + tweet.favorite_count embedded = c.embed_sentence(tweet.full_text, model='twitter') db.session.add(Tweet(user_id=userid, status=tweet.full_text, id=tweet.id, embedding=embedded, interactions=interactions)) return jsonify({"message": "User and existing tweets added to database"})
def fetch_user_data(screen_name=None): print(screen_name) api = twitter_api() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150) print("STATUSES COUNT:", len(statuses)) # store users in the database db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) db_user.screen_name = twitter_user.screen_name db_user.name = twitter_user.name db_user.location = twitter_user.location db_user.followers_count = twitter_user.followers_count db.session.add(db_user) db.session.commit() # store tweets in the database all_tweet_texts = [status.full_text for status in statuses] embeddings = list( basilica_connection.embed_sentences(all_tweet_texts, model="twitter")) print("NUMBER OF EMBEDDINGS:", len(embeddings)) counter = 0 for status in statuses: print(status.full_text) print('----') db_tweet = Tweet.query.get(status.id) or Tweet(id=status.id) db_tweet.user_id = status.author.id db_tweet.full_text = status.full_text embedding = embeddings[counter] print(len(embedding)) db_tweet.embedding = embedding db.session.add(db_tweet) counter += 1 db.session.commit() return "OK"