#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Question Omegle chat help


    Hi,
    Im new to Python, so I just got some script off the internet.
    Now I want to try to understand it, but I dont know where to start. The only thing I figured out was 16:len( rec ) means the received text chat.
    Can you guys help me understand the code?

    Code:
    import urllib2 as url
    import urllib
    import httplib as http
    import sys
    from threading import Thread
    import time
    
    t=None
    def fmtId( string ):
        return string[1:len( string ) - 1]
    
    def talk(id,req,text=''):
        typing = url.urlopen('http://omegle.com/typing', '&id='+id)
        typing.close()
        if text=='':
            msg = str(raw_input('> '))
        else:
            msg=text
    
        msgReq = url.urlopen('http://omegle.com/send', '&msg='+msg+'&id='+id)
        msgReq.close()
    
    def listenServer( id, req ):
        site = url.urlopen(req)
        rec = site.read()
        while('connected' not in rec):
            site = url.urlopen(req)
            rec = site.read()
            if 'waiting' in rec:
                print("Waiting...")
        print('Found one')
        t = Thread(None,listen,None,(id,req))
        t.start()
        try:
            say(id,req)
        except  KeyboardInterrupt:
            omegleConnect()
            
    def listen(id,req):
        while True:
            site = url.urlopen(req)
            rec = site.read()
            if 'strangerDisconnected' in rec:
                    print('He is gone')
                    disconnect(id)
                    omegleConnect()
            elif 'typing' in rec:
                    print("He's typing something...")          
            elif 'gotMessage' in rec:
                    if 'asl' in rec[16:len( rec ) - 2] or 'm/f' in rec[16:len( rec ) - 2] or 'webcam' in rec[16:len( rec ) - 2]:
                        print(rec[16:len( rec ) - 2])
                        talk(id,req,'Bai!')
                        disconnect(id)
                        print('Disconnected, trying to find new target..')
                        omegleConnect()
                    else:
                        print(rec[16:len( rec ) - 2])
                
    def say(id,req):
        while True:
            talk(id,req)
            time.sleep(5)
    
    def omegleConnect():
        site = url.urlopen('http://omegle.com/start','')
        id = fmtId( site.read() )
        print(id)
        req = url.Request('http://omegle.com/events', urllib.urlencode( {'id':id}))
        print('Gotta find one')
        listenServer(id,req)
    
    def disconnect( id ):
        url.urlopen( 'http://omegle.com/disconnect', urllib.urlencode( {'id':id} ) )
       
    omegleConnect()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Up to the last line, the program defines stuff.
    That is, it stores into the global name space some words. The names starting in the first column to the left hand side of the "=" sign are associated with the evaluated expressions on the right hand side of the "=". I saw one of these, t=None. "t" is associated with None, because the expression None can't be further reduced.
    The names between "def" and "(", with def starting again in the left hand column become associated with the executable statement block indented below them. Now, while that block is merely stored for later use, the expressions with in the (function, arguments) are evaluated right away. For example, in
    def talk(id,req,text='')
    the "''" expression evaluates to an empty string which becomes a default value for the text argument were the talk function to be used.

    The import statements also populated the global name space (note, "global" means "global within this module"). In this case you've shown

    import httplib as http
    import sys
    from threading import Thread

    putting http, sys, and Thread into the global name space. As python loads these modules it recursively goes through all the processes I've just explained populating those modules global name spaces accordingly. Reference objects in these modules using their names as module.name . Some renaming occurs. When in your module you see "http" it actually refers to the httplib module. Were you to look for documentation or source code you'd need to search for "httplib", not "http". The sys module is loaded normally. Finally, the code doesn't make all of the threading module available to you. Just whatever happens to be associated with Thread. You'd find that in the "threading" documentation at docs.python.org/library .

    I'm glad there wasn't a class definition as well. A class definition would require more explanation.

    The last line of your program "omegleConnect()" looks for the name omegleConnect, finds an associated callable (has an __call__ method. If you don't believe me use print(dir(omegleConnect)) before the call to it, but after omegleConnect is defined). And the following "()" invokes the statements. Track it all from there.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Status check?


    I sort of understand most of the code now I think.
    But, after a while Omeggle comes up with a Captcha message. How do I check for this ? Doesnt this give a 404 error or something?
    Afaik, something along the lines of this
    Code:
    if 'REcaptcha' in rec[16:len( rec ) - 2]
    print('Error! Please go to omegle and fill in captcha!')
    This check for text in the web page I think..

IMN logo majestic logo threadwatch logo seochat tools logo