diff --git a/modules/active/get_quote.py b/modules/active/get_quote.py index 12d6e74..d433992 100644 --- a/modules/active/get_quote.py +++ b/modules/active/get_quote.py @@ -62,13 +62,28 @@ class WikiQuoteParser(HTMLParser): if self.in_li==1 and self.in_ul==1 and self.h2_num>=1: self.quote+=data.replace("\n","") +def search_wiki(code,term): + results = [] + url = "http://"+code[0:2]+".wikiquote.org/w/api.php?action=opensearch&search="+urllib2.quote(term) + req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"}) + con = urllib2.urlopen( req ) + data=con.read() + data=data[data[1:].index("[")+2:data.rindex("]")-1] + for dat in data.split(","): + results.append( dat[1:-1].decode('string_escape') ) + return results def quote(msg,args): if len(args)!=3: brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" language (two letter code) topic",'groupchat') else: try: - url = "http://"+args[1][0:2]+".wikiquote.org/wiki/"+args[2] + search_results = search_wiki(args[1],args[2]) + if len( search_results )==0: + brathuhn.sendMsg(brathuhn.ROOM,"No quotes found",'groupchat') + + + url = "http://"+args[1][0:2]+".wikiquote.org/wiki/" + urllib2.quote(search_results[0]) req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"}) con = urllib2.urlopen( req ) @@ -78,12 +93,12 @@ def quote(msg,args): wqp.feed(data) #for i in wqp.quotes: # print "-- ",i - if len(wqp.quotes)==1: - brathuhn.sendMsg(brathuhn.ROOM,wqp.quotes[0],'groupchat') - elif len(wqp.quotes)>1: + + if len(wqp.quotes)>=1: + brathuhn.sendMsg(brathuhn.ROOM,'Quoting "'+ search_results[0] +'"') brathuhn.sendMsg(brathuhn.ROOM,wqp.quotes[randint(0,len(wqp.quotes)-1)],'groupchat') else: - brathuhn.sendMsg(brathuhn.ROOM,"No quotes found",'groupchat') + brathuhn.sendMsg(brathuhn.ROOM,'Could not crawl quotes from "'+url+'"','groupchat') con.close() except exceptions.Exception as e: brathuhn.sendMsg(brathuhn.ROOM,"Network Error "+unicode(e),'groupchat')