From 99c698893ee66a24e0c3eb1004c0c4ce44cb16fe Mon Sep 17 00:00:00 2001 From: yvesf Date: Wed, 27 May 2009 19:58:52 +0000 Subject: blah git-svn-id: http://xapek.org/svn/common/omegle@1012 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- Documentation.txt | 9 ++--- test.py | 104 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 86 insertions(+), 27 deletions(-) diff --git a/Documentation.txt b/Documentation.txt index b2aa48b..80386c0 100644 --- a/Documentation.txt +++ b/Documentation.txt @@ -31,10 +31,11 @@ RETURNS: [[TYPE,data...],...] EXAMPLE 5: [["waiting"], ["connected"]] Return Messagetypes: - ["typing"] - Stranger is typing - ["gotMessage", "ABCDE"] - Stranger send "ABCDE" - ["waiting"] - ??? - ["connected"] - ready to send messages + ["typing"] - Stranger is typing + ["gotMessage", "ABCDE"] - Stranger send "ABCDE" + ["waiting"] - ??? + ["connected"] - ready to send messages + ["strangerDisconnected"] - Stranger pressed disconnect == Send Message URL: /send diff --git a/test.py b/test.py index 188d7dc..19564bb 100644 --- a/test.py +++ b/test.py @@ -1,17 +1,46 @@ import sys import httplib import urllib +import time +from threading import Thread, Lock + +class Timer(Thread): + def __init__(self,func,interval=0.50): + Thread.__init__(self) + self.func = func + self.interval = interval + + self.running = False + + def run(self): + self.running = True + while self.running: + self.func() + time.sleep(self.interval) + + def stop(self): + self.running = False + -headers = { - "Host" : "omegle.com", - "Content-type": "application/x-www-form-urlencoded; charset=utf-8", - "Accept": "application/json"} class OmegleChat(object): - def __init__(self): + headers = { + "Host" : "omegle.com", + "Content-type": "application/x-www-form-urlencoded; charset=utf-8", + "Accept": "application/json" + } + + def __init__(self,poll_interval=0.5): self.conn = httplib.HTTPConnection('www.omegle.com') - self.conn.set_debuglevel(100) +# self.conn.set_debuglevel(100) + + self.conn_lock = Lock() + + self.timer = Timer(self.events, poll_interval) + def start(self): - self.conn.request("POST", "/start", {}, headers) + self.conn_lock.acquire() + self.conn.request("POST", "/start", {}, OmegleChat.headers) + self.conn_lock.release() r=self.conn.getresponse() body=r.read() id=body.split("\"") @@ -20,43 +49,72 @@ class OmegleChat(object): print "Connected, id=%s" % self.id else: raise Exception("Bad response: %s" % body) - self.events() + #Poll events + self.timer.start() def disconnect(self): + self.timer.stop() + + self.conn_lock.acquire() self.conn.request("POST", "/disconnect", urllib.urlencode({'id' : self.id}), - headers) + OmegleChat.headers) r = self.conn.getresponse() - print r.read() - + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/disconnect; Bad response: %s" % body) + def events(self): - self.conn.request("POST", + conn = httplib.HTTPConnection('www.omegle.com') + conn.request("POST", "/events", urllib.urlencode({'id' : self.id}), - headers) - r=self.conn.getresponse() + OmegleChat.headers) + r=conn.getresponse() body=r.read() - print "Poll response: %s" % body - return body + self.on_event(conn,body) + conn.close() def send(self,msg): + self.conn_lock.acquire() self.conn.request("POST", "/send", urllib.urlencode({'id':self.id,'msg':msg}), - headers) + OmegleChat.headers) r=self.conn.getresponse() - print r.read() + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/send; Bad response %s" % body) + + def typing(self,msg): + self.conn_lock.acquire() + self.conn.request("POST", + "/typing", + urllib.urlencode({'id':self.id}), + OmegleChat.headers) + r=self.conn.getresponse() + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/typing; Bad response %s" % body) + + + def on_event(self,conn,body): + print "Event: %s" % body chat = OmegleChat() chat.start() while 1==1: - cmd=sys.stdin.readline().strip() - if cmd=="": - print "Poll:" - chat.events() - elif cmd=="quit": + try: + cmd=sys.stdin.readline().strip() + except KeyboardInterrupt: + chat.disconnect() + sys.exit(0) + if cmd=="quit": chat.disconnect() break else: -- cgit v1.2.1