#!/usr/bin/python """ Reads the last line of file most recently updated and sends output to a speech synthesis engine. some up front notes: This script needs an IRC client that can write chat logs to a file. For it to work properly needs to be called with dnotify or inotify in the following form: dnotify -M /absolute/path/to/logs -e /path/to/saytail.py The /absolute/path/to/logs is set by hand at present and assigned to the variable logpath (see below) presently other markers are set to read Konversation logs but with a little bit of modification it should be able to read any log that is written one line at a time, and with a little more modification it should be able to read just about any log a normal user can read. """ import os, re, sys ## Initialization ## # assign default varriable values message = "" # avoid non assignment error # still hand coded logpath = "/home/kurt/logs/" ## functions ## def find_latest_file(logpath): """ Determine the last file modified in log directory, logpath """ latest = 0 # set comarison value to zero for file in os.listdir(logpath): # loop thorough each file # get the time the file was last modified this_time = os.path.getmtime(file) # in seconds since epoch if this_time >= latest: # how does it compare, larger is more recent latest = this_time # if larger make it a new standard logname = file # if larger make its associated file new standard # assign the most recently modified file name in logpath to logname return logname # this was the most recently modified file, so use it def read_file(log): """ Read the file passed as log and return last line """ # open the log for reading logfile = open(log, "r") filetext = logfile.readlines() lastline = filetext[-1] return lastline def check_marker(lastline): """ Test the message for marker contents This looks for known substrings that help determine the parts of the line last added to the log. This is highly dependent on what client has actually written the log. We can perhaps make these as modules for different clients. """ # most are normal messages so we test this first messagemarker = "] <" # in konversation marks end time begin message special_marker = "] *" # in konversation marks /me join_marker = "] Join " # in konversation marks join nick_marker = "] Nick " # in konversation change nick marker = "" # will set to message or special marker if "] <" in lastline: marker = "] <" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] *" in lastline: marker = "] *" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Join" in lastline: marker = "] Join" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Part" in lastline: marker = "] Part" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Quit" in lastline: marker = "] Quit" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Nick" in lastline: marker = "] Nick" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Topic" in lastline: marker = "] Channel" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Mode" in lastline: marker = "] Mode" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Created" in lastline: marker = "] Created" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Notice" in lastline: marker = "] Notice" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] Whois" in lastline: marker = "] Whois" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] <->" in lastline: marker = "] <->" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) elif "] MOTD" in lastline: marker = "] MOTD" mkst = lastline.find(marker) mknd = lastline.find("\t", mkst) else: marker = "" mkst = 0 mknd = len(lastline) return marker, mkst, mknd def parse_message(mkst,mknd,lastline): """ Moldes the parts of message to be spoken aloud """ print "marker = ",marker," lastline = ",lastline #debug mid = lastline[mkst +1:mknd]# gets mid section of lastline mid = mid.strip()#remove whitespace if mid.startswith("<") and mid.endswith(">"): mid = mid[1:-1]# if nick in tag pluck it out body = lastline[mknd:]# the body of the message saytext = mid + " said " + body # assemble parts return saytext # return text to be read aloud def say_message(saytext): """ Send final text to speech engine """ # Start KTTSD (if not already running) os.system('kttsd') # sends kttsd command to shell # say message using dcop # can comment out next line of code to save time in debugging # since it takes quite a bit of time to say the message each time os.system('dcop kttsd KSpeech sayMessage " ' + saytext + ' " "en"') ## start body of code ## print "## here we go..." os.chdir(logpath) # change working directory to logpath logname = find_latest_file(logpath)#finds last file updated log = logpath + logname # log updated # get the last line of most recently changed log print"log = ",log lastline = read_file(log) # get parts of the last line in log marker, mkst, mknd = check_marker(lastline) #print "mkst = ",mkst,type(mkst)," mknd = ",mknd,type(mknd)#debug # mold the statement to be read aloud saytext = parse_message(mkst,mknd,lastline) if len(message) and logbody.find(">"): # don't say blank message saytext = nickname + filler + message # compose actual articulation #print "saytext = ",saytext # debug tool comment out when finished#debug say_message(saytext) print "That's all folks!"