Use Python to monitor Twitter accounts for crypto tweets

Ledger Nano X - The secure hardware wallet

Crypto Twitter is very popular in the Crypto Currency industry. It is a forum for communicating the latest Crypto news and information. In the past, tweets have generated a lot of positive and or negative excitement which have caused movements in the crypto markets. In addition it has been useful to help gauge the general sentiment in the industry.

The Python code below can be used to monitor a Twitter account, parses the latest tweet and looks for a specific key word. If the word is found in the tweet it prints out a statement in your IDE. This can be expanded to perform many different functions. As an example one can expand the function below to:

Getting Started

To run the code that listens to tweets you need to do the following:

  • Go to Twitter and register for a developer account. It is very easy and fast.
  • In the developer section of Twitter create a new project
  • After you create a new project you will be given an apikey, secret, accesstoken, accesstoken secret.
  • All of these inputs are needed to connect to Twitter and are required for this Python project
  • PIP install tweepy in your Python project. To read more go here – https://docs.tweepy.org/en/latest/install.html
  • PIP install re and time. These are additional dependencies that are required

Now you are ready to get started implementing your Python script. In the code below change the following parameters:

  • the authorization parameters
  • input a users screen name – name of account you want to monitor. You can use the screen name.
  • change the search word “test” to monitor for a word of your choice. As an example “rocket”, “car”, “bitcoin”.

Read the comments in the code below. This sample is just enough to get you started. There is a lot more customization you can do.

import tweepy
import re
import time


auth = tweepy.OAuthHandler(yourapikey, yoursecret)
auth.set_access_token(youraccessToken, youraccessTokenSecret)
api = tweepy.API(auth)
print('logged into Twitter')


def twitter_tweets():
    #Use tweepy api to go to a user account and get the last actual tweet.  Don't include replies.
    tweet = api.user_timeline(screen_name='nameofaccounttomonitor', tweet_mode='extended', exclude_replies='true')
    print(tweet[0].full_text)

    #convert tweet into a string
    txt = str(tweet)

    #convert the text to lower case and use regex to look for a specific key word "test"
    test = re.search('test', str.lower(txt))

    #use an if statement to print your results
    if test:
        print(f'There is a Twitter match for test', str(time.ctime()))
    else:
        print('There is no Twitter match for test')

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Next – Can you make money creating an arbitrage bot running on the Ethereum block chain?

Leave a Reply