Python 3 Twitter Searcher

Peachy Software logo


So I wrote a Twitter search in Python 3 a while back and thought I would write a tutorial on how you can use this to search Twitter for anything you like and not have to follow the people to find what you are looking for!

First, you will need to install via the Terminal a Python module called Tweepy, to do this copy and paste the below command into your Terminal...

pip3 install tweepy

This will install the tweepy module for you so we can call on it in the code.

You can read the Tweepy documentation here - https://tweepy.readthedocs.io/en/3.7.0/index.html

I would recommend using JetBrains PyCharm CE to edit and run this code as the Python console in PyCharm allows all kinds of formats where as IDLE does not! This means if a tweet gets called and printed to the console which has emotes or different language symbols python’s IDLE will throw an error.

You can install PyCharm from my downloads page
HERE

Once you have PyCharm installed on your machine create a new project and call it Twitter Searcher or something along those lines. Inside the project, file create a new Python file then copy and paste the below code into the coding area of the PyCharm.


from datetime import datetime
import tweepy
import json
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

with open('twitter_auth.json') as file: #Reads the json file for below keys
secrets = json.load(file)

auth = tweepy.OAuthHandler(secrets['consumer_key'], secrets['consumer_secret'])
auth.set_access_token(secrets['access_token'], secrets['access_token_secret'])

api = tweepy.API(auth)

class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_status(self, status):
print(status.text)
return True

def on_error(self, status_code):
if status_code == 420:
return False

if __name__ == '__main__':
print(datetime.today().replace(microsecond=0))
print('\nSearching Twitter ...')
l = StdOutListener()
stream = Stream(auth, l)
#Enter your search below...
stream.filter(track=['xbox', '#xbox'])



Before you run the code, we need to create a json file inside the python file directory, use your favourite text editor and create a blank file but make sure you change the file extension from .txt or .rtf to .json

json


OK now you have the .json file saved inside the python directory we need to create a developer account with Twitter so we can use the oAuth security keys which are personal to you (Your Twitter account)

To do this go to the following link and create a developer account on Twitter -
https://developer.twitter.com/en.html

Then create an app and generate your Keys and Token codes - here’s a good video on how to do this -
https://www.youtube.com/watch?v=WrZqF7qqvJ0

Once you have your Key and Token codes from Twitter you can now insert them inside your json file we created. Be sure to enter them in the same format as below…


{
"consumer_key": “YOUR KEY",
"consumer_secret": “YOUR SECRET",
"access_token": “YOUR ACCESS TOKEN",
"access_token_secret": “YOUR TOKEN SECRET"
}


Why do you need to this? Well the code inside the Python script reads this and takes the keys and tokens and assigns them to variables for us to send to Twitter so we can use the Twitter API which is the below part of the Python script…


with open('twitter_auth.json') as file: #Reads the json file for below keys
secrets = json.load(file)

auth = tweepy.OAuthHandler(secrets['consumer_key'], secrets['consumer_secret'])
auth.set_access_token(secrets['access_token'], secrets['access_token_secret'])

api = tweepy.API(auth)


Now we have done all the boring bits we are ready to run our code

NOTE - Make sure you change in the code the name of your json file to what ever you have named it!


with open('
twitter_auth.json') as file: #Reads the json file for below keys
secrets = json.load(file)


Just before we run the code make sure you enter what you would like to search for in the stream.filter line

Now you are ready to run the code and watch your search appear in the console.

If you have found this useful please let me know via my contact page or on Twitter @PESHuk, don’t be afraid of modifying my code to your own tastes, I personally find this useful for finding BETA codes for un-released games for the xbox, let me know what you use this script for!