forked from lageraci/pyak
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyak.py
457 lines (382 loc) · 13.6 KB
/
pyak.py
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import base64
import hmac
import json
import requests
import time
import urllib
import os
from hashlib import sha1
from hashlib import md5
def parse_time(timestr):
format = "%Y-%m-%d %H:%M:%S"
return time.mktime(time.strptime(timestr, format))
class Location:
def __init__(self, latitude, longitude, delta=None):
self.latitude = latitude
self.longitude = longitude
if delta is None:
delta = "0.030000"
self.delta = delta
def __str__(self):
return "Location(%s, %s)" % (self.latitude, self.longitude)
class PeekLocation:
def __init__(self, raw):
self.id = raw['peekID']
self.can_submit = bool(raw['canSubmit'])
self.name = raw['location']
lat = raw['latitude']
lon = raw['longitude']
d = raw['delta']
self.location = Location(lat, lon, d)
class Comment:
def __init__(self, raw, message_id, client):
self.client = client
self.message_id = message_id
self.comment_id = raw["commentID"]
self.comment = raw["comment"]
self.time = parse_time(raw["time"])
self.likes = int(raw["numberOfLikes"])
self.poster_id = raw["posterID"]
self.liked = int(raw["liked"])
self.message_id = self.message_id.replace('\\', '')
def upvote(self):
if self.liked == 0:
self.likes += 1
self.liked += 1
return self.client.upvote_comment(self.comment_id)
def downvote(self):
if self.liked == 0:
self.likes -= 1
self.liked -= 1
return self.client.downvote_comment(self.comment_id)
def report(self):
return self.client.report_comment(self.comment_id, self.message_id)
def delete(self):
if self.poster_id == self.client.id:
return self.client.delete_comment(self.comment_id, self.message_id)
def reply(self, comment):
return self.client.post_comment(self.message_id, comment)
def print_comment(self):
my_action = ""
if self.liked > 0:
my_action = "^"
elif self.liked < 0:
my_action = "v"
print "%s(%s) %s" % (my_action, self.likes, self.comment)
class Yak:
def __init__(self, raw, client):
self.client = client
self.poster_id = raw["posterID"]
self.hide_pin = bool(int(raw["hidePin"]))
self.message_id = raw["messageID"]
self.delivery_id = raw["deliveryID"]
self.longitude = raw["longitude"]
self.comments = int(raw["comments"])
self.time = parse_time(raw["time"])
self.latitude = raw["latitude"]
self.likes = int(raw["numberOfLikes"])
self.message = raw["message"]
self.type = raw["type"]
self.liked = int(raw["liked"])
self.reyaked = raw["reyaked"]
#Yaks don't always have a handle
try:
self.handle = raw["handle"]
except KeyError:
self.handle = None
#For some reason this seems necessary
self.message_id = self.message_id.replace('\\', '')
def upvote(self):
if self.liked == 0:
self.liked += 1
self.likes += 1
return self.client.upvote_yak(self.message_id)
def downvote(self):
if self.liked == 0:
self.liked -= 1
self.likes -= 1
return self.client.downvote_yak(self.message_id)
def report(self):
return self.client.report_yak(self.message_id)
def delete(self):
if self.poster_id == self.client.id:
return self.client.delete_yak(self.message_id)
def add_comment(self, comment):
return self.client.post_comment(self.message_id, comment)
def get_comments(self):
return self.client.get_comments(self.message_id)
def print_yak(self):
if self.handle is not None:
print "%s:" % self.handle
print self.message
print "%s likes, %s comments. posted %s at %s %s" % (self.likes, self.comments, self.time, self.latitude, self.longitude)
class Yakker:
base_url = "http://yikyakapp.com/api/"
user_agent = "android-async-http/1.4.4 (http://loopj.com/android-async-http)"
def __init__(self, user_id=None, location=None, force_register=False):
if location is None:
location = Location('0', '0')
self.update_location(location)
if user_id is None:
user_id = self.gen_id()
self.register_id_new(user_id)
elif force_register:
self.register_id_new(user_id)
self.id = user_id
self.handle = None
#self.update_stats()
def gen_id(self):
return md5(os.urandom(128)).hexdigest().upper()
def register_id_new(self, id):
params = {
"userID": id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
result = self.get("registerUser", params)
return result
def sign_request(self, page, params):
key = "35FD04E8-B7B1-45C4-9886-94A75F4A2BB4"
#The salt is just the current time in seconds since epoch
salt = str(int(time.time()))
#The message to be signed is essentially the request, with parameters sorted
msg = "/api/" + page
sorted_params = params.keys()
sorted_params.sort()
if len(params) > 0:
msg += "?"
for param in sorted_params:
msg += "%s=%s&" % (param, params[param])
#Chop off last "&"
if len(params) > 0:
msg = msg[:-1]
#the salt is just appended directly
msg += salt
#Calculate the signature
h = hmac.new(key, msg, sha1)
hash = base64.b64encode(h.digest())
return hash, salt
def get(self, page, params):
url = self.base_url + page
hash, salt = self.sign_request(page, params)
params['hash'] = hash
params['salt'] = salt
headers = {
"User-Agent": self.user_agent,
"Accept-Encoding": "gzip",
}
return requests.get(url, params=params, headers=headers)
def post(self, page, params):
url = self.base_url + page
hash, salt = self.sign_request(page, params)
getparams = {'hash': hash, 'salt': salt}
headers = {
"User-Agent": self.user_agent,
"Accept-Encoding": "gzip",
}
return requests.post(url, data=params, params=getparams, headers=headers)
def get_yak_list(self, page, params):
return self.parse_yaks(self.get(page, params).text)
def parse_yaks(self, text):
try:
raw_yaks = json.loads(text)["messages"]
except:
raw_yaks = []
yaks = []
for raw_yak in raw_yaks:
yaks.append(Yak(raw_yak, self))
return yaks
def parse_comments(self, text, message_id):
try:
raw_comments = json.loads(text)["comments"]
except:
raw_comments = []
comments = []
for raw_comment in raw_comments:
comments.append(Comment(raw_comment, message_id, self))
return comments
def contact(self, message):
params = {
"userID": self.id,
"message": message
}
return self.get("contactUs", params)
def upvote_yak(self, message_id):
params = {
"userID": self.id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("likeMessage", params)
def downvote_yak(self, message_id):
params = {
"userID": self.id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("downvoteMessage", params)
def upvote_comment(self, comment_id):
params = {
"userID": self.id,
"commentID": comment_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("likeComment", params)
def downvote_comment(self, comment_id):
params = {
"userID": self.id,
"commentID": comment_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("downvoteComment", params)
def report_yak(self, message_id):
params = params = {
"userID": self.id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("reportMessage", params)
def delete_yak(self, message_id):
params = params = {
"userID": self.id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("deleteMessage2", params)
def report_comment(self, comment_id, message_id):
params = {
"userID": self.id,
"commentID": comment_id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("reportMessage", params)
def delete_comment(self, comment_id, message_id):
params = {
"userID": self.id,
"commentID": comment_id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get("deleteComment", params)
def get_greatest(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getGreatest", params)
def get_my_tops(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getMyTops", params)
def get_recent_replied(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getMyRecentReplies", params)
def update_location(self, location):
self.location = location
def get_my_recent_yaks(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getMyRecentYaks", params)
def get_area_tops(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getAreaTops", params)
def get_yaks(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.get_yak_list("getMessages", params)
def post_yak(self, message, showloc=False, handle=False):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
"message": message,
}
if not showloc:
params["hidePin"] = "1"
if handle and (self.handle is not None):
params["hndl"] = self.handle
return self.post("sendMessage", params)
def get_comments(self, message_id):
params = {
"userID": self.id,
"messageID": message_id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.parse_comments(self.get("getComments", params).text, message_id)
def post_comment(self, message_id, comment):
params = {
"userID": self.id,
"messageID": message_id,
"comment": comment,
"lat": self.location.latitude,
"long": self.location.longitude,
}
return self.post("postComment", params)
def get_peek_locations(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
data = self.get("getMessages", params).json()
peeks = []
for peek_json in data['otherLocations']:
peeks.append(PeekLocation(peek_json))
return peeks
def get_featured_locations(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
data = self.get("getMessages", params).json()
peeks = []
for peek_json in data['featuredLocations']:
peeks.append(PeekLocation(peek_json))
return peeks
def get_yakarma(self):
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
}
data = self.get("getMessages", params).json()
return int(data['yakarma'])
def peek(self, peek_id):
if isinstance(peek_id, PeekLocation):
peek_id = peek_id.id
params = {
"userID": self.id,
"lat": self.location.latitude,
"long": self.location.longitude,
'peekID': peek_id,
}
return self.get_yak_list("getPeekMessages", params)