summaryrefslogtreecommitdiff
path: root/omegle.py
diff options
context:
space:
mode:
Diffstat (limited to 'omegle.py')
-rw-r--r--omegle.py106
1 files changed, 59 insertions, 47 deletions
diff --git a/omegle.py b/omegle.py
index a3294e0..d80075a 100644
--- a/omegle.py
+++ b/omegle.py
@@ -4,9 +4,16 @@ import urllib
import time
import json
import logging
-from threading import Thread, Lock
+from threading import Thread, Lock, Condition
+
+__all__ = ['OmegleChat', 'get_count']
class Timer(Thread):
+ """
+ This class defines a object that calls the function object
+ given as func every given interval in a Thread and stops immedietly after
+ stop() has been called
+ """
def __init__(self,func,interval=0.50):
Thread.__init__(self)
self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
@@ -15,62 +22,66 @@ class Timer(Thread):
self.running = False
+ self.lock = Condition()
+
def run(self):
self.running = True
+ self.lock.acquire()
while self.running:
self.func()
- time.sleep(self.interval)
+ self.lock.wait(self.interval)
self.logger.debug("self.running == False")
def stop(self):
self.running = False
+ self.lock.notify()
-
-class OmegleChat(object):
+class RESTClient(object):
headers = {
"Host" : "omegle.com",
"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
"Accept": "application/json"
}
+ def __init__(self,host,http_debuglevel=0):
+ self.conn_lock = Lock()
+ self.conn = httplib.HTTPConnection(host)
+ self.conn.set_debuglevel(http_debuglevel)
+
+ def request(self,method,path,params):
+ self.conn_lock.acquire()
+ self.conn.request(method,path,params,RESTClient.headers)
+ resp = self.conn.getresponse()
+ self.conn_lock.release()
- def __init__(self,name="",poll_interval=0.5):
- self.conn = httplib.HTTPConnection('www.omegle.com')
-# self.conn.set_debuglevel(1)
+ return resp
- self.conn_lock = Lock()
+class OmegleChat(RESTClient):
+ def __init__(self,poll_interval=0.5):
+ RESTClient.__init__(self,'www.omegle.com')
self.timer = Timer(self.events, poll_interval)
self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name)
self.is_connected = False
def start(self):
- self.conn_lock.acquire()
- self.conn.request("POST", "/start", {}, OmegleChat.headers)
- self.conn_lock.release()
- r=self.conn.getresponse()
- body=r.read()
+ resp = RESTClient.request(self, "POST", "/start", {})
+ body=resp.read()
id=body.split("\"")
if id.__len__() == 3:
self.id = id[1]
self.logger.info("Connected id=%s" % self.id)
+ #Poll events
+ self.timer.start()
else:
self.logger.error("Bad response: %s" % body)
raise Exception("Bad response: %s" % body)
- #Poll events
- self.timer.start()
def disconnect(self):
self.logger.debug("/disconnect")
self.is_connected = False
self.timer.stop()
- self.conn_lock.acquire()
- self.conn.request("POST",
- "/disconnect",
- urllib.urlencode({'id' : self.id}),
- OmegleChat.headers)
- r = self.conn.getresponse()
- self.conn_lock.release()
+ r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id}))
body = r.read()
self.logger.debug("/disconnect sent. Read: %s" % body)
if body == "win":
@@ -80,6 +91,7 @@ class OmegleChat(object):
raise Exception("/disconnect; Bad response: %s" % body)
def events(self):
+ """does use its own "HTTPConnection" because its called async from a thread"""
conn = httplib.HTTPConnection('www.omegle.com')
conn.request("POST",
"/events",
@@ -91,50 +103,35 @@ class OmegleChat(object):
conn.close()
def send(self,msg):
- msg = unicode(msg).encode("utf8")
- self.conn_lock.acquire()
- self.conn.request("POST",
- "/send",
- urllib.urlencode({'id':self.id,'msg':msg}),
- OmegleChat.headers)
- r=self.conn.getresponse()
- self.conn_lock.release()
+ #if a raw string given, assume its utf8
+ if msg.__class__ == str:
+ msg = msg.decode("utf8")
+ #convert utf8 to RAW-utf8
+ msg = msg.encode("utf8")
+
+ r = RESTClient.request(self,"POST","/send",urllib.urlencode({'id':self.id,'msg':msg}))
body = r.read()
if body != "win":
self.logger.error("/send; Bad response %s" % body)
raise Exception("/send; Bad response %s" % body)
def typing(self):
- self.conn_lock.acquire()
- self.conn.request("POST",
- "/typing",
- urllib.urlencode({'id':self.id}),
- OmegleChat.headers)
- r=self.conn.getresponse()
- self.conn_lock.release()
+ r = RESTClient.request(self, "POST", "/typing", urllib.urlencode({'id':self.id}))
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()
+ r = RESTClient.request(self, "POST", "/stoppedtyping", urllib.urlencode({'id':self.id}))
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(unicode(body))
+ data_set=json.read(body.decode("utf8"))
if not data_set:
self.logger.debug("event: no data received")
return
@@ -172,11 +169,26 @@ class OmegleChat(object):
def on_wait(self):
self.logger.info("Server sent [\"waiting\"]")
+def get_count():
+ """Return the number of current online omegle users"""
+ headers = {
+ "Host" : "omegle.com",
+ "Content-type": "application/x-www-form-urlencoded; charset=utf-8",
+ "Accept": "application/json"}
+ conn = httplib.HTTPConnection('www.omegle.com')
+ conn.request("GET",
+ "/count",
+ urllib.urlencode({}),
+ headers)
+ body = conn.getresponse().read()
+ conn.close()
+ return body
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
print "Lets chat. Type \"quit\" to disconnect"
chat = OmegleChat()
+ chat.on_disconnect = lambda: sys.exit(0)
chat.start()
while 1==1:
try: