Advertisement
Guest User

Brendan Warkentin

a guest
Feb 3rd, 2010
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. #!/usr/bin/env python2.6
  2.  
  3. import json, urllib, urllib2, threading, time
  4.  
  5. class myomegler:
  6.     def __init__(self, bot = None):
  7.         self.jd = json.decoder.JSONDecoder()
  8.         self.op = urllib2.build_opener()
  9.  
  10.         self.connected = False
  11.         self.typing = False
  12.         self.id = None
  13.  
  14.         # Bot
  15.         self.bot = bot
  16.  
  17.         #Threads
  18.         self.typ_t = threading.Thread(target = self.typing_off)
  19.  
  20.         self.evt_t = threading.Thread(target = self.events)
  21.         self.evt_t.start()
  22.  
  23.         self.con_t = threading.Thread(target = self.connect)
  24.         self.con_t.start()
  25.  
  26.     def __del__(self):
  27.         try:
  28.             self.evt_t = None
  29.             self.con_t = None
  30.             self.typ_t = None
  31.         except:
  32.             pass
  33.    
  34.     def events(self):
  35.         while True:
  36.             if not self.connected:
  37.                 time.sleep(0.2)
  38.             else:
  39.                 rep = self.getEvent()
  40.  
  41.                 if rep[0] == "waiting" or rep[0] == "connected":
  42.                     self.connected = True
  43.  
  44.                     if not self.bot == None: self.bot.notify("con")
  45.                     print "*** connected" ###
  46.                 elif rep[0] == "typing":
  47.                     try:
  48.                         self.typ_t = threading.Thread(target = self.typing_off)
  49.                     except:
  50.                         pass
  51.                     self.typing = True
  52.                     self.typ_t.start()
  53.                     print "*** " + rep[0] ###
  54.                 elif rep[0] == "stoppedTyping":
  55.                     self.typing = False
  56.                     print "*** " + rep[0] ###
  57.                 elif rep[0] == "strangerDisconnected":
  58.                     self.connected = False
  59.                     self.typing = False
  60.                     self.id = None
  61.  
  62.                     if not self.bot == None: self.bot.notify("dis")
  63.                     print "*** " + rep[0] ###
  64.                 elif rep[0] == "gotMessage":
  65.                     try:
  66.                         self.typ_t = threading.Thread(target = self.typing_off)
  67.                     except:
  68.                         pass
  69.                     self.typing = False
  70.                     self.recv(rep[1])
  71.                 else:
  72.                     print "***>>>"
  73.                     print rep
  74.                     print "***<<<"
  75.  
  76.     def connect(self):
  77.         while True:
  78.             if self.connected:
  79.                 time.sleep(0.2)
  80.             else:
  81.                 try:
  82.                     r = self.op.open("http://omegle.com/start?rcs=1", "")
  83.                     ret = r.read()
  84.                     r.close()
  85.  
  86.                     ret = ret[1:-1]
  87.                     self.id = ret
  88.                     self.connected = True
  89.                 except:
  90.                     pass
  91.    
  92.     def disconnect(self):
  93.         try:
  94.             info = urllib.urlencode({"id":self.id})
  95.             r = self.op.open("http://omegle.com/disconnect", info)
  96.             r.read()
  97.             r.close()
  98.             print "*** youDisconnected" ###
  99.         except:
  100.             pass
  101.  
  102.     def typing_off(self):
  103.         time.sleep(10)
  104.         self.typing = False
  105.  
  106.     def getEvent(self):
  107.         ret = None
  108.  
  109.         while type(ret) != list:
  110.             try:
  111.                 info = urllib.urlencode({"id":self.id})
  112.                 r = self.op.open("http://omegle.com/events", info, 30)
  113.                 ret = r.read()
  114.                 r.close()
  115.                 ret = self.jd.decode(ret)
  116.             except:
  117.                 pass
  118.  
  119.         return ret[0]
  120.  
  121.     def set_typing(self):
  122.         try:
  123.             info = urllib.urlencode({"id":self.id})
  124.             r = self.op.open("http://omegle.com/typing", info)
  125.             r.read()
  126.             r.close()
  127.         except:
  128.             pass
  129.  
  130.     def send(self, msg):
  131.         while not self.connected:
  132.             time.sleep(0.2)
  133.         try:
  134.             info = urllib.urlencode({"id":self.id, "msg":msg})
  135.             r = self.op.open("http://omegle.com/send", info)
  136.             r.read()
  137.             r.close()
  138.         except:
  139.             pass
  140.  
  141.         print "You: " + msg
  142.  
  143.     def recv(self, msg):
  144.         print "Stranger: " + msg
  145.         if not self.bot == None: self.bot.notify("msg", msg)
  146.  
  147. import hal, os
  148.  
  149. class bot:
  150.     def __init__(self):
  151.         self.connected = False
  152.         self.typing = False
  153.         self.o = myomegler(self)
  154.         #if os.path.exists(hal.FILENAME):
  155.         #   with open(hal.FILENAME, 'rb') as fp:
  156.         #       self.h = hal.pickle.load(fp) #Buggy
  157.         #else:
  158.         self.h = hal.HAL()
  159.         self.h.train("training")
  160.         #self.h.train("logs/logs.lrn")
  161.    
  162.     def __del__(self):
  163.         del self.o
  164.  
  165.     def notify(self, type, msg = None):
  166.         if type == "con":
  167.             self.connected = True
  168.         elif type == "dis":
  169.             self.connected = False
  170.             self.mp = 0
  171.         elif type == "msg":
  172.             self.recv(msg)
  173.    
  174.     def recv(self, msg):
  175.         self.o.send(self.h.process(msg))
  176.  
  177. #o = myomegler()
  178. b = bot()
  179. try:
  180.     while True:
  181.         time.sleep(1)
  182. except:
  183.     print "##TERMINATED##"
  184.     del b
  185.     import os
  186.     os._exit(1)
  187.  
  188.  
  189. """
  190.  
  191. __Protocol Dissection__
  192.  
  193. omegle.com
  194.  
  195. Start Convo:
  196.     POST start?rcs=1
  197.     Reply: "sOm3Id"
  198.  
  199. Periodic Events:
  200.     POST events
  201.         id=sOm3Id
  202.     Replies(JSON):
  203.         [["waiting"], ["connected"]]
  204.         [["typing"]]
  205.         [["stoppedTyping"]]
  206.         [["gotMessage", "Hi"]]
  207.         [["strangerDisconnected"]]
  208.    
  209.     Other Info: Loop with 30 second timeout
  210.  
  211. Typing:
  212.     POST typing
  213.         id=sOm3Id
  214.     Reply: win
  215.  
  216. Send:
  217.     POST send
  218.         id=sOm3Id
  219.         msg=Hi
  220.     Reply: win
  221.  
  222. Disconnect:
  223.     POST disconnect
  224.         id=sOm3Id
  225.     Reply: win
  226.  
  227. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement