summaryrefslogtreecommitdiff
path: root/omegle/__init__.py
blob: 5291b558de6214a1accd51705a699df2381a683e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python
# -*- vim: encoding: utf-8 -*-
# Yves Fischer, xapek.org 2009
import sys
import httplib
import urllib
import json
import logging
from threading import Thread, Lock, Event

__all__ = ['OmegleChat', 'get_count']

class Timer(Thread):
	"""
	This class defines a object that calls the function object
	given as func every given interval in a Thread and stops immedietly after
	stop() has been called
	"""
	def __init__(self,func,interval=0.50):
		Thread.__init__(self)
		self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
		self.func = func
		self.interval = interval

		self.running = False

		self.event = Event()

	def run(self):
		self.running = True
		while self.running:
			self.func()
			self.event.wait(self.interval)
			self.event.clear()
		self.logger.debug("self.running == False")

	def stop(self):
		""" XXX break func() ? """
		self.running = False
		self.event.set()

class RESTClient(object):
	"""Some routines used for calling the webservice"""
	headers = {
		"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
		"Accept": "application/json"
	}
	def __init__(self,host,http_debuglevel=0):
		self.host=host
		self.conn_lock = Lock()
		self.conn = httplib.HTTPConnection(host)
		self.conn.set_debuglevel(http_debuglevel)
	
	def request(self,method,path,params):
		self.conn_lock.acquire()
		try:
			headers = RESTClient.headers.copy()
			headers['Host'] = self.host
			headers['Content-Length'] = str(len(params))
			self.conn.request(method,path,params,headers)
			resp = self.conn.getresponse()
		finally:
			self.conn_lock.release()
		return resp


class OmegleChat(RESTClient):
	def __init__(self,poll_interval=0.5,name="",host="omegle.com"):
		self.name = name
		self.poll_interval=poll_interval
		RESTClient.__init__(self,host)

		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 host=%s" % self.host)
		if self.is_connected:
			self.logger.error("Already connected")
			raise Exception("Already connected")
		resp = RESTClient.request(self, "POST", "/start", {})
		body=resp.read()
		id=body.split("\"")
		if id.__len__() == 3:
			self.id = id[1]
			self.logger.info("Connected id=%s" % self.id)
			#start /events polling
			self.is_connected = True
			self.timer = Timer(self._events, self.poll_interval)
			self.timer.start()
		else:
			self.logger.error("Bad response: %s" % body)
			raise Exception("Bad response: %s" % body)

	def disconnect(self):
		"""Close a chat session."""
		self.logger.debug("disconnect()")
		self.is_confirmed = False
		self.timer.stop()
		if self.is_connected:
			r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id}))
			body = r.read()
			self.logger.debug("/disconnect sent. Read: %s" % body)
			if body == "win":
				self.is_connected = False
				self.id = None
				self.conn.close()
			else:
				raise Exception("/disconnect; Bad response: %s" % body)

	def _events(self):
		"""does use its own "HTTPConnection" because its called async from a thread"""
		conn = httplib.HTTPConnection(self.host)
		params = urllib.urlencode({'id' : self.id})
		headers = OmegleChat.headers.copy()
		headers['Host'] = "omegle.com"
		headers['Content-Length'] = str(len(params))
		conn.request("POST", 
			"/events", 
			params,
			OmegleChat.headers)
		r=conn.getresponse()
		body=r.read()
		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:
			try:
				msg = msg.decode("utf8")
			except UnicodeDecodeError:
				pass
		#convert utf8 to RAW-utf8
		try:
			msg = msg.encode("utf8")
		except UnicodeEncodeError,e:
			self.logger.critical(e)

		if not self.is_confirmed:
			self.logger.error("Cant send message if not is_confirmed")
			return ###XXX raise??

		params = {'id':self.id,'msg':msg}
		params = urllib.urlencode(params)
		try:
			r = RESTClient.request(self,"POST","/send",params)
		except Exception,e:
			self.logger.error("Error by /send request: %s" % e)
			raise e
			return

		body = r.read()
		if body != "win":
			self.logger.error("/send; Bad response %s" % body)
			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":
			self.logger.error("/typing; Bad response %s" % body)
			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):
		try:
			data_set=json.read(body.decode("utf8"))
			if not data_set:
				self.on_idle()
				return
			for data in data_set:
				if not self.is_connected: #in case of disconnect
					break #stop dispatching messages
				if data[0] == "typing":
					self.on_typing()
				elif data[0] == "gotMessage" and data.__len__() == 2:
					self.on_message(data[1])
				elif data[0] == "connected":
					self.is_confirmed = True
					self.on_connect()
				elif data[0] == "strangerDisconnected":
					self.is_confirmed = False
					self.is_connected = False
					self.timer.stop()
					self.on_disconnect()
				elif data[0] == "stoppedTyping":
					self.on_stopped_typing()
				elif data[0] == "waiting":
					self.on_wait()
				else:
					self.logger.error("Unknown JSON Data: %s" % body)
		except json.ReadException:
			self.logger.error("Json ReadException. Body: %s" % body)

	def on_idle(self):
		self.logger.debug("event: no data received")
	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(host="omegle.com"):
	"""Return the number of current online omegle users"""
	headers = {
		"Host" : host,
		"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
		"Accept": "application/json"}
	conn = httplib.HTTPConnection(host)
	conn.request("GET", 
		"/count", 
		urllib.urlencode({}),
		headers)
	body = conn.getresponse().read()
	conn.close()
	return body

if __name__ == "__main__":
	"""Extrem einfacher Chat-Client"""
	logging.basicConfig(level=logging.INFO)
	print "Lets chat. Type \"quit\" to disconnect"
	chat = OmegleChat()
	def exit():
		OmegleChat.on_disconnect(chat)
		sys.exit(0)
	chat.on_disconnect = exit
	chat.start()
	while 1==1:
		try:
			cmd=sys.stdin.readline().strip()
		except KeyboardInterrupt:
			chat.disconnect()
			break
		if cmd=="quit":
			chat.disconnect()
			break
		else:
			print ">>> %s" % cmd
			chat.send(cmd)