Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.6
- import json, urllib, urllib2, threading, time
- class myomegler:
- def __init__(self, bot = None):
- self.jd = json.decoder.JSONDecoder()
- self.op = urllib2.build_opener()
- self.connected = False
- self.typing = False
- self.id = None
- # Bot
- self.bot = bot
- #Threads
- self.typ_t = threading.Thread(target = self.typing_off)
- self.evt_t = threading.Thread(target = self.events)
- self.evt_t.start()
- self.con_t = threading.Thread(target = self.connect)
- self.con_t.start()
- def __del__(self):
- try:
- self.evt_t = None
- self.con_t = None
- self.typ_t = None
- except:
- pass
- def events(self):
- while True:
- if not self.connected:
- time.sleep(0.2)
- else:
- rep = self.getEvent()
- if rep[0] == "waiting" or rep[0] == "connected":
- self.connected = True
- if not self.bot == None: self.bot.notify("con")
- print "*** connected" ###
- elif rep[0] == "typing":
- try:
- self.typ_t = threading.Thread(target = self.typing_off)
- except:
- pass
- self.typing = True
- self.typ_t.start()
- print "*** " + rep[0] ###
- elif rep[0] == "stoppedTyping":
- self.typing = False
- print "*** " + rep[0] ###
- elif rep[0] == "strangerDisconnected":
- self.connected = False
- self.typing = False
- self.id = None
- if not self.bot == None: self.bot.notify("dis")
- print "*** " + rep[0] ###
- elif rep[0] == "gotMessage":
- try:
- self.typ_t = threading.Thread(target = self.typing_off)
- except:
- pass
- self.typing = False
- self.recv(rep[1])
- else:
- print "***>>>"
- print rep
- print "***<<<"
- def connect(self):
- while True:
- if self.connected:
- time.sleep(0.2)
- else:
- try:
- r = self.op.open("http://omegle.com/start?rcs=1", "")
- ret = r.read()
- r.close()
- ret = ret[1:-1]
- self.id = ret
- self.connected = True
- except:
- pass
- def disconnect(self):
- try:
- info = urllib.urlencode({"id":self.id})
- r = self.op.open("http://omegle.com/disconnect", info)
- r.read()
- r.close()
- print "*** youDisconnected" ###
- except:
- pass
- def typing_off(self):
- time.sleep(10)
- self.typing = False
- def getEvent(self):
- ret = None
- while type(ret) != list:
- try:
- info = urllib.urlencode({"id":self.id})
- r = self.op.open("http://omegle.com/events", info, 30)
- ret = r.read()
- r.close()
- ret = self.jd.decode(ret)
- except:
- pass
- return ret[0]
- def set_typing(self):
- try:
- info = urllib.urlencode({"id":self.id})
- r = self.op.open("http://omegle.com/typing", info)
- r.read()
- r.close()
- except:
- pass
- def send(self, msg):
- while not self.connected:
- time.sleep(0.2)
- try:
- info = urllib.urlencode({"id":self.id, "msg":msg})
- r = self.op.open("http://omegle.com/send", info)
- r.read()
- r.close()
- except:
- pass
- print "You: " + msg
- def recv(self, msg):
- print "Stranger: " + msg
- if not self.bot == None: self.bot.notify("msg", msg)
- import hal, os
- class bot:
- def __init__(self):
- self.connected = False
- self.typing = False
- self.o = myomegler(self)
- #if os.path.exists(hal.FILENAME):
- # with open(hal.FILENAME, 'rb') as fp:
- # self.h = hal.pickle.load(fp) #Buggy
- #else:
- self.h = hal.HAL()
- self.h.train("training")
- #self.h.train("logs/logs.lrn")
- def __del__(self):
- del self.o
- def notify(self, type, msg = None):
- if type == "con":
- self.connected = True
- elif type == "dis":
- self.connected = False
- self.mp = 0
- elif type == "msg":
- self.recv(msg)
- def recv(self, msg):
- self.o.send(self.h.process(msg))
- #o = myomegler()
- b = bot()
- try:
- while True:
- time.sleep(1)
- except:
- print "##TERMINATED##"
- del b
- import os
- os._exit(1)
- """
- __Protocol Dissection__
- omegle.com
- Start Convo:
- POST start?rcs=1
- Reply: "sOm3Id"
- Periodic Events:
- POST events
- id=sOm3Id
- Replies(JSON):
- [["waiting"], ["connected"]]
- [["typing"]]
- [["stoppedTyping"]]
- [["gotMessage", "Hi"]]
- [["strangerDisconnected"]]
- Other Info: Loop with 30 second timeout
- Typing:
- POST typing
- id=sOm3Id
- Reply: win
- Send:
- POST send
- id=sOm3Id
- msg=Hi
- Reply: win
- Disconnect:
- POST disconnect
- id=sOm3Id
- Reply: win
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement