Skip to content

Instantly share code, notes, and snippets.

@passcod
Created November 24, 2012 03:57
Show Gist options
  • Select an option

  • Save passcod/4138280 to your computer and use it in GitHub Desktop.

Select an option

Save passcod/4138280 to your computer and use it in GitHub Desktop.
•AZ•BOT• mod for NaNoWriMo
# coding: utf-8
"""
NaNoWriMo.org helper mod
Provides wordcounts (.wc) and word war
(.sprint) helpers.
Depends on:
core mods (azbot >= 1.4.0)
lxml
"""
import _util
import time
import datetime
import urllib2
from lxml import etree
from threading import Thread
def get_counts(user):
print "Grabbing %s’s wchistory" % user
url = "http://nanowrimo.org/wordcount_api/wchistory/%s" % user
rawxml = urllib2.urlopen(url, timeout = 5).read().encode('utf8')
doc = etree.XML(rawxml)
if doc.xpath('error'):
return [-1,"Error: %s" % doc.xpath('error')[0].text]
else:
wordcount = float(doc.xpath('user_wordcount')[0].text)
days = doc.xpath('wordcounts')[0]
today = int(days[-1][0].text)
return [int(wordcount), today]
def nano_wordcount(opts, args):
if len(args) == 0:
_util.give_help(opts, "<nanowrimo username> …")
return
for user in args:
try:
c = get_counts(user)
except:
_util.answer(opts, "AAÅÆRGH!")
return
if c[0] == -1:
_util.answer(opts, c[1])
else:
total = 50000
msg = "Word count for %s: %d (%d%% of %d)" % (opts['user'], c[0], c[0]*100/total, total)
_util.answer(opts, msg)
sprint = None
class nanotimer(Thread):
def set(self, time = 15, func = lambda: 0, args = []):
self.time = time * 60
self.func = func
self.args = args
def run(self):
time.sleep(self.time)
self.func(*self.args)
def calla(opts):
global sprint
sprint = None
_util.answer(opts, "Sprint over!")
def callb(opts, duration):
t = nanotimer()
t.set(duration, calla, [opts])
global sprint
sprint = {'start': datetime.datetime.now(), 'duration': duration}
_util.answer(opts, "…set… GO!")
t.start()
def nano_sprint(opts, args=[]):
global sprint
if sprint:
if 'pending' in sprint:
_util.answer(opts, "Pending…")
else:
diff = (sprint['duration'] - (datetime.datetime.now() - sprint['start']).seconds) / 60.0
_util.answer(opts, ("Sprint is running… %1.f minutes left!" % diff).replace("-","~"))
return
if len(args) != 1:
_util.give_help(opts, "<minutes>")
return
try:
t = int(args[0])
except ValueError:
return
tim = nanotimer()
tim.set(1, callb, [opts, t])
_util.answer(opts, "%d-minute sprint starts in one minute. On your marks…" % t)
sprint = {'pending': True}
tim.start()
_util.register(nano_sprint, 'command', 'sprint')
_util.register(nano_wordcount, 'command', 'wc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment