0

So I have been trying too convert an omegle bot, which was written in python2, to python3. This is the original code: https://gist.github.com/thefinn93/1543082

Now this is my code:

import requests
import sys
import json
import urllib
import random
import time

server = b"odo-bucket.omegle.com"

debug_log = False   # Set to FALSE to disable excessive messages
config = {'verbose': open("/dev/null","w")}
headers = {}
headers['Referer'] = b'http://odo-bucket.omegle.com/'
headers['Connection'] = b'keep-alive'
headers['User-Agent'] = b'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2'
headers['Content-type'] = b'application/x-www-form-urlencoded; charset=UTF-8'
headers['Accept'] = b'application/json'
headers['Accept-Encoding'] = b'gzip,deflate,sdch'
headers['Accept-Language'] = b'en-US'
headers['Accept-Charset'] = b'ISO-8859-1,utf-8;q=0.7,*;q=0.3'

if debug_log:
    config['verbose'] = debug_log

def debug(msg):
    if debug_log:
        print("DEBUG: " + str(msg))
        debug_log.write(str(msg) + "\n")

def getcookies():
    r = requests.get(b"http://" + server + b"/")
    debug(r.cookies)
    return(r.cookies)

def start():
    r = requests.request(b"POST", b"http://" + server + b"/start?rcs=1&spid=", data=b"rcs=1&spid=", headers=headers)
    omegle_id = r.content.strip(b"\"")
    print("Got ID: " + str(omegle_id))
    cookies = getcookies()
    event(omegle_id, cookies)

def send(omegle_id, cookies, msg):
    r = requests.request(b"POST","http://" + server + "/send", data="msg=" + urllib.quote_plus(msg) + "&id=" + omegle_id, headers=headers, cookies=cookies)
    if r.content == "win":
        print("You: " + msg)
    else:
        print("Error sending message, check the log")
        debug(r.content)

def event(omegle_id, cookies):
    captcha = False
    next = False
    r = requests.request(b"POST",b"http://" + server + b"/events",data=b"id=" + omegle_id, cookies=cookies, headers=headers)
    try:
        parsed = json.loads(r.content)
        for e in parsed:
            if e[0] == "waiting":
                print("Waiting for a connection...")
            elif e[0] == "count":
                print("There are " + str(e[1]) + " people connected to Omegle")
            elif e[0] == "connected":
                print("Connection established!")
                send(omegle_id, cookies, "HI I just want to talk ;_;")
            elif e[0] == "typing":
                print("Stranger is typing...")
            elif e[0] == "stoppedTyping":
                print ("Stranger stopped typing")
            elif e[0] == "gotMessage":
                print("Stranger: " + e[1])
                try:
                    cat=""
                    time.sleep(random.randint(1,5))
                    i_r=random.randint(1,8)
                    if i_r==1:
                        cat="that's cute :3"
                    elif i_r==2:
                        cat="yeah, guess your right.."
                    elif i_r==3:
                        cat="yeah, tell me something about yourself!!"
                    elif i_r==4:
                        cat="what's up"
                    elif i_r==5:
                        cat="me too"
                    else:
                        time.sleep(random.randint(3,9))
                        send(omegle_id, cookies, "I really have to tell you something...")
                    time.sleep(random.randint(3,9))
                    cat="I love you."

                    send(omegle_id, cookies, cat)
                except:
                    debug("Send errors!")
            elif e[0] == "strangerDisconnected":
                print("Stranger Disconnected")
                next = True
            elif e[0] == "suggestSpyee":
                print ("Omegle thinks you should be a spy. Fuck omegle.")
            elif e[0] == "recaptchaRequired":
                print("Omegle think's you're a bot (now where would it get a silly idea like that?). Fuckin omegle. Recaptcha code: " + e[1])
                captcha = True

    except:
        print("Derka derka derka")
    if next:
        print("Reconnecting...")
        start()
    elif not captcha:
        event(omegle_id, cookies)

start()

The error I get is:

Traceback (most recent call last):
  File "p3.py", line 124, in <module>
    start()
  File "p3.py", line 46, in start
    r = requests.request(b"POST", b"http://" + server + b"/start?rcs=1&spid=", data=b"rcs=1&spid=", headers=headers)
  File "/usr/lib/python3.4/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 456, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 553, in send
    adapter = self.get_adapter(url=request.url)
  File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 608, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'b'http://odo-bucket.omegle.com/start?rcs=1&spid=''

I didn't really understand what would fix this error, nor what the problem really is, even after looking it up.

UPDATE: Now after removing all the b's I get the following error:

Traceback (most recent call last):
  File "p3.py", line 124, in <module>
    start()
  File "p3.py", line 47, in start
    omegle_id = r.content.strip("\"")
TypeError: Type str doesn't support the buffer API

UPDATE 2: After putting the b back to r.content, I get the following error message:

Traceback (most recent call last):
  File "p3.py", line 124, in <module>
    start()
  File "p3.py", line 50, in start
    event(omegle_id, cookies)
  File "p3.py", line 63, in event
    r = requests.request("POST","http://" + server + "/events",data="id=" + omegle_id,   cookies=cookies, headers=headers)
TypeError: Can't convert 'bytes' object to str implicitly

UPDATE 3: Everytime I try to start it excepts "Derka derka", what could be causing this (It wasn't like that with python2).

Tsumugi Kotobuki
  • 103
  • 1
  • 1
  • 10
  • 2
    Have you tried running `2to3`? It will usually identify problems like this, and can often even fix them automatically. – abarnert Aug 08 '14 at 21:54

1 Answers1

4

requests takes strings, not bytes values for the URL.

Because your URLs are bytes values, requests is converting them to strings with str(), and the resulting string contains the characters b' at the start. That's no a valid scheme like http:// or https://.

The majority of your bytestrings should really be regular strings instead; only the content.strip() call deals with actual bytes.

The headers will be encoded for you, for example. Don't even set the Content-Type header; requests will take care of that for you if you pass in a dictionary (using string keys and values) to the data keyword argument.

You shouldn't set the Connection header either; leave connection management to requests as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Now I get the following error message: Traceback (most recent call last): File "p3.py", line 124, in start() File "p3.py", line 47, in start omegle_id = r.content.strip("\"") TypeError: Type str doesn't support the buffer API – Tsumugi Kotobuki Aug 08 '14 at 22:46
  • So I can just delete the whole header section? – Tsumugi Kotobuki Aug 08 '14 at 22:50
  • I did say `r.content` is a bytes value; that is why you get your error. If the server is returning JSON however, why not use the `response.json()` method instead? – Martijn Pieters Aug 08 '14 at 23:06
  • The rest of the headers may play a role, no need to ditch it all. – Martijn Pieters Aug 08 '14 at 23:07
  • so I put the b back to r.content, and now I get the following error message: Traceback (most recent call last): File "p3.py", line 124, in start() File "p3.py", line 50, in start event(omegle_id, cookies) File "p3.py", line 63, in event r = requests.request("POST","http://" + server + "/events",data="id=" + omegle_id, cookies=cookies, headers=headers) TypeError: Can't convert 'bytes' object to str implicitly – Tsumugi Kotobuki Aug 08 '14 at 23:16
  • You are still mixing bytes and strings somewhere. The `data` parameter will have to be bytes too unless you use a dictionary instead (recommended here). – Martijn Pieters Aug 08 '14 at 23:19
  • Okay thanks I set added the b's to the data parameters, but now my code excepts "Derka derka", this wasn't like that with python2. Any suggestions what I might have done wrong? – Tsumugi Kotobuki Aug 08 '14 at 23:55
  • Remove the blanket except (see [Why is "except: pass" a bad programming practice?](http://stackoverflow.com/q/21553327)) so you can see what the error is. – Martijn Pieters Aug 09 '14 at 07:22