Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenthall committed Jan 30, 2014
1 parent f4070e1 commit ebbec8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
39 changes: 35 additions & 4 deletions chantbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@
import re
import math
import itertools
import logging
import os

config= ConfigParser.ConfigParser()
config.read('config.cfg')

# Setup Twitter client

oauth = OAuth(config.get('OAuth','accesstoken'),
config.get('OAuth','accesstokenkey'),
config.get('OAuth','consumerkey'),
config.get('OAuth','consumersecret'))

t = Twitter(auth=oauth)

# Setup Logging

logpath = config.get('Logging','logpath')

if not os.path.exists(logpath):
os.makedirs(logpath)

##cruft?
logger = logging.getLogger('log')
#todo: use date/time as the log file name
hdlr = logging.FileHandler(os.path.join(logpath,
"%s.log" % datetime.now().isoformat()))
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

# Setup chants

source = config.get('Content','source')

keywords = config.get('Content','keywords').split(',')
Expand Down Expand Up @@ -83,6 +106,8 @@ def prepare_chants(source):

chants = prepare_chants(source)

# Begin chanting

# which chant to start with
chantburn = int(config.get('Schedule','chantburn'))

Expand Down Expand Up @@ -111,26 +136,32 @@ def do_chant(chant):
interval = duration / (len(chant.bursts) - 1)

rest = interval - len(chant.lines) * beat
print "interval: %d" % interval
print "rest: %d" % rest

logger.debug("Interval: %d. Rest: %d." % (interval,rest))

for burst in chant.bursts:
for line in burst:
t.statuses.update(status=line)
logger.debug(line)
time.sleep(beat)

logger.debug("(rest)")
time.sleep(rest)


chants = itertools.cycle(chants)

# burn in to appropriate starting chant
for i in range(chantburn):
logger.debug("Buring in for %d." % chantburn)
chants.next()

index = chantburn

for chant in chants:
logger.debug("Computing start time")
start = compute_start()
wait = start - datetime.now()
print "Waiting to start for %s" % wait
logger.debug("Waiting for %s for next chant." % wait)
time.sleep(wait.total_seconds())
logger.debug("Beginning chant index %d" % index)
do_chant(chant)
7 changes: 5 additions & 2 deletions config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ consumersecret:xxxx

[Content]
source=corpus/processed/1000skies.txt
keywords=beard,eye,doors,feast,skinny,glittering,child,Mariner,ship,lighthouse
keywords=prayer,guidance,gratitude,happiness,innerpeace,devotion,grace,freedom,blessing,compassion,joy

[Logging]
logpath=log

[Schedule]
starttime:21:57
beat:1
bursts:5
duration:180
chantburn:0
chantburn:0

0 comments on commit ebbec8e

Please sign in to comment.