2014-11-01

Updating My NaNo Novel

Today I woke up eager to get cracking on my Novel for National Novel Writing Month. Although the site didn't crash this year, booyah!, there was a rather persnickety issue with deadlocks in the database that was keeping participants from updating their novel word count. Brutal

After a few hours of debugging and some quick/dirty hacks, and then a more robust fix, the servers were chugging along at a fairly good clip and I could finally get down to typing some words.

Hold on a sec! There is no way that I'm going to do a bunch of typing and then copy/paste my words into the NaNoWriMo word count checker every time I do a bunch of writing. To be less dramatic, this morning I was trying to take advantage of the new wordcount Write API when I became aware of the database problems.

Anyway, surprise surprise I am using Geany to write my novel, and geany has a nice feature where one can define a project and set build commands for that project. Now I know what you are thinking, a static text file doesn't need a build command, and while this is true, I have decided to use the build command to run a script that will count the words in my novel and upload the count to the NaNoWriMo server.

My workflow goes like this:

  1. Type type type my novel
  2. press F8 to run the 'build script'
  3. always skip step 3
  4. rejoice at the simplicity

The script is a quick bit of Ruby with some wrappers around some shell utilities.

Enter The Ruby

#!/usr/bin/env ruby
require 'digest/sha1'
require 'net/http'

#--- config ---#
name 'MY_NANO_NAME'
secret_key 'MY_SECRET_KEY'
novel_file 'MY_NOVEL.txt'
count_file 'count.txt'

#get the word count from the novel
wc `wc -w #{novel_file}`.to_i

#get the last recorded count
lrc `cat #{count_file}`.to_i

#is the word count greater than the last recorded count?
if wc lrc
  #update the lrc
  `echo #{wc#{count_file}`
  #create the hashable string
  hs "#{secret_key}#{name}#{wc}"
  puts hs
  #hash the string
  hashed Digest::SHA1.hexdigesths )
  puts hashed
  #prepare the data
  data = {namenamehashhashedwordcountwc}
  #create and send the request
  http Net::HTTP.new('nanowrimo.org')
  request Net::HTTP::Put.new('/api/wordcount')
  request.set_form_datadata )
  response http.request(request)
  puts response.body

end

For easier copy/paste, the code is available at http://hoof.jezra.net/snip/oq.

Now quit reading, and go write, or read.

Comments
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
7 minus 1
Answer:
required
subscribe
 
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008