usr bin python encoding utf-8 import weechat import re import psycopg2

  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import weechat
import re
import psycopg2, psycopg2.extensions
import traceback
from random import random
from time import time
from itertools import groupby
from locale import getlocale
local_charset = getlocale()[1]
bot_nickname = 'FYHT'
server_name = 'omskirc'
channel_name = '#buktopuha'
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
connection = psycopg2.connect(database = "buktopuha")
connection.set_client_encoding('UNICODE')
def cleanup():
global connection
connection.close()
connection = None
return weechat.PLUGIN_RC_OK
def urlGrabParsemsg(server, command):
# :nick!ident@host PRIVMSG dest :foobarbaz
l = command.split(' ')
mask = l[0][1:]
nick = mask.split("!")[0]
dest = l[2]
message = ' '.join(l[3:])[1:]
###########################################
#nothing, info, message = command.split(":", 2)
#info = info.split(' ')
if dest == weechat.get_info('nick', server):
channel = nick
else:
channel = dest
return (nick, channel, message)
# ERROR: value too long for type character(40)
# CONTEXT: COPY words, line 667658, column word: "японскаянациональнаяодеждасвободногопокроя"
send_queue = []
def flush_queue():
global send_queue
local_send, send_queue = send_queue, []
for func, msg in local_send:
if func == 'command':
weechat.command(msg.encode(local_charset), channel_name, server_name)
elif func == 'prnt':
weechat.prnt(msg.encode(local_charset), channel_name, server_name)
else:
raise NotImplementedError, 'what is %s?' % func
# BUGZZZ: weechat.remove_timer_handler("flush_queue")
return weechat.PLUGIN_RC_OK
def enqueue(command):
global send_queue
send_queue.append(('command', command))
# BUGZZZ: weechat.add_timer_handler(1, "flush_queue")
def echo(line):
global send_queue
send_queue.append(('prnt', line))
# BUGZZZ: weechat.add_timer_handler(1, "flush_queue")
def on_msg(server, args):
global send_queue
nick, chan, message = urlGrabParsemsg(server, args)
message = unicode(message, local_charset)
if chan == channel_name and server == server_name and nick == bot_nickname:
# пофиксить Экстра Вопрос
match = re.match(u'(?:\x0302)?(?:Bоnpoс #\d+|Экстра Bоnpoс):(?:\x0312)? (.*) (?:\x0307)? \((\d+) букв(?:\x0307)?\)(?:\x03)?', message)
if match and random() > 0.75:
echo(message)
question = match.group(1)
no_of_alphas = int(match.group(2))
words = set([word.lower() for word in re.findall(u'\w+', question, re.UNICODE)])
# echo(u"Quest: [%s]" % ', '.join(words))
# echo(u"Alpha: %i" % no_of_alphas)
try:
ph = ', '.join(('%s', ) * len(words))
cursor = connection.cursor()
start = time()
cursor.execute(
"""SELECT COUNT(definer_id) AS matches, word FROM defs JOIN words ON (term_id = word_id)
WHERE definer_id IN(SELECT word_id FROM words WHERE word IN (""" + ph + """)) AND LENGTH(word) = %s
GROUP BY term_id,word HAVING COUNT(definer_id) > 1 ORDER BY matches DESC LIMIT 10""",
tuple(words) + (no_of_alphas, ))
variants = cursor.fetchall()
end = time()
echo(u"Распарсил за %f c..." % (end - start))
if len(variants):
for matches, list in groupby(variants, lambda row: row[0]):
list = [row[1].strip() for row in list]
list.sort()
echo(u"%i совпадений: %s" % (matches, u' '.join(list)))
elif not variants:
echo(u"Не осилил")
if len(variants) == 1 or len(variants) > 0 and variants[0][0] > variants[1][0]:
enqueue(variants[0][1])
except Exception, e:
traceback.print_exception(None, e, None)
return weechat.PLUGIN_RC_OK
if weechat.register("buktopuha", "0.0", "cleanup", "buktopuha autosolver"):
weechat.add_message_handler("privmsg", "on_msg")
weechat.add_timer_handler(1, "flush_queue")
# vim:set tabstop=4 softtabstop=4 shiftwidth=4:
# vim:set foldmethod=marker foldlevel=32 foldmarker={{{,}}}:
# vim:set expandtab: