/ Published in: Python
This is one of my first python programs so it's probably written very poorly...
Example use:
print 'Connecting to omegle...'
om = omegle()
if om.chatid:
print 'Chatid: '+ om.chatid
else:
if om.error:
print om.error
else:
print 'No chatid given, we\'re possibly blocked.'
# Wait for our connection
while (not om.connected):
om.getevents()
sleep(0.5)
# Connected; Drop a message
om.send('HAI, ASL!? LOLO')
# Main loop, check for responses
while (om.connected):
om.getevents()
if not om.error:
if om.message:
print 'Stranger: ' + om.message
else:
print om.error
sleep(0.5)
else:
print 'Disconnecting...'
del om
Expand |
Embed | Plain Text
from urllib import urlencode from BaseHTTPServer import BaseHTTPRequestHandler import json class omegle: """A class to interface directly with the Omegle website""" # Global variables url = 'http://omegle.com' useragent = 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)' # Connection status connecting = False connected = False # For Debugging/Error handling response = None error = None # Partner status chatid = None message = None istyping = False def __init__(self): """Alias the connect function""" self.connect() def connect(self): """Connect to the server and get a partners chat id""" self.chatid = str(self.getresponse('/start?rcs=1',{})).strip('"') def getevents(self): """Poll the server for current events""" # Clear any previous error messages self.error = None self.message = False self.typing = False self.response = json.loads(self.getresponse('/events', {'id': self.chatid})) if self.response: for response in self.response: if response[0] == 'waiting': connecting = True elif response[0] == 'connected': connecting = False self.connected = True elif response[0] == 'gotMessage': # Sometimes it's possible to recieve a blank message if response[1]: # If messages aren't encoded the str function fails self.message = str(response[1].encode('utf-8')) elif response[0] == 'typing': self.istyping = True elif response[0] == 'stoppedTyping': self.istyping = False elif response[0] == 'strangerDisconnected': self.connected = False elif response[0] == 'recaptchaRequired': self.connected = False self.error = 'Omegle\'s asking for a captcha.' else: # Fallback for debugging if a command isn't recognised self.error = 'Command not recognised: ' + response[0] return self.error def send(self, message): """Send a message Keyword arguments: message -- The message to send. """ self.response = self.getresponse('/send', {'id': self.chatid, 'msg': message}) if (self.response == 'fail'): self.error = 'Failed to send message: "' + str(message) + '".' def typing(self): """Indicate to the server the client's typing""" self.getresponse('/typing', {'id': self.chatid}) def stoppedTyping(self): """Indicate to the server the client's stopped typing""" self.getresponse('/stoppedTyping', {'id': self.chatid}) def disconnect(self): """Indicate to the server the client's disconnected""" self.getresponse('/disconnect', {'id': self.chatid}) self.connected = False def getresponse(self, url, data=None): """Send data to the server and return the result. Keyword arguments: url -- The url for the page we're getting (relative to self.url). data -- Optional data to post to the server. """ self.error = None try: # Setup the page request request = Request(self.url + url) request.add_header('User-agent', self.useragent) # Have we got data to POST? if data is not None: request.add_data(urlencode(data)) return urlopen(request).read() except URLError, e: # Is our error HTTP or URL based? if hasattr(e, 'reason'): self.error = 'Cannot retrieve URL:' + e.reason[1] elif hasattr(e, 'code'): self.error = 'Cannot retrieve URL:' + str(e.code) + BaseHTTPRequestHandler.responses[e.code][0] def __del__(self): """Alias the disconnect function""" self.disconnect()
You need to login to post a comment.
