A spectre is haunting Twitter — the spectre of bots.

As well as doing some online learning, I want to keep myself busy with little practical projects that have some kind of purpose, even if they aren’t useful. My mischievous side liked the idea of making a twitter bot and after a little bit of googling I found that this was something that wasn’t too hard to achieve, even for a novice like myself.

I used this tutorial which is incredibly thorough. The first few steps are fairly obvious, make a twitter account and because the tutorial uses the python language, install that too. The next step is to give developer access to your account, to do this you make a new twitter app. Then you obtain the keys and access tokens for that app and save them in some way because we will need them later. These are what allows your script to log in remotely to the account and post to it’s heart’s content.

After this, we need to download and install a python wrapper for the Twitter API, this allows python to access the Twitter API and execute the script we are about to write. This tutorial recommends using Python Twitter Tools by @sixohsix. After running a few setup programs its almost time to start writing the script. Before that, we need to install the MarkovBot library as this is the basis for the Twitter bot we are building. The libraries in Python are similar to those in C, in that they contain pre-made routines. After the Markov library has been installed the script needs feeding a lot of data so it can create a Markov chain N.B. I don’t really understand what a Markov chain is, something to do with the previous state of something determining the probability of future events, probability mathematics not being one of my strong points.

To get a lot of data into the bot, you tell it to read a book or two, literally. I chose Karl Marx’s and Friedrich Engel’s ‘The Communist Manifesto’. Once you have a plain text version of your book in the folder that contains all the bits for the bot we can finally start writing the script.

The script isn’t too complicated and Python seems to be a fairly easy language to get to grips with and not too dissimilar to C. In fact it seems at first glance to be a bit easier to write with than C, I’m not seeing many semi-colons or curly braces. I wont go into the specifics of the script because it is explained so well in the tutorial. I’m also going to upload the code to my GitHub so I can show the options I chose and the subtle changes I made.

The first problem I faced with the code is that the script would simply terminate straight away and the bot wouldn’t actually tweet anything. After searching through the comments on the tutorial I found that you have to use the sleep command to make the bot run in the background. This can be seen in lines 31 and 32 of my code.

The next problem I faced wasn’t really to do with the code, it was to do with Twitter. Unfortunately I was a little bit over zealous with the bot and because its keyword was so general, it was sending out far too many tweets, Twitter considered it spam and so took away the app’s write permission.


“Behind the scenes at Twitter HQ”

I suppose this is good in a way because it stops people from turning your account into a spambot but in this instance, I WANT TO BE A BOT! To hopefully remedy this I made a simple change to my code. Instead of searching for the target string ‘Communism’, the bot now looks for ‘#KarlMarx’. This hashtag turns up in twitter far less than the word ‘Communism’ so I should have very little risk of the bot spamming the heck out of people.

At the time of writing the new version of the bot has been live for about 20 minutes and so far has found two tweets that contained the new target string. Hopefully twitter won’t kill it off this time. At some point in the future I will probably end up getting a Raspberry Pi or similar and run the bot from that so I can have it running 24/7 and so I can actually turn off my laptop.


To ‘C’ or not to ‘C’?

This post is going to be all about the week 1/pset1 of the CS50 course.

Week 1 follows on from week 0 by building on the principles taught with Scratch, however it now moves these principles into the text based programming language ‘C’.

The lecture goes over basic commands and syntax as well as implementing loops. The tasks posed for this week are:

  • Say hello to the world with hello.c.
  • Calculate your water consumption with water.c.
  • Recreate one of Mario’s pyramids, in mario.c.
  • Provide a user with either cash or credit in greedy.c or credit.c.


Hello.c is a simple program that prints the phrase “Hello, world!”. The code is given in the instructions and this task serves to get the user used to using the CS50 IDE interface and the syntax highlighting. It also introduces the user to the clang compiler.


Water.c asks the user to create a program that will work out the equivalent bottles of water used when showering based on how long the shower takes. It introduces simple maths into a program and works along the lines of n * 12 to determine the number of bottles.

The program prompts the user for an input and then uses a do while loop to do the calculation only when the user inputs a valid integer.


This task instructs the user to create a program that will take an integer from the user and then print a right angled pyramid on the screen that is as many lines tall as the user specifies. The pyramid is made from ‘#’ and ‘ ‘, and the top level has two ‘#’.

Once the user has entered a valid value (between 0 and 23) the main bulk of this code is contained within 3 for loops. These loops simply take it in turns to print the spaces and then print the #’s. The program uses simple formulas to determine the number of characters to print based on initial height entered. The code for this project can be seen here.


The final task in this week’s problem set requires the user to figure out the smallest amount of coins that can be given to make up a customers change, the coins you have to choose from are $0.25, $0.10, $0.05 and $0.01.

This challenge introduces someone of the problems that are inherent in the way that a computer counts and displays numbers, namely floating point imprecision. Because a number is represented by a series of 1’s and 0’s there is a finite amount of combinations of those that are used to represent an infinite amount of numbers. The example given in the lecture is that the the sum 1 / 10 displayed to a large number of decimal places should just be represented as 0.1000000000… but it is actually shown as 0.100000000000000000555111512312578… .

Because we are working in cents it is a good idea to multiply by 100 to move from decimal values to integer values we can then use the round command to round the number to the nearest integer. Once this calculation has been completed we can use a series of if loops inside a while loop to determine whether the change can be divided into each of the respective coins from biggest to smallest, all of this is going on inside a while loop which ensures that script runs whilst a valid integer is provided.

For example, if the value of $0.44 is given then the script:

  1. Multiplies by 100 = 44
  2. Can 44 be divided by 25? Yes, once
  3. Can the remainder be divided by 10? Yes, once
  4. Can the remainder be divided by 5? Yes, once
  5. Can the remainder be divided by 1? Yes, four times
  6. Print the amount of coins = 7

That explanation is just a rough approximation, in reality the script asks if the number can be divided by 25, if so it subtracts 25 from the number and adds one to the coin count, this then repeats, if it cannot be divided by 25 it moves on to the next denomination and so on.

The code for this script can be seen here.

This week was a lot of fun and it felt really good to start getting to grips with a programming language, I’ve heard good and bad things about ‘Learn C the Hard Way’ in so much as it’s a bit of a masochistic thing to undertake but it’s incredibly thorough. I think I will stick to the basics for now and see what happens in the future…

Programming with Scratch

The first week of CS50x is an introduction to the course and an introduction to thinking in the same way as a computer. As an introduction to programming and the logical steps that a computer takes I was introduced to an online programming environment called Scratch. Developed by MIT, it allows for users with little or no prior programming knowledge to construct simple scripts and programs using a visual interface.

Scratch works by offering the user a series of ‘puzzle pieces’ to create functions, variables and loops. Some of these pieces are shown below

Screen Shot 2017-04-23 at 21.37.01

Problem Set 0

The task set out in problem set 0 was simply to play around with scratch and get a feel for how the logic of programming works and then submit a program, the specifications for the program were:

  • Your project must have at least two sprites, at least one of which must resemble something other than a cat.
  • Your project must have at least three scripts total (i.e., not necessarily three per sprite).
  • Your project must use at least one condition, one loop, and one variable.
  • Your project must use at least one sound.

For this task I decided to create a mini game in which a character has to move across three screens to collect beach balls in order to win. This is a much simpler concept than I had originally wanted, I had originally wanted to add enemies into the game, but the scripts for these were proving a little too complicated at the time for them to be included in a time effective manor.

The main difficulty was working around the script which changes the backgrounds. This script detects when the main character sprite was at the edge of the screen, switches the background to the next one in the sequence and then moves the character to the other side of the screen to give a scrolling background effect. Actually getting the program to do this wasn’t too difficult but getting the beachballs that were placed in the game to reappear when returning to the background only if they hadn’t been collected proved to be something I couldn’t figure out.

The final version of my project is below: