summaryrefslogtreecommitdiff
path: root/omegle.py
diff options
context:
space:
mode:
Diffstat (limited to 'omegle.py')
-rw-r--r--omegle.py44
1 files changed, 35 insertions, 9 deletions
diff --git a/omegle.py b/omegle.py
index f2efe8a..42f9f77 100644
--- a/omegle.py
+++ b/omegle.py
@@ -3,6 +3,7 @@ import httplib
import urllib
import time
import json
+import logging
from threading import Thread, Lock
class Timer(Thread):
@@ -38,6 +39,8 @@ class OmegleChat(object):
self.timer = Timer(self.events, poll_interval)
+ self.logger = logging.getLogger(__name__ + self.__class__.__name__)
+
def start(self):
self.conn_lock.acquire()
self.conn.request("POST", "/start", {}, OmegleChat.headers)
@@ -47,8 +50,9 @@ class OmegleChat(object):
id=body.split("\"")
if id.__len__() == 3:
self.id = id[1]
- print "Connected, id=%s" % self.id
+ self.logger.info("Connected id=%s" % self.id)
else:
+ self.logger.error("Bad response: %s" % body)
raise Exception("Bad response: %s" % body)
#Poll events
self.timer.start()
@@ -79,6 +83,7 @@ class OmegleChat(object):
conn.close()
def send(self,msg):
+ msg = unicode(msg)
self.conn_lock.acquire()
self.conn.request("POST",
"/send",
@@ -88,6 +93,7 @@ class OmegleChat(object):
self.conn_lock.release()
body = r.read()
if body != "win":
+ self.logger.error("/send; Bad response %s" % body)
raise Exception("/send; Bad response %s" % body)
def typing(self):
@@ -100,14 +106,29 @@ class OmegleChat(object):
self.conn_lock.release()
body = r.read()
if body != "win":
+ self.logger.error("/typing; Bad response %s" % body)
raise Exception("/typing; Bad response %s" % body)
+ def stopped_typing(self):
+ self.conn_lock.acquire()
+ self.conn.request("POST",
+ "/stoppedtyping",
+ urllib.urlencode({'id':self.id}),
+ OmegleChat.headers)
+ r=self.conn.getresponse()
+ self.conn_lock.release()
+ body = r.read()
+ if body != "win":
+ self.logger.error("/stoppedtyping; Bad response %s" % body)
+ raise Exception("/stoppedtyping; Bad response %s" % body)
+
+
def dispatch_event(self,conn,body):
try:
- data_set=json.read(body)
+ data_set=json.read(unicode(body))
if not data_set:
- print "No Data received"
+ self.logger.debug("event: no data received")
return
for data in data_set:
if data[0] == "typing":
@@ -119,24 +140,29 @@ class OmegleChat(object):
elif data[0] == "strangerDisconnected":
self.timer.stop()
self.on_disconnect()
+ elif data[0] == "stoppedTyping":
+ self.on_stopped_typing()
elif data[0] == "waiting":
pass #unhandled
else:
- print "Unknown JSON Data: %s" % body
+ self.logger.error("Unknown JSON Data: %s" % body)
except json.ReadException:
- print "Json ReadException. Body: %s" % body
+ self.logger.error("Json ReadException. Body: %s" % body)
def on_message(self,message):
- print "<<< %s" % message
+ self.logger.info("<<< %s" % message)
def on_connect(self):
- print "Connected"
+ self.logger.info("Connection confirmed")
def on_typing(self):
- print "Stranger is typing"
+ self.logger.info("Stranger is typing")
+ def on_stopped_typing(self):
+ self.logger.info("Stranger stopped typing")
def on_disconnect(self):
- print "Stranger Disconnectet"
+ self.logger.info("Stranger Disconnectet")
if __name__ == "__main__":
+ logging.basicConfig(level=logging.INFO)
print "Lets chat. Type \"quit\" to disconnect"
chat = OmegleChat()
chat.start()