def get_user(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, 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() #return "OK" #breakpoint() 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)) # 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() #breakpoint() #return "OK" return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets
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 get_user(screen_name=None): print(screen_name) api = twitter_api() user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150, exclude_replies=True, include_rts=False) 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() # get existing user from the db or initilaize a new one: return jsonify({"user": user._json, "tweets": [s._json for s in statuses]})
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"
def fetch_user_data(screen_name=None): print(screen_name) # get our user data for whichever twitter user api = twitter_api() twitter_user = api.get_user(screen_name) statuses = api.user_timeline(screen_name, tweet_mode="extended", count=150, exclude_replies=True, include_rts=False) # # STORE USER # # get existing user from the db # or initialize a new one if the query returns None db_user = User.query.get(twitter_user.id) or User(id=twitter_user.id) # assign column values to the twitter information 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 # add user to database db.session.add(db_user) db.session.commit() #breakpoint() #return "OK" #return render_template("user.html", user=db_user, tweets=statuses) # tweets=db_tweets # # STORE TWEETS # # initialize basilica api basilica_api = basilica_api_client() # create list of tweet texts all_tweet_texts = [status.full_text for status in statuses] # pass tweets to api so it can embed the sentences using the twitter model 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() #breakpoint() return "OK"