summaryrefslogtreecommitdiff
path: root/megahal_bot.py
blob: 9865df729a7c77a67cec1620582744df284c5ecb (plain)
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
import logging
import sys
from threading import Event

import mh_python
import time
import pyPgSQL.PgSQL
from omegle import OmegleChat



class MegahalBot(OmegleChat):
	def __init__(self,disconnect_event,name,host="omegle.com",color=0):
		self.disconnect_event = disconnect_event
		self.name=name
		self.color = color
		OmegleChat.__init__(self,name="Megahal",host=host)
		self.idlecount = 0
		

	def c(self,str,bold=False,color=0):
		if color == 0:
			color = self.color
		if bold:
			return "\033[1m\033[%sm%s\033[0m\033[0m" %(color, str)
		else:
			return "\033[%sm%s\033[0m" %(self.color, str)


	def respond(self,message):
		r=mh_python.doreply(message.encode("utf-8"))
		print self.c("%s [MSG] %s: %s" %(time.strftime("%H:%M:%S"), self.name, r),bold=True)
		self.send(r.decode("utf-8"))

	def on_idle(self):
		self.idlecount += 1
		if self.idlecount > 8:
			self.logger.info("Idle > 8. disconnect()")
			self.disconnect()
		else:
			self.logger.info("Idle count %s/8" % self.idlecount)
	def on_message(self,message):
		self.idlecount = 0
		print self.c("%s [MSG] %s: %s" %(time.strftime("%H:%M:%S"), "Stranger", message),bold=True,color=32)
		time.sleep(1)
		self.respond(message)

	def on_connect(self):
		print self.c("%s [EVT] %s Connection confirmed" % (time.strftime("%H:%M:%S"), self.name))


	def on_disconnect(self):
		print self.c("%s [EVT] %s disconnect" % (time.strftime("%H:%M:%S"), self.name))
		self.disconnect_event.set()



if __name__ == "__main__":
	mh_python.initbrain()

	conn = pyPgSQL.PgSQL.connect("")
	cur = conn.cursor()
	cur.execute("SELECT message FROM omegle_messages")
	messages = cur.fetchall()
	c=0
	for message in messages:
		c+=1
		if c%20==0:
			print "%s%s/%s" % ("\033[2K\033[E",c,len(messages)),
		#XXX pass massage[0] to mh and end with a fucked up python
		mh_python.learn("%s " % (message[0]))

	print ""
	cur.close()
	conn.close()

	logging.basicConfig(level=logging.DEBUG)
	print "press ctrl-c to abort"
	
	event = Event()
	if len(sys.argv) == 2:
		bot=MegahalBot(event,"A",host=sys.argv[1],color=31)
	else: 
		bot=MegahalBot(event,"A",host="omegle.com",color=31)#red

	bot.start()

	try:
               while True:
                       event.wait(0.5)
                       if event.isSet():
                               print "partner"
                               break
	except KeyboardInterrupt:
		print "CTRL-C pressed, exiting"

	if bot.is_connected: bot.disconnect()