arul's code
free code snippets

Python script to tweet your Internet speed

This is a simple Python script that tweets your Internet speed. I wrote this script because my former Internet provider C* suddenly slowed down and had very slow speeds of 1Mbps; my plan was supposedly up to 100Mbps download/10Mbps upload. This is a variant of what I originally wrote.

To begin, you first need a Twitter account and have Python 2.7 installed. You will also have to install pip.

Steps

First, create a Twitter application. Go to https://apps.twitter.com/ and click on Create New App.

After creating the application, click on the tab that says Keys and Access Tokens. Generate the Consumer Key and Secret. Also, generate the Access Token and Token Secret. Do not give these values to anyone.

Click on the tab Permissions and set the access to Read and Write.

Download the speedtest.net command line script speedtest.py from GitHub. Save it in a directory of your choice. I copied mine to /usr/local/bin/speedtest.py.

Copy my script internetspeed.py to a directory of your choice. I copied mine to /usr/local/bin/internetspeed.py.


import subprocess
import tweepy
import requests

#
# Python script to tweet your Internet speed
# Arul John { http://aruljohn.com }
#

def main():
  script_path = '/usr/local/bin/speedtest.py'
  # Find IP address and remove the last 2 octets
  ip = None
  r = requests.get('https://api.aruljohn.com/ip')
  if r.status_code == requests.codes.ok:
    ip = r.text
    if ip != None:
      ip =  '.'.join(ip.split('.')[:2]) + '.xx.xx'

  # Get the Internet speed by calling speedtest-cli
  speed = subprocess.check_output(['python', script_path, '--simple'])
  speed = "My #InternetSpeed :\n" + speed
  if ip != None:
    speed += "IP address: " + ip

  # Twitter consumer key, secret, access token and secret
  consumer_key = ''
  consumer_secret = ''
  access_token = ''
  access_token_secret = ''
  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_token_secret)
  api = tweepy.API(auth)

  # Set status on Twitter
  status = api.update_status(status=speed)

  # Display information for logging
  print speed

if __name__ == "__main__":
  main()

Edit internetspeed.py and replace the four values (consumer_key, consumer_secret, access_token, access_token_secret) with the values in the Twitter application you just created. Also, replace the script_path value with the full path to the speedtest.py script.

You may or may not have the Python modules requests and `tweepy'. Let's install them.

  pip install requests
  pip install tweepy

Now that the dependencies are installed, run the script:

  python speedtest.py

To set a cronjob to run this script and tweet your Internet speed every 3 hours, you can use this line:


# Run every 3 hours
5 0-23/3 * * * /usr/bin/python /usr/local/bin/internetspeed.py >> /tmp/speedtest.log

This is what your tweet will look like. You can modify the script internetspeed.py accordingly to your style.

Please let me know if you run into any issues. Thanks for reading this post.

Share this

Last Updated: Posted on 05 Jan 2017