-
Notifications
You must be signed in to change notification settings - Fork 0
/
anna-old.py
362 lines (326 loc) · 13.2 KB
/
anna-old.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# coding: utf-8
from time import sleep
from livechanapi import *
import wikipedia
import duckduckgo
import re
import os
import sys
import json
import requests
import random
import wolframalpha
import urbandict
from cleverbot import Cleverbot
import random
from datetime import datetime, timedelta
import config
import sqlite3
import shutil
import pickle
from game import Game
cb = Cleverbot()
game = Game()
def kc_quote(needle=None):
conn = sqlite3.connect('KC.db')
if needle:
cur = conn.execute('SELECT text FROM kcposts WHERE text LIKE ("%" || ? || "%") ORDER BY RANDOM() LIMIT 1', (needle,))
text = cur.fetchone()
if text:
return '\n'.join(line for line in text[0].splitlines() if not line.startswith('>>'))
else:
return
cur = conn.execute('SELECT text FROM kcposts ORDER BY RANDOM() LIMIT 1')
text = cur.fetchone()[0]
return '\n'.join(line for line in text.splitlines() if not line.startswith('>>'))
def stats():
conn = sqlite3.connect('lb.sqlite')
cur = conn.execute('SELECT name,country,count(id) as c FROM posts GROUP BY ident ORDER BY c DESC LIMIT 20')
return '\n'.join('%s | %s | %s' % row for row in cur.fetchall())
def online():
conn = sqlite3.connect('lb.sqlite')
cur = conn.execute('SELECT name,country FROM posts WHERE date>? GROUP BY ident', (datetime.now()-timedelta(minutes=3),))
return '\n'.join('%s | %s' % row for row in cur.fetchall())
def countries():
conn = sqlite3.connect('lb.sqlite')
cur = conn.execute('SELECT country_name,count(id) as c FROM posts WHERE country_name!="" GROUP BY country_name ORDER BY c DESC LIMIT 20')
return '\n'.join('%s|%s' % row for row in cur.fetchall())
def regions():
conn = sqlite3.connect('lb.sqlite')
cur = conn.execute('SELECT country,count(id) as c FROM posts WHERE country!="" GROUP BY country ORDER BY c DESC LIMIT 20')
return '\n'.join('%s|%s' % row for row in cur.fetchall())
def reddit(subreddit=None):
if not subreddit:
subreddit = 'pics'
try:
res = json.loads(requests.get('https://www.reddit.com/r/%s/top.json?sort=top&t=week&limit=100' % subreddit, headers={'User-Agent':'/r/your_user_name'}).text)['data']['children']
post = random.choice(res)['data']
text = post['title']
if 'i.imgur.com' not in post['url'] and 'imgur.com' in post['url']:
response = re.findall(r'(http://i.imgur.com/[\w\.]+)', requests.get(post['url']).text)
if response:
post['url'] = response[0]
if "i.imgur.com" in post['url'] or post['url'].endswith('.jpg') or post['url'].endswith('.png'):
response = requests.get(post['url'], stream=True)
img = 'reddit%s' % os.path.splitext(post['url'])[1]
with open(img, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
return (text, img)
if post['url']:
text += "\n%s" % post['url']
if post.get('selftext'):
text += "\n%s" % post['selftext']
return (text, "anna" + str(random.randint(1,5)) + ".png")
except Exception as e:
print e
return
def norris():
res = requests.get('http://api.icndb.com/jokes/random')#, params={'firstName': '', 'lastName': ''})
return res.json()['value']['joke']
refuse_message = "That query was ambiguous, Onii-chan"
refuse_message = "You want shit? Idiot"
refuse_message = "Хоћеш срање? Идиоте."
if (len(sys.argv) < 2):
print "Usage: python anna.py [channel]"
exit()
channel = sys.argv[1]
wolfram = wolframalpha.Client(config.wolframAPI)
# globals
users = {}
replies = {
'russia':"Russia STRONG!",
'usa': "USA USA USA USA USA USA USA! Vote Trump! USA USA USA USA!",
'weed': 'dude 420 lmao',
'ayy': 'lmao',
'idiot': 'You want shit?',
}
annaposts = []
lastpost = datetime.now()
def process_chat(*args):
img = "anna" + str(random.randint(6,8)) + ".png"
try:
ident = args[0]["identifier"]
global users
global lastpost
global cb
if ident in users:
interval = (datetime.now() - users[ident]).total_seconds()
#if interval < 7:
# return
message = args[0]["body"]
name = args[0]["name"]
count = str(args[0]["count"])
convo = args[0]["convo"]
country_name = args[0]["country_name"]
country = args[0]["country"]
if "trip" in args[0]:
trip = args[0]["trip"]
else:
trip = ""
if trip == "!!n60sL82Wd2" and name == "anna":
annaposts.append(count)
return
# default message
out_message = ""
if not message.strip().startswith('.'):
with sqlite3.connect('lb.sqlite') as conn:
conn.execute('INSERT INTO posts(id,ident,name,trip,convo,text,country,country_name,date) VALUES(?,?,?,?,?,?,?,?,?);',(count, ident, name, trip, convo, message, country, country_name, datetime.now()))
for k, v in replies.items():
if message.lower() == '.%s' % k:
out_message = v
#if (('to die' in message.lower() or 'death' in message.lower() or 'suicide' in message.lower()) and 'want' in message.lower()) or "kill me" in message.lower():
# out_message = random.choice(["Kill urself already.", "Just do it. Kill yourself.", "Suicide is the answer.", "Die"])
# helpful
t = re.compile('[wW]hat (is|are) (.+)\?').match(message)
if (t):
try:
res = wolfram.query(t.group(2))
#out_message = next(res.results).text
out_message = '\n'.join(z.text for z in res.pods[1:] if z)
#out_message = wikipedia.summary(t.group(1), sentences=1)
except Exception as e:
print res.__dict__
print out_message
out_message = ""
print "wolfram error",e
# kc
t = re.compile('\.kc( (.+))?').match(message)
if (t):
res = kc_quote(t.group(1))
if res:
out_message = res
# reddit
t = re.compile('\.reddit( (.+))?').match(message)
if (t):
if t.group(1) and t.group(1).strip().replace('_','').isalpha() and not re.match('.*(gay|cock|penis|ass)',t.group(1)):
res = reddit(t.group(1).strip())
else:
res = reddit()
if res:
out_message, img = res
# urban
t = re.compile('\.urban (.+)').match(message)
if (t):
res = ''
for l in urbandict.define(t.group(1)):
res += "def: %s\nexample: %s\n" % (l['def'].strip(), l['example'].strip())
if res:
out_message = res
# play
t = re.compile('\.play( (.+))?').match(message)
if (t):
out_message, img = game.play(t.group(1), ident, name, country)
convo = "hangman"
# wolfram
t = re.compile('\.wa (.+)').match(message)
if (t):
try:
res = wolfram.query(t.group(1))
out_message = next(res.results).text
except Exception as e:
out_message = refuse_message
img = "anna" + str(random.randint(1,5)) + ".png"
#img = "shit.jpg"
print e
# wiki
t = re.compile('\.wiki(pedia)? (.+)').match(message)
if (t):
try:
out_message = wikipedia.summary(t.group(2), sentences=3)
except wikipedia.DisambiguationError as e:
out_message = str(e)
except Exception as e:
out_message = refuse_message
img = "anna" + str(random.randint(1,5)) + ".png"
#img = "shit.jpg"
print e
# google
t = re.compile('\.google( (.+))?').match(message)
if (t):
try:
r = duckduckgo.query(t.group(2))
for i in xrange(len(r.related) if len(r.related) < 4 else 3):
result = r.related[i]
out_message += '\n'+ result.text + '\n'
out_message += '[i]' + result.url + ' [/i]\n'
except Exception as e:
out_message = refuse_message
img = "anna" + str(random.randint(1,5)) + ".png"
#img = "shit.jpg"
print e
# random
t = re.compile('\.random( (.+))?').match(message)
if (t):
try:
if t.group(1) and t.group(2).isdigit():
out_message += str(random.randint(0, int(t.group(2))))
else:
out_message += str(random.randint(0, 100))
if int(out_message)%10 == int(out_message)/10:
out_message += " (you got doubles :3)"
except Exception as e:
out_message = "That was ambiguous, Onii-chan"
img = "anna" + str(random.randint(1,5)) + ".png"
#img = "shit.jpg"
print e
# fortune
t = re.compile('\.fortune( (.+))?').match(message)
if (t):
out_message = os.popen('fortune fortunes').read().strip()
# riddle
t = re.compile('\.riddle( (.+))?').match(message)
if (t):
out_message = os.popen('fortune riddles').read().strip()
# stats
t = re.compile('\.stats( (.+))?').match(message)
if (t):
out_message = stats()
# scores
t = re.compile('\.scores( (.+))?').match(message)
if (t):
out_message = game.stats()
# online
t = re.compile('\.online( (.+))?').match(message)
if (t):
out_message = online()
# countries
t = re.compile('\.countries( (.+))?').match(message)
if (t):
out_message = countries()
# regions
t = re.compile('\.regions( (.+))?').match(message)
if (t):
out_message = regions()
# joke
t = re.compile('\.joke( (.+))?').match(message)
if (t):
out_message = norris()#random.choice(open('jokes.txt').read().split('\n-')).strip()
# meme
t = re.compile('\.meme( (.+))?').match(message)
if (t):
out_message = " "
img = 'memes/'+random.choice(os.listdir('memes'))
# hi
t = re.compile('\.hi( (.+))?').match(message)
if (t):
out_message = "%s, %s!" % (random.choice(['Hi', 'Hello', 'Privet', 'Hola', 'Bonjour', 'Hallo']), name)
# help
t = re.compile('\.help( (.+))?').match(message)
if (t):
out_message = "commands are .hi .porn .kc .random .joke .fortune .google .urban .wa .wiki .riddle .meme .play .reddit .stats .countries .regions .online .scores"
out_message += '\nor "Anna ...?" or "What is/are ...?"'
# porn
t = re.compile('\.porn( (.+))?').match(message)
if (t):
out_message = random.choice([
'You are wankaholic',
'You are addicted to masturbating',
'You are addicted to pornography',
'Hi wanka',
])
img = 'wanka.jpg'
img = 'porn/'+random.choice(os.listdir('porn'))
convo = 'nsfw'
## add
#t = re.compile('\.add (\w+) (.+)').match(message)
#if (t):
# #print t.groups(1)[0], t.groups(1)[1]
# replies[t.groups(1)[0]] = t.groups(1)[1]
if (message.splitlines() and message.splitlines()[0].lstrip('>') in annaposts) or (message.lower().startswith('anna') and message.endswith('?')):
try:
if message.lower().startswith('anna'):
message = message[4:].strip()
out_message = cb.ask(u'\n'.join(line for line in message.splitlines() if not line.startswith('>')))
except Exception as e:
out_message = refuse_message
img = "anna" + str(random.randint(1,5)) + ".png"
#img = "shit.jpg"
print e
cb = Cleverbot()
#if count[-1]==count[-2] and random.randint(1,5)==2:
# out_message = "CHECKED"
# img = "dubs" + str(random.randint(1,5)) + ".jpg"
#if 'anna' in message.lower():
# out_message = "you said my name, Onii-chan?"
#if 'kcmod' in message.lower():
# out_message = "mods are cucks"
if not out_message and random.randint(0,45) == 5 and (datetime.now() - lastpost).total_seconds() > 30:
print (datetime.now() - lastpost).total_seconds()
out_message = kc_quote()
count = 0
if out_message != "":
users[ident] = datetime.now()
lastpost = datetime.now()
if (count):
out_message = ">>"+count+"\n"+out_message#.encode('ascii', 'ignore')
post_chat(out_message, channel,
name="anna", trip=config.annaTrip,
convo=convo, file=img)
except Exception as e:
#print type(e), e
#raise
print e
login(callback=process_chat)
join_chat(channel)
while 1:
sleep(10)