Skip to content

Instantly share code, notes, and snippets.

@HBBisenieks
Last active November 5, 2015 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HBBisenieks/702f8f5d19b780503b0a to your computer and use it in GitHub Desktop.
Save HBBisenieks/702f8f5d19b780503b0a to your computer and use it in GitHub Desktop.
Simple script suitable for firing from a cron job for updating NaNoWriMo wordcount via their API
#!/bin/bash
# If you're writing your NaNo work in chunks, this compiles your chunks first
cat /path/to/NaNoWriMo/chunks/chunk_* > /path/to/NaNoWriMo/compiled.txt
# Set wordcount variable
wc=`wc -w /path/to/NaNoWriMo/compiled.txt | tail -n 1 | awk '{print $1}'`
# Get current wordcount from NaNoWriMo
nanowc=`curl http://nanowrimo.org/wordcount_api/wc/hbbisenieks | grep -oPm1 "(?<=<user_wordcount>)[^<]+"`
# Test if new wordcount is equal to posted wordcount
# if your wordcount and NaNo's are the same, there's no point calling an update
if [ "$wc" -ne "$nanowc" ] ; then
# API key can be obtained by visiting http://nanowrimo.org/wordcount_api
key='[API key][username]'
# Make pre-hashed string
prehash=$key$wc
# Actually hash the string
sha=`echo -n $prehash | sha1sum | awk '{print $1}'`
# Post results
curl -X PUT -d hash=$sha -d name=[username] -d wordcount=$wc http://nanowrimo.org/api/wordcount
else
echo "No change to wordcount found"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment