diff options
Diffstat (limited to 'omegle.py')
-rw-r--r-- | omegle.py | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -37,6 +37,7 @@ class Timer(Thread): self.event.set() class RESTClient(object): + """Some routines used for calling the webservice""" headers = { "Host" : "omegle.com", "Content-type": "application/x-www-form-urlencoded; charset=utf-8", @@ -52,20 +53,21 @@ class RESTClient(object): self.conn.request(method,path,params,RESTClient.headers) resp = self.conn.getresponse() self.conn_lock.release() - return resp class OmegleChat(RESTClient): def __init__(self,poll_interval=0.5,name=""): RESTClient.__init__(self,'www.omegle.com') - self.timer = Timer(self.events, poll_interval) + self.timer = Timer(self._events, poll_interval) self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name) self.is_confirmed = False #after getting a connection confirm self.is_connected = False #after having self.id def start(self): + """Starts a chat session. You have to wait until + is_confirmed is set (or use on_connect)""" self.logger.debug("/start") if self.is_connected: self.logger.error("Already connected") @@ -76,7 +78,7 @@ class OmegleChat(RESTClient): if id.__len__() == 3: self.id = id[1] self.logger.info("Connected id=%s" % self.id) - #Poll events + #start /events polling self.timer.start() self.is_connected = True else: @@ -84,11 +86,13 @@ class OmegleChat(RESTClient): raise Exception("Bad response: %s" % body) def disconnect(self): + """Close a chat session.""" self.logger.debug("disconnect()") self.is_connected = False self.timer.stop() if self.is_confirmed: self.is_confirmed = False + self.id = None r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id})) body = r.read() self.logger.debug("/disconnect sent. Read: %s" % body) @@ -99,7 +103,7 @@ class OmegleChat(RESTClient): self.is_confirmed = True raise Exception("/disconnect; Bad response: %s" % body) - def events(self): + def _events(self): """does use its own "HTTPConnection" because its called async from a thread""" conn = httplib.HTTPConnection('www.omegle.com') conn.request("POST", @@ -108,10 +112,14 @@ class OmegleChat(RESTClient): OmegleChat.headers) r=conn.getresponse() body=r.read() - self.dispatch_event(conn,body) + self._dispatch_event(conn,body) conn.close() def send(self,msg): + """Send a message to the Stranger + Arguments: + msg -- the message - in utf8 + """ #if a raw string given, assume its utf8 if msg.__class__ == str: msg = msg.decode("utf8") @@ -125,6 +133,7 @@ class OmegleChat(RESTClient): raise Exception("/send; Bad response %s" % body) def typing(self): + """Notify the Stranger that youre typing a message""" r = RESTClient.request(self, "POST", "/typing", urllib.urlencode({'id':self.id})) body = r.read() if body != "win": @@ -132,13 +141,14 @@ class OmegleChat(RESTClient): raise Exception("/typing; Bad response %s" % body) def stopped_typing(self): + """Notify the Stranger that youre stopped typing""" 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): + def _dispatch_event(self,conn,body): try: data_set=json.read(body.decode("utf8")) if not data_set: @@ -167,16 +177,22 @@ class OmegleChat(RESTClient): self.logger.error("Json ReadException. Body: %s" % body) def on_message(self,message): + """To be overwritten""" self.logger.info("<<< %s" % message) def on_connect(self): + """To be overwritten""" self.logger.info("Connection confirmed") def on_typing(self): + """To be overwritten""" self.logger.info("Stranger is typing") def on_stopped_typing(self): + """To be overwritten""" self.logger.info("Stranger stopped typing") def on_disconnect(self): + """To be overwritten""" self.logger.info("Stranger Disconnectet") def on_wait(self): + """To be overwritten""" self.logger.info("Server sent [\"waiting\"]") def get_count(): |