-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathYikYak.py
executable file
·474 lines (386 loc) · 14.9 KB
/
YikYak.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#! /usr/bin/env python3
import API as pk
import pygeocoder
import requests
def main():
# Title text
print("\nYik Yak Command Line Edition : Created by djtech42\n\n")
# Initialize Google Geocoder API
geocoder = pygeocoder.Geocoder("AIzaSyAGeW6l17ATMZiNTRExwvfa2iuPA1DvJqM")
try:
# If location already set in past, read file
f = open("locationsetting", "r")
fileinput = f.read()
f.close()
# Extract location coordinates and name from file
coords = fileinput.split('\n')
currentlatitude = coords[0]
currentlongitude = coords[1]
print("Location is set to: ", coords[2])
# Set up coordinate object
coordlocation = pk.Location(currentlatitude, currentlongitude)
except FileNotFoundError:
# If first time using app, ask for preferred location
coordlocation = newLocation(geocoder)
# If location retrieval fails, ask user for coordinates
if coordlocation == 0:
print("Please enter coordinates manually: ")
currentlatitude = input("Latitude: ")
currentlongitude = input("Longitude: ")
coordlocation = pk.Location(currentlatitude, currentlongitude)
print()
try:
# If user already has ID, read file
f = open("userID", "r")
userID = f.read()
f.close()
# start API with saved user ID
remoteyakker = pk.Yakker(userID, coordlocation, False)
except FileNotFoundError:
# start API and create new user ID
remoteyakker = pk.Yakker(None, coordlocation, True)
try:
# Create file if it does not exist and write user ID
f = open("userID", 'w+')
f.write(remoteyakker.id)
f.close()
except:
pass
# Print User Info Text
print("User ID: ", remoteyakker.id, "\n")
print("Connecting to Yik Yak server...\n")
connection = True
try:
print("Yakarma Level:",remoteyakker.get_yakarma(), "\n")
except:
print("Error: Not connected to the Internet\n")
connection = False
if connection:
print("Type one of the one-letter commands below or use the command in conjunction with a parameter.")
currentlist = []
# When actions are completed, user can execute another action or quit the app
while True:
# Insert line gap
print()
# Show all action choices
choice = input("*Read Latest Yaks\t\t(R)\n*Read Top Local Yaks\t\t(T)\n\n*Read Best Yaks of All Time\t(B)\n\n*Show User Yaks\t\t\t(S)\n*Show User Comments\t\t(O)\n\n*Show Top User Yaks\t\t(G)\n\n*Post Yak\t\t\t(P) or (P <message>)\n*Post Comment\t\t\t(C) or (C <yak#>)\n\n*Upvote Yak\t\t\t(U) or (U <yak#>)\n*Downvote Yak\t\t\t(D) or (D <yak#>)\n*Report Yak\t\t\t(E) or (E <yak#>)\n*Show Recent Yak Upvotes\t(A)\n\n*Upvote Comment\t\t\t(V) or (V <yak# comment#>)\n*Downvote Comment\t\t(H) or (H <yak# comment#>)\n*Report Comment\t\t\t(M) or (M <yak# comment#>)\n\n*Yakarma Level\t\t\t(Y)\n\n*Choose New User ID\t\t(I) or (I <userID>)\n*Choose New Location\t\t(L) or (L <location>)\n\n*Contact Yik Yak\t\t(F)\n\n*Quit App\t\t\t(Q)\n\n-> ")
# Read Yaks
if choice.upper() == 'R':
currentlist = remoteyakker.get_yaks()
read(currentlist)
# Read Local Top Yaks
elif choice.upper() == 'T':
currentlist = remoteyakker.get_area_tops()
read(currentlist)
# Read Best of All Time
elif choice.upper() == 'B':
currentlist = remoteyakker.get_greatest()
read(currentlist)
# Show User Yaks
elif choice.upper() == 'S':
currentlist = remoteyakker.get_my_recent_yaks()
read(currentlist)
# Show User Comments
elif choice.upper() == 'O':
currentlist = remoteyakker.get_recent_replied()
read(currentlist)
elif choice.upper() == 'G':
currentlist = remoteyakker.get_my_tops()
read(currentlist)
# Show Recent Yak Upvotes
elif choice.upper() == 'A':
currentlist = remoteyakker.get_yaks()
upvotedlist = []
for yak in currentlist:
if yak.liked:
upvotedlist.append(yak)
if len(upvotedlist) == 0:
print("No recent upvotes")
read(upvotedlist)
# Post Yak
elif choice[0].upper() == 'P':
# set message from parameter or input
if len(choice) > 2:
message = choice[2:]
else:
message = input("Enter message to yak: \n")
# handle and location options
handle = input("Add handle: (Blank to omit): \n")
showlocation = input("Show location? (Y/N) ")
if showlocation.upper() == 'Y':
allowlocation = True
else:
allowlocation = False
if handle == '':
posted = remoteyakker.post_yak(message, showloc=allowlocation)
else:
posted = remoteyakker.post_yak(message, showloc=allowlocation, handle=handle)
if posted:
if len(remoteyakker.get_my_recent_yaks()) > 0 and remoteyakker.get_my_recent_yaks()[0].message == message:
print("\nYak successful :)")
else:
print("\nYak failed :(\t", end='')
else:
print("\nYak failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
# Post Comment
elif choice[0].upper() == 'C':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
# set message from parameter or input
if len(choice) > 2:
yakNum = int(choice[2:])
else:
yakNum = int(input("Enter yak number (displayed above each one): "))
comment = input("Enter comment:\n")
posted = remoteyakker.post_comment(currentlist[yakNum-1].message_id, comment)
if posted:
if len(remoteyakker.get_recent_replied()) > 0 and remoteyakker.get_recent_replied()[0].get_comments()[:-1] == comment:
print("\nComment successful :)")
else:
print("\nComment failed :(\t", end='')
else:
print("\nComment failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Upvote Yak
elif choice[0].upper() == 'U':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
if len(choice) > 2:
# Extract yak number
voteYakNum = int(choice[2:])
else:
voteYakNum = int(input("Enter yak number to upvote (displayed above each one): "))
upvoted = remoteyakker.upvote_yak(currentlist[voteYakNum-1].message_id)
if upvoted:
print("\nUpvote successful :)")
else:
print("\nUpvote failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Downvote Yak
elif choice[0].upper() == 'D':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
if len(choice) > 2:
# Extract yak number
voteYakNum = int(choice[2:])
else:
voteYakNum = int(input("Enter yak number to downvote (displayed above each one): "))
downvoted = remoteyakker.downvote_yak(currentlist[voteYakNum-1].message_id)
if downvoted:
print("\nDownvote successful :)")
else:
print("\nDownvote failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Report Yak
elif choice[0].upper() == 'E':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
if len(choice) > 2:
# Extract yak number
reportYakNum = int(choice[2:])
else:
reportYakNum = int(input("Enter yak number to report (displayed above each one): "))
reported = remoteyakker.report_yak(currentlist[reportYakNum-1].message_id)
if reported:
print("\nReport successful :)")
else:
print("\nReport failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Upvote Comment
elif choice[0].upper() == 'V':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
parameters = choice.split()
if len(parameters) == 3:
yakNum = int(parameters[1])
voteCommentNum = int(parameters[2])
elif len(parameters) == 2:
yakNum = int(parameters[1])
voteCommentNum = int(input("Enter comment number to upvote (displayed above each one): "))
else:
yakNum = int(input("Enter yak number (displayed above each one): "))
voteCommentNum = int(input("Enter comment number to upvote (displayed above each one): "))
upvoted = remoteyakker.upvote_comment(currentlist[yakNum-1].get_comments()[voteCommentNum-1].comment_id)
if upvoted:
print("\nUpvote successful :)")
else:
print("\nUpvote failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Downvote Comment
elif choice[0].upper() == 'H':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
parameters = choice.split()
if len(parameters) == 3:
yakNum = int(parameters[1])
voteCommentNum = int(parameters[2])
elif len(parameters) == 2:
yakNum = int(parameters[1])
voteCommentNum = int(input("Enter comment number to downvote (displayed above each one): "))
else:
yakNum = int(input("Enter yak number (displayed above each one): "))
voteCommentNum = int(input("Enter comment number to downvote (displayed above each one): "))
downvoted = remoteyakker.downvote_comment(currentlist[yakNum-1].get_comments()[voteCommentNum-1].comment_id)
if downvoted:
print("\nDownvote successful :)")
else:
print("\nDownvote failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Report Comment
elif choice[0].upper() == 'M':
# If yaks not loaded, tell user to load one of the options
if len(currentlist) > 0:
parameters = choice.split()
if len(parameters) == 3:
yakNum = int(parameters[1])
reportCommentNum = int(parameters[2])
elif len(parameters) == 2:
yakNum = int(parameters[1])
reportCommentNum = int(input("Enter comment number to report (displayed above each one): "))
else:
yakNum = int(input("Enter yak number (displayed above each one): "))
reportCommentNum = int(input("Enter comment number to report (displayed above each one): "))
reported = remoteyakker.report_comment(currentlist[yakNum-1].get_comments()[reportCommentNum-1].comment_id)
if reported:
print("\nReport successful :)")
else:
print("\nReport failed :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
else:
print ("You must load a list of yaks first by reading latest, top local, best, or user yaks.")
# Yakarma Level
elif choice.upper() == 'Y':
print ("\nYakarma Level:",remoteyakker.get_yakarma())
# Change User ID
elif choice[0].upper() == 'I':
if len(choice) > 2:
remoteyakker = setUserID(remoteyakker.location, choice[2:])
else:
remoteyakker = setUserID(remoteyakker.location)
# Print User Info Text
print("\nUser ID: ", remoteyakker.id, "\n")
print("Connecting to Yik Yak server...\n")
print ("Yakarma Level:",remoteyakker.get_yakarma(), "\n")
# Change Location
elif choice[0].upper() == 'L':
# set location from parameter or input
if len(choice) > 2:
coordlocation = changeLocation(geocoder, choice[2:])
else:
coordlocation = changeLocation(geocoder)
remoteyakker.update_location(coordlocation)
yaklist = remoteyakker.get_yaks()
currentlist = yaklist
# Contact Yik Yak
elif choice.upper() == 'F':
message = input("Enter message to send to Yik Yak: ")
contacted = remoteyakker.contact(message)
if contacted:
print("\nYik Yak contacted successfully :)")
else:
print("\nFailed to contact Yik Yak :(\t", end='')
print (posted.status_code, end='')
print (" ", end='')
print (requests.status_codes._codes[posted.status_code][0])
# Quit App
elif choice.upper() == 'Q':
break;
def newLocation(geocoder, address=""):
# figure out location latitude and longitude based on address
if len(address) == 0:
address = input("Enter college name or address: ")
try:
currentlocation = geocoder.geocode(address)
except:
print("\nGoogle Geocoding API is offline or has reached the limit of queries.\n")
return 0
coordlocation = 0
try:
coordlocation = pk.Location(currentlocation.latitude, currentlocation.longitude)
# Create file if it does not exist and write
f = open("locationsetting", 'w+')
coordoutput = str(currentlocation.latitude) + '\n' + str(currentlocation.longitude)
f.write(coordoutput)
f.write("\n")
f.write(address)
f.close()
except:
print("Unable to get location.")
return coordlocation
def setUserID(location, userID=""):
if userID == "":
userID = input("Enter userID or leave blank to generate random ID: ")
if userID == "":
# Create new userID
remoteyakker = pk.Yakker(None, location, True)
else:
# Use existing userID
remoteyakker = pk.Yakker(userID, location, False)
try:
# Create file if it does not exist and write user ID
f = open("userID", 'w+')
f.write(remoteyakker.id)
f.close()
except:
pass
return remoteyakker
def changeLocation(geocoder, address=""):
coordlocation = newLocation(geocoder, address)
# If location retrieval fails, ask user for coordinates
if coordlocation == 0:
print("\nPlease enter coordinates manually: ")
currentlatitude = input("Latitude: ")
currentlongitude = input("Longitude: ")
coordlocation = pk.Location(currentlatitude, currentlongitude)
return coordlocation
def read(yaklist):
yakNum = 1
for yak in yaklist:
# line between yaks
print ("_" * 93)
# show yak
print (yakNum)
yak.print_yak()
commentNum = 1
# comments header
comments = yak.get_comments()
print ("\n\t\tComments:", end='')
# number of comments
print (len(comments))
# print all comments separated by dashes
for comment in comments:
print ("\t {0:>4}".format(commentNum), end=' ')
print ("-" * 77)
comment.print_comment()
commentNum += 1
yakNum += 1
main()