def update_tweet_info(tweet: dict): """ Updates number of likes and number of retweets in tweets table. """ cur.execute('''UPDATE tweets SET(no_likes, no_retweets) = (%s, %s) WHERE tweet_id = (%s);''', (tweet['favorite_count'], tweet['retweet_count'], tweet['id'])) conn.commit()
def add_followers(who: int, whom: int, followed_at: datetime): """ Insert information about followers to the table 'follows' - id, id of the followed person and date. Each row must be unique. If the record already exists, it raises an exception and will not insert it again. """ try: cur.execute('''INSERT INTO follows(who, whom, followed_at) VALUES (%s, %s, %s);''', (who, whom, followed_at)) conn.commit() except: conn.rollback()
def insert_followers_sum(user: dict): """ Insert a brief info about number of followers at given day. """ try: cur.execute("""INSERT INTO followers_sum (who, checked_at, followers) VALUES (%s, %s, %s) """, (user['id'], date.today(), user['followers_count'])) conn.commit() except: conn.rollback()
def add_tweet_info(tweet: dict): """ Insert information about tweets to the table 'tweets' - tweet_id, user_id, date, number of likes and number of retweets. """ try: cur.execute('''INSERT INTO tweets(tweet_id, user_id, tweet_date, no_likes, no_retweets) VALUES (%s, %s, %s, %s, %s);''', ( tweet['id'], tweet['user']['id'], tweet['created_at'], tweet['favorite_count'], tweet['retweet_count'] )) conn.commit() except: conn.rollback()
def add_new_user(user: dict, do_check: bool = False) -> dict: """ Verify, if the user is in the database. If not, then it will do so. """ cur.execute('''SELECT * FROM twitter_user WHERE id = %s;''', (user['id'],)) row = cur.fetchone() if not row: cur.execute('''INSERT INTO twitter_user(id, screen_name, do_check) VALUES (%s, %s, %s);''', (user['id'], user['screen_name'], do_check)) conn.commit() row = { 'id': user['id'], 'screen_name': user['screen_name'], 'do_check': do_check, } return row
def update_do_check(user: dict, status: bool): """ Change the value in the do_check column. """ cur.execute('''UPDATE twitter_user SET do_check = %s WHERE id = %s''', (status, user['id'])) conn.commit()