skip to Main Content

I’m streaming Twitter data from the API into a Postgres database by modeling this script. Using those exact methods, I’m able to stream the data successfully into the two tables (one containing user_id/user_name, and the other containing data). I’ve been able to make minor changes to extract a few other bits of information, but using these methods I’m only collecting retweets given a keyword list and I would like to collect all tweets given the list. Based on the way the original script is collecting/storing retweet user_ids and user_names, I changed the code tried to stream into a new table without making any references to retweets. Unfortunately, the result of this were two empty tables. The code ran fine otherwise, and was printing statements to the terminal, there was just no data. Why would this be? Below is my code:

import psycopg2
import tweepy
import json
import numpy as np

# Importing postgres credentials
import postgres_credentials

# Importing twitter credentials
import twitter_credentials


# Accesing twitter from the App created in my account
def autorize_twitter_api():
"""
This function gets the consumer key, consumer secret key, access token
and access token secret given by the app created in your Twitter account
and authenticate them with Tweepy.
"""
# Get access and costumer key and tokens
auth = tweepy.OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)

return auth


def create_tweets_table(term_to_search):
"""
This function open a connection with an already created database and creates a new table to
store tweets related to a subject specified by the user
"""

# Connect to Twitter Database created in Postgres
conn_twitter = psycopg2.connect(dbname=postgres_credentials.dbname, user=postgres_credentials.user, password=postgres_credentials.password, host=postgres_credentials.host,
                                    port=postgres_credentials.port)

# Create a cursor to perform database operations
cursor_twitter = conn_twitter.cursor()

# with the cursor now, create two tables, users twitter and the corresponding table according to the selected topic
cursor_twitter.execute("CREATE TABLE IF NOT EXISTS test_twitter_users (user_id VARCHAR PRIMARY KEY, user_name VARCHAR);")

query_create = "CREATE TABLE IF NOT EXISTS %s (id SERIAL, created_at_utc timestamp, tweet text NOT NULL, user_id VARCHAR, user_name VARCHAR, PRIMARY KEY(id), FOREIGN KEY(user_id) REFERENCES twitter_users(user_id));" % (
            "test_tweet_text")
cursor_twitter.execute(query_create)

# Commit changes
conn_twitter.commit()

# Close cursor and the connection
cursor_twitter.close()
conn_twitter.close()
return


def store_tweets_in_table(term_to_search, created_at_utc, tweet, user_id, user_name):
"""
This function open a connection with an already created database and inserts into corresponding table
tweets related to the selected topic
"""

# Connect to Twitter Database created in Postgres
conn_twitter = psycopg2.connect(dbname=postgres_credentials.dbname, user=postgres_credentials.user, password=postgres_credentials.password, host=postgres_credentials.host,
                                    port=postgres_credentials.port)

# Create a cursor to perform database operations
cursor_twitter = conn_twitter.cursor()

# with the cursor now, insert tweet into table
cursor_twitter.execute(
    "INSERT INTO test_twitter_users (user_id, user_name) VALUES (%s, %s) ON CONFLICT(user_id) DO NOTHING;",
    (user_id, user_name))

cursor_twitter.execute(
    "INSERT INTO %s (created_at_utc, tweet, user_id, user_name) VALUES (%%s, %%s, %%s, %%s);" % (
                'test_tweet_text'),
    (created_at_utc, tweet, user_id, user_name))

# Commit changes
conn_twitter.commit()

# Close cursor and the connection
cursor_twitter.close()
conn_twitter.close()
return


class MyStreamListener(tweepy.StreamListener):
'''
def on_status(self, status):
    print(status.text)
'''

def on_data(self, raw_data):

    try:
        global term_to_search

        data = json.loads(raw_data)

        # Obtain all the variables to store in each column
        user_id = data['user']['id']
        user_name = data['user']['name']
        created_at_utc = data['created_at']
        tweet = data['text']

        # Store them in the corresponding table in the database
        store_tweets_in_table(term_to_search, created_at_utc, tweet, user_id, user_name)

    except Exception as e:
        print(e)

def on_error(self, status_code):
    if status_code == 420:
        # returning False in on_error disconnects the stream
        return False

########################################################################

while True:
if __name__ == "__main__":
    # Creates the table for storing the tweets
    term_to_search = ["donald trump","trump"]
    create_tweets_table(term_to_search)

    # Connect to the streaming twitter API
    api = tweepy.API(wait_on_rate_limit_notify=True)

    # Stream the tweets
    try:
        streamer = tweepy.Stream(auth=autorize_twitter_api(), listener=MyStreamListener(api=api),tweet_mode='extended')
        streamer.filter(track=term_to_search)
    except:
        continue

2

Answers


  1. Chosen as BEST ANSWER

    I discovered the issue - I was creating two new tables, but inserting data into two different tables.


  2. What happen if you print the values in this function? do you have values there?

    def on_data(self, raw_data):
    
        try:
            global term_to_search
    
            data = json.loads(raw_data)
    
            # Obtain all the variables to store in each column
            user_id = data['user']['id']
            user_name = data['user']['name']
            created_at_utc = data['created_at']
            tweet = data['text']
    
            # Store them in the corresponding table in the database
            store_tweets_in_table(term_to_search, created_at_utc, tweet, user_id, user_name)
    
        except Exception as e:
            print(e)
    
    

    When you print the sql statements, can you see the inserts without data?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search