-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpyak.py
343 lines (289 loc) · 9.92 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
import json
import requests
import time
import urllib
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
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"])
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.handle = raw["handle"]
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"]
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://www.yikyakapp.com/YikYakFiles/"
user_agent = "android-async-http/1.4.4 (http://loopj.com/android-async-http)"
def __init__(self, user_id=None):
if user_id is None:
user_id = self.register_id()
self.id = user_id
self.location = Location("0", "0")
self.handle = None
#self.update_stats()
def get(self, page, params):
url = self.base_url + page
if len(params) > 0:
url += "?" + urllib.urlencode(params)
headers = {
"User-Agent": self.user_agent,
"Accept-Encoding": "gzip",
}
return requests.get(url, 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.php", params)
def upvote_yak(self, message_id):
params = {
"userID": self.id,
"messageID": message_id
}
return self.get("likeMessage.php", params)
def downvote_yak(self, message_id):
params = {
"userID": self.id,
"messageID": message_id
}
return self.get("downvoteMessage.php", params)
def upvote_comment(self, comment_id):
params = {
"userID": self.id,
"commentID": comment_id
}
return self.get("likeComment.php", params)
def downvote_comment(self, comment_id):
params = {
"userID": self.id,
"commentID": comment_id
}
return self.get("downvoteComment.php", params)
def report_yak(self, message_id):
params = params = {
"userID": self.id,
"messageID": message_id
}
return self.get("reportMessage.php", params)
def delete_yak(self, message_id):
params = params = {
"userID": self.id,
"messageID": message_id
}
return self.get("deleteMessage2.php", params)
def report_comment(self, comment_id, message_id):
params = {
"userID": self.id,
"commentID": comment_id,
"messageID": message_id,
}
return self.get("reportMessage.php", params)
def delete_comment(self, comment_id, message_id):
params = {
"userID": self.id,
"commentID": comment_id,
"messageID": message_id,
}
return self.get("deleteComment.php", params)
def update_handle(self, handle):
success = False
if handle != self.handle:
self.handle = handle
params = {
"userID": self.id,
"handle": self.handle
}
success = bool(self.get("updateHandle.php", params) == "0")
return success
def get_handle_info(self):
params = {
"userID": self.id,
}
return self.get("getHandleInfo.php", params).text
def update_stats(self):
params = {
"userID": self.id,
}
stats = self.get("getMyStats.php", params).text.split()
#TODO: fix this for real...
try:
self.num_messages = int(stats[0])
self.neg3 = int(stats[1])
self.upvotes_given = int(stats[2])
self.downvotes_given = int(stats[3])
self.yak_score = int(stats[4])
except IndexError:
pass
def get_greatest(self):
return self.get_yak_list("getGreatest.php", {})
def get_my_tops(self):
params = {
"userID": self.id,
}
return self.get_yak_list("getMyTops.php", params)
def get_recent_replied(self):
params = {
"userID": self.id,
}
return self.get_yak_list("getMyRecentReplied.php", params)
def update_location(self, location):
self.loc = location
params = {
"userID": self.id,
"lat": location.latitude,
"long": location.longitude,
}
return self.get("updateLocation.php", params)
def get_my_recent_yaks(self):
params = {
"userID": self.id,
}
return self.get_yak_list("getMyRecentYaks.php", params)
def get_area_tops(self, loc=None):
if loc is None:
loc = self.location
params = {
"lat": loc.latitude,
"long": loc.longitude,
}
return self.get_yak_list("getAreaTops.php", params)
def get_yaks(self):
params = {
"userID": self.id,
}
return self.get_yak_list("getMessages.php", params)
def post_yak(self, message, loc=None, showloc=False, handle=False):
if loc is None:
loc = self.location
params = {
"userID": self.id,
"lat": loc.latitude,
"long": loc.longitude,
"message": message,
}
if not showloc:
params["hidePin"] = "1"
if handle and (self.handle is not None):
params["hndl"] = self.handle
return self.get("sendMessage.php", params)
def get_comments(self, message_id):
params = {
"userID": self.id,
"messageID": message_id
}
return self.parse_comments(self.get("getComments.php", params).text, message_id)
def post_comment(self, message_id, comment):
params = {
"userID": self.id,
"messageID": message_id,
"comment": comment,
}
return self.get("postComment.php", params)
def peek(self, location):
params = {
"userID": self.id,
"lat": location.latitude,
"long": location.longitude,
"delta": location.delta,
}
return self.get_yak_list("getPeekMessages.php", params)
def register_id(self):
result = self.get("registerUserDroid.php", {})
return result.text