Introduction: Instructable Hit Announcer Bot

About: We are two brothers who like to make things.

In this instructables I will show you how to make a simple Python bot that alerts you when your instructable (your tutorial) gets more views.

This Python bot's user interface is a command line app.

When you get an increased quantity in views, it will beep, display an alert visually, and speak the announcement!

You can customize the minimum view increase interval.

I've included two bonus scripts. The first sends you a notification email instead of text-to-speech, and the other script allows you to customize the notification ringtone.

NOTICE: These Python scripts may no longer work someday if instructables.com modifies their API, or if Google changes their captcha regulations. We will try to update this tutorial with a fix if that happens!

Step 1: Watch the Video

The video contains a full step-by-step tutorial.

If you prefer reading instead of watching, the rest of this instructable is just for you.

Step 2: Get the Files

  1. Download Python 3.6.0 from https://www.python.org/downloads/
  2. Download the Python files for this project from www.makersa.ga

Step 3: Install Python and Add to Path

Open up the python installer you just downloaded.

Click install, but make sure to check the checkboxes that say "Add Python to PATH" and "Install Pip"!

Step 4: Install the Required Python Libraries

Open up your computer's command prompt terminal. On my Windows PC, this is done by going to Start, then typing in "CMD", clicking the "CMD" app to open it up.

2. In the command prompt, type in each of the following commands, hitting enter between them:

pip install requests
pip install time
pip install gtts
pip install pyfiglet
pip install colorama
pip install num2words
pip install simpleaudio
pip install smtplib
pip install time

This will install the necessary libraries.

Step 5: If You Want Email Notifications

If you decide to use the alternative script that sends email notifications, you will need to configure your Gmail account to enable "allow less secure apps".

While logged into your gmail at gmail.com, go to https://myaccount.google.com/security

Scroll down to the part that says "Allow less secure apps"

Turn ON "allow less secure apps".

If you don't want to make your main gmail less secure, or if you don't already have gmail, then sign up for a new gmail solely for this purpose.

Step 6: Run the Python Bot!

Open up command prompt the same way you did earlier.

CD into the location of the python script folder you already downloaded and unzipped from the Maker Saga website, and type in cd ____ with the path to the python script replacing the blank line.

For example, on my laptop the command is

cd c:\users\donovan\downloads\viewbot

Hit enter, then type in "py" followed by the name of the Python bot script.

for example:

py viewnotify31617.py

or for the email notification version

py viewnotify_email.py

or for the custom ringtone version

py viewnotify_custom_ringtone.py

then hit enter.

The command line app will now load, and it will prompt you for your info.

Step 7: Configure the Python Bot

The bot will ask you for the stats JSON link from your instructables tutorial page.

It is not the main URL to your tutorial, so you will need to get it by...

  1. Go to your instructable page in Google Chrome,
  2. Right click, and select inspect.
  3. Click the Network tab
  4. In the search box called "filter", type in "Stats"
  5. Reload the page.
  6. Look for the item called "getIbleStats"
  7. Right click the getIbleStats and select Copy->Copy Link Address.
  8. That is the Stats JSON URL for your tutorial. Paste in into the bot command line app when it prompts you for it.
  9. Follow the rest of the prompts from command line bot.
  10. The bot will now scan for views (hits) on a loop and alert you whenever you want!

I found this URL by looking at a cookie called "categoryTimer" that instructables.com loads when you visit their site. It contained a unique ID that was also inside the unofficial JSON API which I found searching the HTTP requests using that ID that occur when any instructable is loaded in Chrome. I might be possible that the JSON URL expires after several hours, and would cause the bot to stop working.

I've ran a bot for more than 10 hours without any hiccups, so I don't think you will encounter hiccups either.

Step 8: How It Works

In case you're curious how the python bot works, I will try my best to explain.

  1. The bot first takes setup info from the user
  2. The bot pulls the view count info from instructables
  3. the bot pauses for 20 seconds to wait for changes
  4. the bot pulls the newer view info from instructables again.
  5. If the newer view count is greater than the older view count by the interval the user set, it plays an alert sound, speaks the announcement, displays the info visually, and/or sends an email alert.
  6. If not, it rescans until it is true.
  7. The bot loops, repeating this process.

The lines that say "import" simple pull in the Python libraries that the script needs in order to work.

This one imports a library for custom fonts:

from pyfiglet import Figlet

This one imports a library for Gmail integration:

import smtplib

This one is for timing delays:

import time

This one if for playing custom .WAV audio ringtones:

import simpleaudio as sa

This one is for custom font highlighting and colors:

import colorama

This one is for pulling the view count of a instructable from the internet

import requests 

This one is for the robot speaking aloud text (like Siri) using Google text-to-speech engine:

import gtts

This one is for to text to speech to say large numbers naturally:

num2words

For example, with num2words, "1999 views" is spoken as "nineteen thousand ninety nine views".

Without num2words, "1999 views" is spoken as "one nine nine nine views".

This line ask for input input and stores it in a variable called "messageappend". Think of variables as nicknames for storing information for accessing later on. The setup portion of the script has a variable for each question prompt.

messageappend = input('What is the title of the instructables tutorial you want to monitor?\n')

The "\n" part just adds a new line for spacing.

There are "While Loops" and "If Loops" too. These parts run over and over again while a condition is true, and stop otherwise. Everything inside a loop is indented by 4 spaces.

This part below takes the URL store in the variable "idlink" and uses requests.get to pull the contents of that link from the internet. Then it identifies the contents as a JSON array. It then pulls the contents of the array key "views" which is the number of views into the variable "viewz", and converts the number inside "viewz" to an integer instead of the data type it was before. "Viewz" is also converted into a string data type called "oldviewsstr"

r = requests.get(idlink)
    jsonstring1 = r.json()
    viewz = jsonstring1['views']
    oldviews = int(viewz)
    oldviewsstr = str(oldviews)

In case you were wondering, a JSON array looks like this:

{"views":7304,"favorites":56,"comments":14}

time.sleep(20) pauses the bot for 20 seconds.

 time.sleep(20)

This part makes a beeping alert noise:

print(chr(7))

This part saves the text to speech file under a new name each time to avoid errors:

x = ['jim', 'john', 'josh']
        tts = 'tts'
        for i in range(0, 3):
            tts = gTTS(text = speek, lang = 'en')
            file1 = str('hello' + str(i) + '.mp3')
            tts.save(file1)

That's the gist of it!

Step 9: That's All!

I hope you enjoyed this instructable!