def remove_all_tweets(): """Empties the database""" try: db = next(get_db()) delete_all_tweets(db) finally: db.close()
def remove_tweets(ids: List[int]): """ Deletes the specified tweets from the database Parameters: ids (list): A list containing the ids of the tweets to be deleted """ for id in tqdm(ids): try: db = next(get_db()) delete_tweet(db, id) finally: db.close()
def new_tweets_only(tweet_df: pd.DataFrame) -> int: """ Puts tweets into the database. Gets rid of duplicates so that all new tweets can be added at the same time saving significant amounts of time. """ tweets = [ TweetCreate( text=tweet_df.iloc[i]["text"], user=tweet_df.iloc[i]["user"], search_query=tweet_df.iloc[i]["search_query"], favorite_count=tweet_df.iloc[i]["favorite_count"], retweet_count=tweet_df.iloc[i]["retweet_count"], follower_count=tweet_df.iloc[i]["follower_count"], media=tweet_df.iloc[i]["media"], is_reply=tweet_df.iloc[i]["is_reply"], is_retweet=tweet_df.iloc[i]["is_retweet"], has_mentions=tweet_df.iloc[i]["has_mentions"], is_funny=tweet_df.iloc[i]["is_funny"], ) for i in range(len(tweet_df)) ] try: # Gets a list containing the text of all tweets in the database db = next(get_db()) tweet_text = [tweet.text for tweet in read_tweets(db, 0, 1000000)] db.close() # Creates a new list containing only the tweets not already in the database new_tweets = [tweet for tweet in tweets if tweet.text not in tweet_text] num_tweets = len(new_tweets) db = next(get_db()) # Adds all of the new tweets as a batch print("Tweets filtered. Posting to database...") create_tweets(db, new_tweets) except Exception as e: print(e) finally: db.close() return num_tweets # , tweet_text, new_tweets, tweet_df
def get_tweet(tweet_id: int) -> models.Tweet: """ Gets the tweet with the specified id from the database Parameters: tweet_id (int): the id of the tweet to query from the database Returns: (models.Tweet): A tweet object from the database with the specified id """ try: db = next(get_db()) tweet = read_tweet(db, tweet_id) finally: db.close() return tweet
def get_tweets(count: int = 100) -> List[models.Tweet]: """ Gets the first n=count tweets from the database Parameters: count (int): the number of tweets to query from the database Returns: (list): A list containing the first n=count tweets from the database """ try: db = next(get_db()) tweets = read_tweets(db, limit=count) finally: db.close() return tweets
def get_query() -> query.Query: """Returns a sqlalchemy.orm.query.Query object for custom queries""" db = next(get_db()) return custom_query(db)