Updating your Nanowrimo Wordcount with Python

Posted on Wed 16 November 2016 in Tutorials • 2 min read

In the last few days, I've definitely slowed down the word count. Fatigue, general busyness and the US election fallout means that I've struggled to be creative. In moments like this I need to do something different. For me that means doing some exercise or scratching an intellectual itch.

So, I decided to do something I meant to do last year and write a Python program that can update my Nanowrimo Word Count without need to visit the website.

Thankfully, Nanowrimo has a restful API, that easily allows you to do this. The basic procedure is:

  1. Get your Nanowrimo user name, secret key and new word count
  2. Combine them together and hash them using the sha1 algorithm
  3. Assemble this hashed data along with your plain text user name and word count and then push them the Nanowrimo endpoint using a PUT request.

Command Line interface

I created a very simple Python script on my MacBook Air. I do most of my writing on a Mac, so naturally this is where it will get the most use. My first effort was more a proof of concept rather than a reflection of best practice. It's the first time I've used Python's hashlib and requests modules and they are the two main ingredients of the recipe.

Here's the code

#!/usr/bin/python

import sys, requests, hashlib, ast

secret_key = "YOUR_SECRET_KEY"
user_name = "YOUR_NANOWRIMO_USERNAME"

wc = sys.argv[1]

hash = hashlib.sha1(secret_key + user_name + wc)

payload = {
    'hash': hash.hexdigest(),
    'name': 'YOUR_NANOWRIMO_USERNAME',
    'wordcount' : wc
}

r = requests.put("http://nanowrimo.org/api/wordcount", data=payload)

res = r.text
res = res.replace("=>", ":")
d = ast.literal_eval(res)

print("%s wordcount updated to %s" % ( d["novel"],d["wordcount"] ))

Naturally you need to change the SECRET_KEY and YOUR_NANOWRIMO_USERNAME for your own Nanowrimo account's secret key and user name, which you can get by going here http://nanowrimo.org/api/wordcount. Note that your key will only appear on this page when you are logged in.

To run the program, I've created a symlink to the Python file in my ~/bin folder, which I've added to my $PATH variable. That means I can invoke the command from anywhere and since Spotlight gives me the ability to open the Terminal.app I can fire off the command in seconds without my fingers leaving the keyboard.

Run the program on the command line using your new word count as an argument. I don't bother validating my input because I trust myself not to enter something dumb!

Once entered, the script will hash my details using hashlib and push them to Nanowrimo's server using request.

Again, I don't bother validating the response. All I do is parse it, pick out the bits I want and put them in a message.

NanoWC.py

As I said, this was a proof of concept, not best practice. Only I'm using this code and I don't recommend that you copy it. To make it 'production' ready and suitable for distribution, I'd definitely add input validation and put some test conditions around the request and response to handle network issues.

So there you have it, a really simple script to update your Nanowrimo word count from the command line.

Share on: Diaspora*TwitterFacebookGoogle+Email