My Wordnik Word of the Day Widget

Blog plan, 2010
Blog plan, 2010

I was first introduced to Wordnik when I watched Erin McKean’s TED Talk, “The joy of lexicography.” (Scroll down to watch the video.) I immediately loved the concept of Wordnik, but they did not have an API with which I could create any useful implementations. Since at least 2010 I have wanted a Wordnik Word of the Day on this blog’s sidebar. Back then, it was possible to grab Wordnik’s word of the day, but not to generate one for a specific list. The Wordnik API has improved since.

The primary objective of building a Wordnik Word of the Day widget in my blog’s sidebar is selfish: I wanted to use the Wordnik API and to create both a WordPress plugin and widget, none of which I’d done previously. Web development is fun, especially when you can tap into the engines of other services. Instead of creating functionality on my blog to store words and their definitions, I could simply add words to my Wordnik list and have the API do all the work with minimal effort on my part. The other reason is that I love words and want to share my favorites with you. Every time you visit this blog you will serendipitously see a word that may not be in your personal lexicon, along with its definition.

Wordnik: Word of the Day Widget
Wordnik: Word of the Day Widget

The plugin and widget

Originally I had planned to simply use PHP to do everything, but I’ve since learned Python and like it very much. Any excuse to do something in Python is a good excuse, and luckily Wordnik has a nice Python client library for the Wordnik API.

When the plugin loads it looks into the WordPress database for a word record for that day. Finding one, it writes the word and definition to the sidebar widget and ends. Otherwise, PHP executes my Python script. The script goes out to Wordnik and fetches all the words from my “brentdanley-com” list. Then the script shuffles the word list, grabs the first one, and returns to Wordnik to get the selected word’s definition. The word and definition are then displayed in the sidebar widget and persisted to the database. At most, Wordnik is queried a single time per day, so performance is excellent; Most of the requests are served from the local database.

The WordPress plugin creates or updates the custom database table when the plugin is enabled and enqueues a custom stylesheet. It’s pretty slick. I’ve included the Python script here for your review. Let me know in the comments if there are improvements I should make. :)

#!/usr/bin/env python2.7
 
from wordnik import *
import random
import json
 
apiUrl = 'http://api.wordnik.com/v4'
apiKey = 'WORDNIK API KEY'
client = swagger.ApiClient(apiKey, apiUrl)
username = 'WORDNIK USERNAME'
password = 'WORDNIK PASSWORD'
list_permalink = 'WORDNIK LIST PERMALINK'
 
accountApi = AccountApi.AccountApi(client)
wordApi = WordApi.WordApi(client)
wordListApi = WordListApi.WordListApi(client)
 
authToken = accountApi.authenticate(username, password).token
 
mylist = wordListApi.getWordListWords(list_permalink, authToken)
random.shuffle(mylist)
 
nikword = mylist[0].word
nikdefs = wordApi.getDefinitions(nikword, limit=1)
nikdef = nikdefs[0].text
 
print json.dumps({"word":nikword,"definition":nikdef})

Leave a Reply