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:
- Type type type my novel
- press F8 to run the 'build script'
- always skip step 3
- rejoice at the simplicity
The script is a quick bit of Ruby with some wrappers around some shell utilities.
Enter The 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.hexdigest( hs )
puts hashed
#prepare the data
data = {name: name, hash: hashed, wordcount: wc}
#create and send the request
http = Net::HTTP.new('nanowrimo.org')
request = Net::HTTP::Put.new('/api/wordcount')
request.set_form_data( data )
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.