Now we want to reuse the code we saw in the Twitter bots workshop to create new conference talk titles in our web application. We could just paste that code into our /talk
route function. The problem with that is that our app.py file begin to get long…
So we’ve included the file named talk_mashup_bot.py
in the talk_mashup_bot/
folder and modified it slightly to look like the following:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Twitter Bot Starter Kit: Bot 3
# ALA Talk generator
# This bot mashes up ALA talk titles to make new ones.
# First, it takes a list of talks from ALA Annual Meetings 2016-2021,
# and splits those titles half: beginnings and endings.
# Then, it chooses a random beginning and a random ending, smushes
# them together into a new talk title, and tweets it.
#Housekeeping
import random
import os
# we're now making up our own function, a reusable piece of code
def splitTitles(myfile):
"""
This function takes text file and splits each line in half.
It returns two lists, first halves and second halves.
"""
#open a text file
talk_title_file = open(myfile,'r',encoding='UTF-8')
talk_titles = talk_title_file.readlines()
talk_title_file.close()
#create empty lists
beginners_list = []
enders_list = []
for line in talk_titles:
line = line.split() #turn the string into a list of words ['like','this']
midpoint = len(line) / 2 #find a rough halfway point in line
midpoint = int(midpoint) #turn that point into an integer instead of a decimal
#stitch together the split-up words, one for the first half and one for the second
beginner = " ".join(line[:midpoint]) #the join syntax sucks, no one can ever remember it
ender = " ".join(line[midpoint:])
#add the talk halves to the two lists
beginners_list.append(beginner)
enders_list.append(ender)
return beginners_list, enders_list #return = what the function spits out to use
#this is the end of the splitTitles() function
def generateTitle():
# this is the way to get the current directory in Python
current_dir = os.path.dirname(os.path.realpath(__file__))
# store the full location of the "ala_all-talk-titles.txt" file in the variable "all_talk_titles_file"
all_talk_titles_file = os.path.join(current_dir, 'ala_all-talk-titles.txt')
# run the splitTitles function and separate the beginning and ending halves
# into two new lists, beginners and enders
beginners_and_enders = splitTitles(all_talk_titles_file)
beginners = beginners_and_enders[0]
enders = beginners_and_enders[1]
# find the length of the lists (number of items)
beginners_length = len(beginners)
enders_length = len(enders)
# choose a random line by picking a number between 1 and the
# number of lines in the lists. (yes, it's convoluted)
title_first_part = beginners[random.randrange(1, beginners_length)]
title_last_part = enders[random.randrange(1, enders_length)]
# putting it all together: what's the new talk title?
tweet_text = title_first_part + ' ' + title_last_part
return tweet_text
# this is the end of the generateTitle() function
Note that for sake of the workshop to this point we’ve removed the sections where we actually send the talk title to Twitter.
We’ve included this file in your jumpstart_webdev_workshop repository under talk_mashup_bot/talk_mashup_bot.py
The lines to really look closely at are:
def generateTitle():
We’ve enclosed the code to generate a mashup talk title into a function which allows that code to be reused.
And instead of updating the status via the Twitter API we’re going to use our function to return the text of the tweet (which is just the newly generated title) like this:
return tweet_text
One nice thing about a module like this is that you can now use it in more than one place without duplicating code.
Now type from talk_mashup_bot import talk_mashup_bot
We can now use our module to create a talk title: talk_mashup_bot.generateTitle()
python
and hit enter. You’ll enter an interactive session in python.from talk_mashup_bot import talk_mashup_bot
and hit enter.talk_mashup_bot.generateTitle()
You should see that a talk title was generated.
When you see a title in your terminal please paste it into chat or unmute and read it.