from xml sax saxutils import unescape import urllib import simplejson

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from xml.sax.saxutils import unescape
import urllib
import simplejson
from processor import Processor
from decorators import simple_named_processor
from entities import decode_entities
REQUEST_URL = u'http://xa4a.org.ua/couchdb/xapy/_external/fti?q=message:%s'\
.encode("utf-8")
REQUEST_URL_DOC = 'http://xa4a.org.ua/couchdb/xapy/'.encode("utf-8")
class LogsProcessor(Processor):
protocol = 'all' #protocol name
lowLevel = 0 # 1 if lowlevel for protocol
help = '!logs query - search for query in google'
@simple_named_processor(['logs','l'], takes_message=True)
def process(self, frm, text, message):
if not text:
return None
try:
query = urllib.urlencode({'q' : text.encode("utf-8")})
url = REQUEST_URL % (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
rows = json['rows']
if rows > 0:
docs = json['docs']
docid = unescape(docs[0]['docid'])
doc = urllib.urlopen(REQUEST_URL_DOC + docid).read()
json = simplejson.loads(doc)
msg = json['message']
nick = json['author']['nick']
timestamp = json['timestamp']
else:
msg = 'nothing'
nick = ''
timestamp = ''
url = results[0]['url']
mess = frm + nick + ': ' + msg + ' ('+ timestamp + ')'
except:
mess = frm + ", sorry, no results :("
return decode_entities(mess)