-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot_sql.py
executable file
·233 lines (210 loc) · 7.35 KB
/
bot_sql.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import socket
import sys
import urllib2
import os
import time
from pysqlite2 import dbapi2 as sqlite
channel = '#masmorra'
nick = 'carcereiro'
server = 'irc.oftc.net'
def sendmsg(msg):
sock.send('PRIVMSG '+ channel + ' :' + str(msg) + '\r\n')
class db():
def __init__(self, dbfile):
if not os.path.exists(dbfile):
self.conn = sqlite.connect(dbfile)
self.cursor = self.conn.cursor()
self.create_table()
self.conn = sqlite.connect(dbfile)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def create_table(self):
self.cursor.execute('CREATE TABLE karma(nome VARCHAR(30) PRIMARY KEY, total INTEGER);')
self.cursor.execute('CREATE TABLE url(nome VARCHAR(30) PRIMARY KEY, total INTEGER);')
self.cursor.execute('CREATE TABLE slack(nome VARCHAR(30), total INTEGER, data DATE, PRIMARY KEY (data, nome));')
self.conn.commit()
def insert_karma(self,nome,total):
try:
self.cursor.execute("INSERT INTO karma(nome,total) VALUES ('%s', %d );" % (nome,total))
self.conn.commit()
return True
except:
#print "Unexpected error:", sys.exc_info()[0]
return False
def increment_karma(self,nome):
if not self.insert_karma(nome,1):
self.cursor.execute("UPDATE karma SET total = total + 1 where nome = '%s';" % (nome))
self.conn.commit()
def decrement_karma(self,nome):
if not self.insert_karma(nome,-1):
self.cursor.execute("UPDATE karma SET total = total - 1 where nome = '%s';" % (nome))
self.conn.commit()
def insert_url(self,nome,total):
try:
self.cursor.execute("INSERT INTO url(nome,total) VALUES ('%s', %d );" % (nome,total))
self.conn.commit()
return True
except:
return False
def increment_url(self,nome):
if not self.insert_url(nome,1):
self.cursor.execute("UPDATE url SET total = total + 1 where nome = '%s';" % (nome))
self.conn.commit()
def insert_slack(self,nome,total):
try:
self.cursor.execute("INSERT INTO slack(nome,total,data) VALUES ('%s', %d, '%s' );" % (nome,total,time.strftime("%Y-%m-%d", time.localtime())))
self.conn.commit()
return True
except:
return False
def increment_slack(self,nome,total):
if not self.insert_slack(nome,total):
self.cursor.execute("UPDATE slack SET total = total + %d where nome = '%s' and data = '%s' ;" % (total,nome,time.strftime("%Y-%m-%d", time.localtime())))
self.conn.commit()
def get_karmas_count(self):
self.cursor.execute('SELECT nome,total FROM karma order by total desc')
karmas = ''
for linha in self.cursor:
if len(karmas) == 0:
karmas = (linha[0]) + ' = ' + str(linha[1])
else:
karmas = karmas + ', ' + (linha[0]) + ' = ' + str(linha[1])
return karmas
def get_karmas(self):
self.cursor.execute('SELECT nome FROM karma order by total desc')
karmas = ''
for linha in self.cursor:
if len(karmas) == 0:
karmas = (linha[0])
else:
karmas = karmas + ', ' + (linha[0])
return karmas
def get_karma(self, nome):
self.cursor.execute("SELECT total FROM karma where nome = '%s'" % (nome))
for linha in self.cursor:
return (linha[0])
def get_urls_count(self):
self.cursor.execute('SELECT nome,total FROM url order by total desc')
urls = ''
for linha in self.cursor:
if len(urls) == 0:
urls = (linha[0]) + ' = ' + str(linha[1])
else:
urls = urls + ', ' + (linha[0]) + ' = ' + str(linha[1])
return urls
def get_slacker_count(self):
self.cursor.execute("SELECT nome,total FROM slack where data = '%s' order by total desc" % (time.strftime("%Y-%m-%d", time.localtime())))
slackers = ''
for linha in self.cursor:
if len(slackers) == 0:
slackers = (linha[0]) + ' = ' + str(linha[1])
else:
slackers = slackers + ', ' + (linha[0]) + ' = ' + str(linha[1])
return slackers
class html:
def __init__(self, url):
self.url = url
self.feed = None
self.headers = {
'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10)',
'Accept-Language' : 'pt-br,en-us,en',
'Accept-Charset' : 'utf-8,ISO-8859-1'
}
def title(self):
self.feed = self.get_data()
title_pattern = re.compile(r"<[Tt][Ii][Tt][Ll][Ee]>(.*)</[Tt][Ii][Tt][Ll][Ee]>", re.UNICODE)
title_search = title_pattern.search(self.feed)
if title_search is not None:
try:
return "[ "+re.sub("&#?\w+;", "", title_search.group(1) )+" ]"
except:
print "Unexpected error:", sys.exc_info()[0]
return "[ Fail in parse ]"
def get_data(self):
try:
reqObj = urllib2.Request(self.url, None, self.headers)
urlObj = urllib2.urlopen(reqObj)
return urlObj.read(4096).strip().replace("\n","").replace("\r", "")
except:
print "Unexpected error:", sys.exc_info()
return "<title>Fail in get</title>"
banco = db('carcereiro.db')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, 6667))
sock.send('NICK %s \r\n' % nick)
sock.send('USER %s \'\' \'\' :%s\r\n' % (nick, 'python'))
sock.send('JOIN %s \r\n' % channel)
while True:
buffer = sock.recv(2040)
if not buffer:
break
print buffer
if buffer.find('PING') != -1:
sock.send('PONG ' + buffer.split() [1] + '\r\n')
if re.search(':[!@]help', buffer, re.UNICODE) is not None or re.search(':'+nick+'[ ,:]+help', buffer, re.UNICODE) is not None:
sendmsg('@karmas, @urls, @slackers\r\n')
regexp = re.compile('PRIVMSG.*[: ]([a-z][0-9a-z_\-\.]+)\+\+', re.UNICODE)
regexm = re.compile('PRIVMSG.*[: ]([a-z][0-9a-z_\-\.]+)\-\-', re.UNICODE)
regexk = re.compile('PRIVMSG.*:karma ([a-z_\-\.]+)', re.UNICODE)
regexu = re.compile('PRIVMSG.*[: ]\@urls', re.UNICODE)
regexs = re.compile('PRIVMSG.*[: ]\@slackers', re.UNICODE)
regexks = re.compile('PRIVMSG.*[: ]\@karmas', re.UNICODE)
regexslack = re.compile(':([a-zA-Z0-9\_]+)!.* PRIVMSG.* :(.*)$', re.UNICODE)
pattern_url = re.compile(':([a-zA-Z0-9\_]+)!.* PRIVMSG .*(http://[áéíóúÁÉÍÓÚÀàa-zA-Z0-9_?=./,\-\+\'~]+)', re.UNICODE)
resultp = regexp.search(buffer)
resultm = regexm.search(buffer)
resultk = regexk.search(buffer)
resultu = regexu.search(buffer)
results = regexs.search(buffer)
resultks = regexks.search(buffer)
resultslack = regexslack.search(buffer)
url_search = pattern_url.search(buffer)
if resultslack is not None:
var = len(resultslack.group(2)) - 1
nick = resultslack.group(1)
banco.increment_slack(nick,var)
if resultp is not None:
var = resultp.group(1)
banco.increment_karma(var)
sendmsg(var + ' now has ' + str(banco.get_karma(var)) + ' points of karma')
continue
if resultm is not None:
var = resultm.group(1)
banco.decrement_karma(var)
sendmsg(var + ' now has ' + str(banco.get_karma(var)) + ' points of karma')
continue
if resultk is not None:
var = resultk.group(1)
points = banco.get_karma(var)
if points is not None:
sendmsg(var + ' have ' + str(points) + ' points of karma')
else:
sendmsg(var + ' doesn\'t have any point of karma')
continue
if resultks is not None:
sendmsg('karmas : ' + banco.get_karmas_count())
continue
if results is not None:
sendmsg('slackers in chars : ' + banco.get_slacker_count())
continue
if resultu is not None:
sendmsg('users : ' + banco.get_urls_count())
continue
if url_search is not None:
try:
url = url_search.group(2)
nick = url_search.group(1)
parser = html(url)
sendmsg( parser.title() )
banco.increment_url( nick )
except:
sendmsg('[ Failed ]')
print url
print "Unexpected error:", sys.exc_info()[0]
sock.close()
banco.close()