#!/usr/bin/env python # copyright Alain Tesio alain@onesite.org # GPL license # Tested with Python 2.1.1 # Last updated: Mon Jan 24 21:11:52 CET 2005 import sys,string,httplib,re,os # Change these settings and create the cachedir cachedir="/var/cache/say" playcommand="play %s" regexp1=re.compile("\* Click here to listen") server="www.m-w.com" def error(err): sys.stderr.write("%s\n" % err) sys.exit(1) def out(s): sys.stdout.write(s) def wget(host,path): out("Getting '%s' at host '%s' ... " % (path,host)) h=httplib.HTTP(host) h.putrequest('GET',path) h.endheaders() errcode, errmsg, headers = h.getreply() if errcode!=200: out("Error\n") f = h.getfile() data = f.read() f.close() out("size=%i\n" % len(data)) return data def wget_pattern(host,path,pattern): body=wget(host,path) for line in body.split("\n"): matchobject=pattern.search(line) if matchobject: return matchobject.groups() def check_file_from_word(word): intermdir=word.upper()[:2] if len(intermdir)==1: intermdir+="A" wavdir="%s/%s" % (cachedir,intermdir) if not(os.path.isdir(wavdir)): os.mkdir(wavdir) localpath="%s/%s.wav" % (wavdir,word.upper()) if not(os.path.isfile(localpath)): try: wavurl1=wget_pattern(server,"/cgi-bin/dictionary?book=Dictionary&va=%s" % word,regexp1)[0] except TypeError: error("Word '%s' not found" % word) try: (server2,url2)=wget_pattern(server,wavurl1,regexp2) except TypeError: error("Error looking up the second url") wavdata=wget(server2,url2) if len(wavdata)<20 or wavdata.find("was not found")!=-1: error("Wav file not found") wavfile=open(localpath,"w") wavfile.write(wavdata) wavfile.close() os.chmod(localpath,0644) return localpath if len(sys.argv)==1: error("Usage: %s word" % sys.argv[0]) else: word=sys.argv[1] localpath=check_file_from_word(word) os.system(playcommand % localpath)