Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 200 lines (155 sloc) 6.033 kb
146c92f0 »
2012-04-14 initial commit
1 import time
2 import json
3 from urllib import urlencode
4
5 import gevent
6 from gevent.queue import Queue
7
c0eb7ed4 »
2012-04-14 another bugfix >.>
8 from requests import async
146c92f0 »
2012-04-14 initial commit
9 import requests
10
df57f6a5 »
2012-04-14 added event object (not a bugfix!)
11 class Event(object):
12 def __init__(self, event):
48a64eca »
2012-04-14 special attribute for message events
13 self.id = event[0][0]
df57f6a5 »
2012-04-14 added event object (not a bugfix!)
14 self.user_id = event[0][1]
15 self.timestamp = int(event[0][2])
16 self.type = event[0][3][0]
17 self.args = event[0][3][1:]
18
48a64eca »
2012-04-14 special attribute for message events
19 if self.type=="Chat":
20 self.message = self.args[0]
21
146c92f0 »
2012-04-14 initial commit
22 class CosketchSession(object):
23
24 def __init__(self, nick, channel):
25
26 self.channel = channel
27 self.uid = round(time.time(),5)
28 self.user_id = None
29 self.lc = -1
30 self._pc_base = str(int(time.time()*1000))
31 self._pc = 0
32 self.sc = 0
33
34 self.action_queue = Queue()
35 self.event_queue = Queue()
a23442fb »
2012-04-14 added event handler registration
36 self.event_handlers = {}
146c92f0 »
2012-04-14 initial commit
37
38 self.session = requests.session()
39
40 # set cookies and authenticate
41 self.session.get('http://cosketch.com/Rooms/'+channel)
42 self.session.post(self.upload_url, {'d': '["Login"]'})
43
44 self.set_nick(nick)
45
46 @property
47 def pc(self):
48 return self._pc_base + str(self._pc)
49
50 @property
51 def upload_url(self):
52 return 'http://cosketch.com/Upload.aspx?'+urlencode({
53 'channel': self.channel,
54 'U': self.uid,
55 'LC': self.lc,
56 't': 'n',
57 })
58
59 @property
60 def download_url(self):
61 return 'http://cosketch.com/Download.aspx?'+urlencode({
62 'channel': self.channel,
63 'U': self.uid,
64 'LC': self.lc,
65 'pc': self.pc,
66 't': 'n',
67 })
68
69 def d(self, data):
70 self.sc += 1
71 return {'d': '{"v":71,"ul":[[%s,%s]]}' % (self.sc, data)}
72
73 def run(self):
74 gevent.spawn(self.event_dispatcher)
75 gevent.spawn(self.process_action_queue)
76 while True:
77 response = self.session.get(self.download_url)
78
79 if response.content == "REFRESH":
80 continue
81
82 parsed = json.loads(response.content)
83 self.lc = parsed['myLast']
84 self._pc += 1
85
a23442fb »
2012-04-14 added event handler registration
86 if self.lc > -1:
87 self.event_queue.put(parsed['dl'])
88
89 def register_event_handler(self, event_type, callback):
90 if event_type not in self.event_handlers:
91 self.event_handlers[event_type] = []
92
93 self.event_handlers[event_type].append(callback)
146c92f0 »
2012-04-14 initial commit
94
95 def event_dispatcher(self):
96 while True:
df57f6a5 »
2012-04-14 added event object (not a bugfix!)
97 event = Event(self.event_queue.get())
a23442fb »
2012-04-14 added event handler registration
98 print 'EVENT:',event.type,':',event.user_id,':',event.args
146c92f0 »
2012-04-14 initial commit
99
100 try:
df57f6a5 »
2012-04-14 added event object (not a bugfix!)
101 if event.type == 'ChangedName' and event.args[0] == self.nick:
102 self.user_id = event.user_id
146c92f0 »
2012-04-14 initial commit
103
a23442fb »
2012-04-14 added event handler registration
104 if event.type in self.event_handlers:
105 for cb in self.event_handlers[event.type]:
bde3cc1c »
2012-04-16 Added text function for writing text
106 try:
107 cb(self, event)
108 except:
109 import traceback
110 traceback.print_exc()
146c92f0 »
2012-04-14 initial commit
111 except:
112 pass
113
114 def process_action_queue(self):
115 while True:
116 data = self.action_queue.get()
117 response = self.session.post(self.upload_url, data)
118 print response.content
119
4498174f »
2012-04-16 cleaned up a little bit and added default arguments to stroke function
120 def stroke(self, points, color="#000000", width=5):
146c92f0 »
2012-04-14 initial commit
121
122 if type(points[0]) is not int:
123 points = [p for point in points for p in point]
124
125 data = self.d('["Stroke",0,"%s",%s,%s,255]' % (color,width,points))
154df261 »
2012-04-14 yet another late-night bug fix >.>
126 self.action_queue.put(data)
4498174f »
2012-04-16 cleaned up a little bit and added default arguments to stroke function
127
128 def text(self, text, position, color="#000000", size=14):
bde3cc1c »
2012-04-16 Added text function for writing text
129 data = self.d('["Text",{x},{y},"{text}","{color}",{size}]'.format(
4498174f »
2012-04-16 cleaned up a little bit and added default arguments to stroke function
130 x = position[0],
131 y = position[1],
132 text = text,
133 color = color,
134 size = size))
bde3cc1c »
2012-04-16 Added text function for writing text
135 self.action_queue.put(data)
136
146c92f0 »
2012-04-14 initial commit
137 def set_nick(self, nick):
138 self.nick = nick.replace('"','\\"')
139
140 data = self.d('["RequestNameChange","%s"]' % self.nick)
141 self.action_queue.put(data)
142
143 def chat(self, message):
144 data = self.d('["Chat","%s"]' % message.replace('"','\\"'))
145 self.action_queue.put(data)
146
147 if __name__ == '__main__':
148
df57f6a5 »
2012-04-14 added event object (not a bugfix!)
149 c = CosketchSession('sketchr', 'udderweb3')
146c92f0 »
2012-04-14 initial commit
150
151 points = [(303,183),(301,185),(299,187),(298,189),(295,191),(293,194),
152 (291,196),(288,198),(286,200),(284,202),(281,204),(279,205),
153 (276,207),(274,209),(272,211),(271,212),(270,214),(269,215),
154 (268,216),(267,216),(265,217),(263,218),(262,219),(261,219),
155 (260,220),(258,220),(258,221),(257,222),(256,223)]
156
157 c.chat('hello world!')
158
159 c.stroke(points, '#000000',3)
160 c.stroke(points, '#FF0000',5)
161 c.stroke(points, '#00FF00',7)
162 c.stroke(points, '#0000FF',9)
163
164 c.stroke([0,0,200,200], '#FF0000', 5)
165 c.stroke([200,0,0,200], '#00FF00', 5)
166 c.stroke([100,0,100,200], '#0000FF', 5)
167
a23442fb »
2012-04-14 added event handler registration
168 def hello(session, event):
48a64eca »
2012-04-14 special attribute for message events
169 if event.message == '!hello':
a23442fb »
2012-04-14 added event handler registration
170 session.chat('hello world!')
171
172 def yo(session, event):
48a64eca »
2012-04-14 special attribute for message events
173 if event.message == 'yo':
a23442fb »
2012-04-14 added event handler registration
174 session.chat('wuddup dawg')
175
176 def random_lines(session, event):
177 from random import randrange
48a64eca »
2012-04-14 special attribute for message events
178 if '!rand' in event.message:
179 n = int(event.message.split()[1])
a23442fb »
2012-04-14 added event handler registration
180 for i in xrange(0, n):
181 x1, y1 = randrange(0,800), randrange(0,600)
182 x2, y2 = randrange(0,800), randrange(0,600)
183 width = randrange(1,5)
184 session.stroke([x1, y1, x2, y2], '#000000', width)
185
186 def fortune(session, event):
187 import os
48a64eca »
2012-04-14 special attribute for message events
188 if event.message == '!fortune':
a23442fb »
2012-04-14 added event handler registration
189 p = os.popen('fortune -s')
190 fortune = p.read()
191 fortune = ' '.join(fortune.split()).replace('\n',' ')
192 session.chat(fortune)
193
194 c.register_event_handler('Chat', hello)
195 c.register_event_handler('Chat', yo)
196 c.register_event_handler('Chat', random_lines)
197 c.register_event_handler('Chat', fortune)
198
146c92f0 »
2012-04-14 initial commit
199 c.run()
Something went wrong with that request. Please try again.