After my first twitterbot I obviously wanted to create a new twitterbot, one a bit more complex, a bit more creative.

The Catalan Elections on the past 27th of September where approaching and thought on doing something about it. There was at the time a big political confrontation between the separatist and the unionist parties, and as usual, the undecided voters would have a huge impact in the final results.

Making a little pun on the situation, my bot tries to provide an (admittedly simple and useless) undecided political discourse by randomly remixing the last tweets of the different candidates and producing new tweets with them.

The list of candidates, with their correspondent twitter handle, whose tweets were considered are (in alphabetical order, although some think this order is the actual outcome of the elections grin):

candidats = [
    'inesarrimadas', # Inés Arrimadas (C's)
    'antoniobanos_', # Antonio Baños (CUP)
    'ramon_espadaler', # Ramon Espadaler (UDC)
    'albiol_xg', # Xavier García Albiol (PP)
    'miqueliceta', # Miquel Iceta (PSC)
    'lluisrabell', # Lluís Rabell (CSQEP)
    'raulromeva', # Raül Romeva (JxS)
    ]

Having the list of twitter handles to fetch tweets for, and again using tweepy, I can easily obtain the last 20 tweets for each of the candidates and split the words so I end up with the list of words composing the 140 tweets:

texts = []
for candidat in candidats:
    print("Fetching tweets for candidate", candidat)
    
    # tweepy function to obtain last 20 tweets of given user
    tweets = api.user_timeline(id=candidat) 
    
    for tweet in tweets:
        texts.append(tweet.text) # the text property contains the actual tweet

words = []
for text in texts:
    for word in text.split():
        words.append(word)

You can see an example output of these two steps in texts.txt and in words.txt.

Once I’ve obtained the list of words, I compose a new tweet randomly appending words from the list and trying to maximise the tweet length, without going over the 140 character limit imposed by Twitter:

tweet = ""
retries = 0
while (len(tweet) < 140):
    selected_word = random.choice(words)
    
    if (len(tweet) + len(selected_word) < 140):
        tweet += " " + selected_word
    else:
        retries += 1
    
    print("Current tweet ", len(tweet), tweet)
    
    if (retries == 10):
        break

The resulting output of this code following the previous example can be found in tweet.txt. And the final result looks like this on Twitter (note that @unaltrebot is my bot for testing purposes):

Not too bad for an hour of coding sweat_smile!

You can check the full profile of “the undecided bot” on Twitter.

Given there have been some noise about the use of bot accounts to promote and amplify political messages before and specially during the campaign, and there is (big smile) room for improvement to my current approach I will try to build a new twitterbot for the Spanish Elections on the next 20th of December, but my bot will not try (at least willingly) to promote or amplify the political position of any contestant party innocent.

Update on 2016-01-22

I’ve published a post of this improved twitterbot for the Spanish Elections. You can follow the bot in twitter too @bot_candidato.