Let us build a simple Twitter Streaming Python script part 1 :).

This is a simple template for experimenting with the Twitter Streaming API using Python threading and queues to process the tweets that will be provided while using the Twitter search API.

I will also include a method that will be useful in encoding tweets in ASCII so that you can also use Windows terminal and Python to create a template of your own and turning the Python script into a website or webapp in ‘part 2

Requirements:

  • Pythons using Twython {pip install twython}
  • Your API access credentials {register/create your app here to get your API access credentials}

First, we need to have the twython installed.

Twython is a premier Python library that provides an easy and up to date ways to access the Twitter data. You can use Python 2.6+ and Python 3. It does query of data and image uploading seamlessly.

To install using pip:

$ pip install twython

To install using easy_install:

$ easy_install twython

To get the code directly from GitHub:

git clone https://github.com/ednasawe/twython.git
cd twython
python setup.py install

Any of the above three steps can be used.

For more documentation, check the twython docs.

Now let us create the access credentials:

create app to get the API access credential

During the registration and application of the Twitter access credentials, do not worry too much about the name, description, if you are just tinkering with the project.

After the registration, you will be provided with the new credentials that include the {consumer_key, consumer_secret, token, and token_secret} that will be found on the keys and the Access Tokens tab.

Twitter Streaming API enables you to continually load a stream of tweets based on the searching parameters you have provided in real time.

Using Python script, you can have the tweets displayed as they are retrieved while your script is still running. The tweet streams gets updated continually until you decide to stop the script by typing ctrl+c on the terminal (or windows terminal).

Using @adamdrake’s twitterstreamtemplate on Github and @victoriadrake Github, I will be able to make use of the queuing in Python and Twython 3.4.0 documentation.

Once you have your Twitter API Access credential, you can input it in the appropriate places.

You should have cloned the file already from here. Then on your terminal cd to the right folder, then type

git clone {https://github.com/ednasawe/twitter-stream-template.git}

On the tweet_stream.py file, insert the keys as shown below.

# Input your credentials below  
consumer_key = 'xxx'  
consumer_secret = 'xxx'  
token = 'xxx'  
token_secret = 'xxx'  

Next, you set the parameters for finding specific tweets or a stream of tweets sample using the line below:

stream.statuses.filter(track='twitter',  language='en')
stream.statuses.sample(language='en')

Now pass the code below to successfully pull up a stream of tweets based on the search parameter that you had provided above.

print(tweet)

Lasty, using the terminal, run the Python code to see the results:

python twitter_stream.py

You will be able to have results like this below:

sample of streamed tweets in real time

Use ctrl+c to stop the streaming of tweets on the terminal.

You can alter the stream results by changing the search parameters by changing this line of code:

stream.statuses.filter(track='twitter',  language='en')

The track parameter lets you filter your tweets by keywords, hashtags, user mentions, and urls. In the above sample, the search contains the keywork ‘twitter‘, which is accepted as a keyword phrase as shown here regarding consuming streaming data.

The language parameter limits the search results to display tweets written in English only. You can use other parameters like ‘follow’ and ‘location’ to search the timeline of the users that you specify and to filter by geolocation respectively. You can have access to more parameters to work with here.

stream.statuses.filter(track='#travel', language='en')

It will print something like this:

stream of tweets with defined parameters

Your results will vary depending on the keyword you use as your parameters. The Twitter Advanced Search page is a great resource for tinkering different combinations and run them on your script.

I hope you do find the sample and overview on how to create a Twitter streaming python script much fun and helpful.

Thank you for reading.

If you have any question or comment, do not hesitate to ask us.

Advertisement