Skip to content

Commit 996f8e7

Browse files
committed
- User ID is saved to file
- Some refactoring
1 parent 73fe900 commit 996f8e7

File tree

1 file changed

+76
-42
lines changed

1 file changed

+76
-42
lines changed

YikYak.py

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def main():
88
print("Note: This app is currently only for viewing and posting yaks at any location. There is no ability to vote or delete yet.\n\n")
99

1010
geocoder = pygeocoder.Geocoder("AIzaSyAGeW6l17ATMZiNTRExwvfa2iuPA1DvJqM")
11-
11+
1212
try:
1313
# If location already set in past, read file
1414
f = open("locationsetting", "r")
@@ -17,85 +17,89 @@ def main():
1717
currentlatitude = coords[0]
1818
currentlongitude = coords[1]
1919
print("Location is set to: ", coords[2])
20-
20+
2121
coordlocation = pk.Location(currentlatitude, currentlongitude)
22-
22+
2323
f.close()
24-
24+
2525
except:
26+
# If first time using app, ask for preferred location
2627
coordlocation = newLocation(geocoder)
2728
# If location retrieval fails, ask user for coordinates
2829
if coordlocation == 0:
2930
print("Please enter coordinates manually: ")
3031
currentlatitude = input("Latitude: ")
3132
currentlongitude = input("Longitude: ")
3233
coordlocation = pk.Location(currentlatitude, currentlongitude)
33-
34+
3435
print()
35-
36-
# start API and get list of yaks
37-
remoteyakker = pk.Yakker(None, coordlocation, False)
38-
yaklist = remoteyakker.get_yaks()
39-
36+
37+
try:
38+
# If user already has ID, read file
39+
f = open("userID", "r")
40+
userID = f.read()
41+
f.close()
42+
43+
# start API with saved user ID
44+
remoteyakker = pk.Yakker(userID, coordlocation, False)
45+
46+
except:
47+
# start API and create new user ID
48+
remoteyakker = pk.Yakker(None, coordlocation, True)
49+
50+
try:
51+
# Create file if it does not exist and write user ID
52+
f = open("userID", 'w+')
53+
f.write(remoteyakker.id)
54+
f.close()
55+
56+
except:
57+
pass
58+
59+
print("User ID: ", remoteyakker.id, "\n")
60+
4061
while True:
4162
choice = input("Read(R), Post(P), Choose New Location(L), or Quit(Q) -> ")
4263
# Read Yaks
4364
if choice == 'R' or choice == 'r':
44-
for yak in yaklist:
45-
# line between yaks
46-
print ("_" * 93)
47-
yak.print_yak()
48-
49-
# comments header
50-
comments = yak.get_comments()
51-
print ("\n\t\tComments:", end='')
52-
print (len(comments))
53-
54-
# print all comments separated by dashes
55-
for comment in comments:
56-
print ("\t\t", end='')
57-
print ("-" * 77)
58-
comment.print_comment()
59-
65+
yaklist = remoteyakker.get_yaks()
66+
read(yaklist)
67+
6068
# Post Yak
6169
elif choice == 'P' or choice == 'p':
6270
message = input("Enter message to yak: \n")
71+
6372
handle = input("Add handle: (Blank to omit): \n")
6473
showlocation = input("Show location? (Y/N)")
65-
74+
6675
if showlocation == 'Y' or showlocation == 'y':
6776
allowlocation = True
6877
else:
6978
allowlocation = False
70-
79+
7180
if handle == '':
7281
posted = remoteyakker.post_yak(message, showloc=allowlocation)
7382
else:
7483
posted = remoteyakker.post_yak(message, showloc=allowlocation, handle=handle)
75-
84+
7685
if posted:
7786
print("\nYak successful :)\n\n")
7887
else:
7988
print("\nYak failed :(\t", end='')
8089
print (posted.status_code, end='')
8190
print (" ", end='')
8291
print (requests.status_codes._codes[posted.status_code][0])
83-
92+
93+
# Change Location
8494
elif choice == 'L' or choice == 'l':
85-
print()
86-
coordlocation = newLocation(geocoder)
87-
if coordlocation == 0:
88-
print("Please enter coordinates manually: ")
89-
currentlatitude = input("Latitude: ")
90-
currentlongitude = input("Longitude: ")
91-
coordlocation = pk.Location(currentlatitude, currentlongitude)
95+
coordlocation = changeLocation(geocoder)
9296
remoteyakker.update_location(coordlocation)
9397
yaklist = remoteyakker.get_yaks()
94-
98+
9599
# Quit App
96100
elif choice == 'Q' or choice == 'q':
97101
break;
98-
102+
99103
def newLocation(geocoder):
100104
# figure out location latitude and longitude based on address
101105
address = input("Enter college name or address: ")
@@ -104,11 +108,11 @@ def newLocation(geocoder):
104108
except:
105109
print("\nGoogle Geocoding API has reached the limit of queries.\n")
106110
return 0
107-
111+
108112
coordlocation = 0
109113
try:
110114
coordlocation = pk.Location(currentlocation.latitude, currentlocation.longitude)
111-
115+
112116
# Create file if it does not exist and write
113117
f = open("locationsetting", 'w+')
114118
coordoutput = str(currentlocation.latitude) + '\n' + str(currentlocation.longitude)
@@ -118,7 +122,37 @@ def newLocation(geocoder):
118122
f.close()
119123
except:
120124
print("Unable to get location.")
121-
125+
122126
return coordlocation
123127

128+
def changeLocation(geocoder):
129+
print()
130+
coordlocation = newLocation(geocoder, address)
131+
if coordlocation == 0:
132+
print("Please enter coordinates manually: ")
133+
currentlatitude = input("Latitude: ")
134+
currentlongitude = input("Longitude: ")
135+
coordlocation = pk.Location(currentlatitude, currentlongitude)
136+
137+
def read(yaklist):
138+
yakNum = 1
139+
for yak in yaklist:
140+
# line between yaks
141+
print ("_" * 93)
142+
print (yakNum)
143+
yak.print_yak()
144+
145+
# comments header
146+
comments = yak.get_comments()
147+
print ("\n\t\tComments:", end='')
148+
print (len(comments))
149+
150+
# print all comments separated by dashes
151+
for comment in comments:
152+
print ("\t\t", end='')
153+
print ("-" * 77)
154+
comment.print_comment()
155+
156+
yakNum += 1
157+
124158
main()

0 commit comments

Comments
 (0)