The other day, one of my managers at work sent me a link because he said it sounded like something I would do. I clicked on the link and it led me to a Reddit post where this guy set up a Raspberry Pi to automatically tweet at his internet service provider whenever he was getting network speeds that were slower than what they advertised for his plan. Unfortunately, I don’t have a Raspberry Pi, but that didn’t stop me from wanting to figure out how to do it with my laptop, so that’s what I did. And now I’m going to share with you how I accomplished it.
Note – Not long after I figured out how to do this, I realized that the guy with the Raspberry Pi posted his python code online. I feel kind of silly for not actually reading the article before attempting to figure this out on my own. However, his code is quite a bit more complex than mine and I’m not really that familiar with Python yet. I’ve learned a great deal more about the tools I’m using and exactly what’s going on in the script by figuring it out as I went. Plus I really enjoyed the whole process and I can explain things in more detail because I understand what I’m sharing.
There are quite a few steps to this, but it’s really not as daunting as it may seem if you break it down.
Prerequisites
I am using Python 2.7, which is what came standard in my Ubuntu 18.04 install. I have not tested this with Python 3. You can see what version of Python you have by running the following command in a terminal.
I recommend creating a new Twitter account for this project as opposed to using your main account. If you are anything like me, you’ll be testing things and tweeting all kinds of stuff that your followers probably don’t want to see.
You will need to sign up with your Twitter account to become a developer (which we will go over) so you can create an app that will give you the necessary keys to interact with Twitter from a command line or script.
You must have a phone number associated to your Twitter account for this to work. You can use the same phone number you use on your main account if you wish.
You must have Speedtest CLI installed to run the network speed tests. You can learn how to do that by checking out this tutorial. -> Test Your Network Speed from the Command Line in Ubuntu 18.04
Get Python Ready
Start off by installing the python-pip package management system.
Then use python-pip to install a python library called tweepy, which will provide easy access to the Twitter API.
Sign Up for a Developer Account
Login to your Twitter account and then go here. -> https://apps.twitter.com/
Click the Create an app button. This will prompt you to apply for a developer account. Click the Apply button.
You will be sent to a page that wants to know your primary reason for using Twitter developer tools. There are many options to choose from here. I went to the Other column and chose the Doing something else option, then clicked the Next button in the bottom right corner.
The following page will want you to confirm it’s you. Once you’ve done this, click the Next button in the bottom right corner.
Now you should be on a page that asks for a description of how you will be using the Twitter API. This has a 200 character minimum. My exact words -> This is for testing purposes. I want to learn more about how the API works and how to interact with Twitter from a Linux command line. This is something that interests me and I think it will be very useful. Feel free to copy this if you don’t know what to say here. There are several toggle switches on this page. I set them all to No, then clicked the Next button in the bottom right corner.
Double check everything on the next page and click the Looks good! button in the bottom right corner.
And finally, you will need to accept the Developer Agreement and click the Submit Application button in the bottom right corner. This will send a confirmation link to the email associated to your Twitter account. Note – This will most likely go to your spam folder, just so you know.
Create Your Twitter Application
After clicking the confirmation link in your email, you will be sent to a welcome page. Click on the Create an app button. This should bring you back to the original page you started on. And again, you will need to click on the Create an app button.
Here you can provide your app details. Your app name must be unique. If the name is already in use, you’ll have to choose another. The application description can be between 10 and 200 characters. You can just say it’s for auto tweeting or testing purposes or whatever. It also asks for a Website URL. You can put the url to your Twitter profile if you don’t know what to put here. The only other required field on this page is at the bottom where it asks how this app will be used. Again, you can just say it’s for testing or auto tweeting or whatever. Click on the Create button.
You will get a Developer Terms prompt, just click the Create button again.
Get Your Keys and Tokens
Now that you’ve created your app, go to the Permissions tab in your app and click the Edit button. Change the setting to Read, write, and Direct Messages, then click the Save button.
Next click on the Keys and tokens tab at the top. Copy the API key and the API secret key and paste them into a text file or something, then click on the Generate button to reveal your access tokens. Copy both of these and paste them into the text file with your keys. Note – Once you close the screen with your tokens, they will not be accessible again. If you close this and don’t have them copied somewhere, you will have to regenerate both API keys and both access tokens, so it’s best to save all 4 somewhere safe right away.
Let’s Make a Python Script
Open a blank text document and paste the following. Edit the red sections as needed, then save the file as autotweet.py
#!/usr/bin/env python import tweepy API_KEY = 'Put_Your_API_Key_Here' API_SECRET = 'Put_Your_API_Secret_Key_Here' ACCESS_TOKEN = 'Put_Your_Access_Token_Here' ACCESS_TOKEN_SECRET = 'Put_Your_Access_Token_Secret_Here' auth = tweepy.OAuthHandler(API_KEY, API_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = tweepy.API(auth) status = 'This tweet was posted from a python script.' api.update_status(status=status)
Now you need to make your python file executable. You can either right-click the file, click the permissions tab and check the box to allow executing the file as a program. Or you can open a terminal, navigate to the directory that contains the file and run the following command.
And at last, you can run your script with this.
Refresh your Twitter feed and you should see your first auto tweet!
This is where I ran into a small problem. I tried to run the script a few times and quickly realized that it only worked the first time. This is because it was throwing an error. Apparently it can’t post a duplicate tweet. In order to get around this, I added a timestamp to the tweet so I could re-post the same tweet as I was editing the file and testing stuff.
#!/usr/bin/env python import tweepy from datetime import datetime dateTimeObj = datetime.now() timeObj = dateTimeObj.time() timeStr = timeObj.strftime('%I:%M %p') API_KEY = 'Put_Your_API_Key_Here' API_SECRET = 'Put_Your_API_Secret_Key_Here' ACCESS_TOKEN = 'Put_Your_Access_Token_Here' ACCESS_TOKEN_SECRET = 'Put_Your_Access_Token_Secret_Here' auth = tweepy.OAuthHandler(API_KEY, API_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = tweepy.API(auth) status = 'This tweet was posted from a python script.' + str(timeStr) + '.' api.update_status(status=status)
So that’s the basics of posting the tweet from the script. We’ll come back to this to make a few changes in a bit.
Let’s Make a Bash Script
This bash script will run the speed test. I am supposed to have a download speed of 100 Mbps, so I’m setting this script to run if it slows down to 50 Mbps or less. You can adjust yours to whatever you want to meet your needs for your plan. Keep in mind that if you are on wireless, you will have slower speeds than you would if you were connected to ethernet. If the download speed is less than or equal to 50 Mbps, then it will export the speed variable into the python script and run the python script.
Open a blank text document and paste the following. Edit the path in red so that it points to your python script, then save the file as speedtest.sh and be sure to make the file executable just like you did with the python script. Note – Do not run this yet. We need to update the python script to import the speed variable from this script.
#!/bin/bash dlspeed=$(speedtest | grep -E "Download" | cut -b 17-19) if [ $dlspeed -le 50 ] then export dlspeed /path/to/autotweet.py "$dlspeed" fi
Modify the Python Script to Import the Speed Variable from the Bash Script
Edit the red section to tweet at your internet service provider’s Twitter account.
#!/usr/bin/env python import tweepy from datetime import datetime import os dateTimeObj = datetime.now() timeObj = dateTimeObj.time() timeStr = timeObj.strftime('%I:%M %p') API_KEY = 'Put_Your_API_Key_Here' API_SECRET = 'Put_Your_API_Secret_Key_Here' ACCESS_TOKEN = 'Put_Your_Access_Token_Here' ACCESS_TOKEN_SECRET = 'Put_Your_Access_Token_Secret_Here' auth = tweepy.OAuthHandler(API_KEY, API_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = tweepy.API(auth) status = '@YourISP My network speed is' + os.environ["dlspeed"] + ' Mbps at ' + str(timeStr) + '. That is far less speed than advertised for my plan!' api.update_status(status=status)
Set the Speed Test to Run Every Hour
Almost finished. The final step is to set your bash script up as a cron job that runs every hour, on the hour.
Run the following command.
When the file opens up, scroll to the bottom and add this line. Edit the red section so that the path points to your bash script.
0 * * * * /path/to/speedtest.sh
Press CTRL + o and then Enter to overwrite and save the crontab file. Then press CTRL + x to exit and install the crontab.
And at last, you’re finished. Now every hour, on the hour, your bash script will run the speed test. If the speed is less than or equal to 50 Mbps, it will run the python script and tweet at your internet service provider to let them know you are getting less than you’re paying for. Use this wisely. 😉