· 4 years ago · Jan 09, 2021, 11:22 AM
1# Student: Jesper Tai
2
3'''
4Psudo code
5
61. Import libraries
7
82. We make the window
9 a. Content of the page
10 b. The background picture of the window
11All the pages will be on seperate functions.
12
133. Menu Page
14 a. Add a heading
15 b. Add a background image
16 c. Add a title
17 d. Contents of the page
18
194. Astronauts in ISS
20 - Add a heading
21 - Add a background image
22 - Add a title
23 - We will request the names from a website
24 - We extract the names
25 - Displaying the names
26 - Connect it back to the main menu
27
285. Current location of the ISS
29 - Add a heading
30 - Add a background image
31 - Add a title
32 - We will request the current coordinates from a website
33 - Extract the info we need
34 - We pass the coordinates to another website for the location name
35 - Display the info
36 - Connect it back to the main menu
37 - We store the info in some kind of database
38
396. Tracking the ISS
40 - Add a heading
41 - Add a background image
42 - Add a title
43 - Extract Data from the db
44 - Display the data
45 - Plot the points, and the trajectory
46 - Connect it back to the main menu
47
487. Exit
49'''
50
51import turtle
52import requests
53import json
54from datetime import datetime
55import sqlite3
56
57connection = sqlite3.connect('ISSData.db')
58# To execute our commands, we need cursor
59cursor = connection.cursor()
60
61cursor.execute(
62 'Create Table if not exists TrackData(time text,latitude float, longitude float, place text)')
63
64
65def insert(time, latitude, longitude, place):
66 with connection:
67 cursor.execute('insert into TrackData values(?,?,?,?)',
68 (time, latitude, longitude, place))
69
70
71def delete():
72 with connection:
73 cursor.execute('delete from TrackData')
74 tracking_ISS()
75
76
77def show():
78 # * means everything
79 cursor.execute('select * from TrackData')
80 return cursor.fetchall()
81
82
83def menu():
84 window.clear()
85 window.title('ISS APP')
86 window.bgpic('stars.gif')
87
88 menu = turtle.Turtle()
89 menu.speed(0)
90 menu.hideturtle()
91 menu.penup()
92 # menu.goto(0, 65)
93
94 # Heading
95 menu.sety(65)
96 menu.color('orange')
97 menu.write('~~~Explore space with Anish~~~',
98 align='center', font=('Arial', 25, 'bold'))
99
100 # Contents of the page
101 menu.goto(-110, 10)
102 menu.right(90)
103 menu.color('white')
104 menu.write('a. Astronauts in ISS', align='left',
105 font=('Arial', 20, 'bold'))
106 menu.forward(15)
107
108 menu.write('b. Current Location of the ISS',
109 align='left', font=('Arial', 20, 'bold'))
110 menu.forward(15)
111
112 menu.write('c. Tracking the ISS', align='left',
113 font=('Arial', 20, 'bold'))
114 menu.forward(15)
115
116 menu.write('d. Exit', align='left', font=('Arial', 20, 'bold'))
117 menu.forward(15)
118
119 # key bindings
120 window.listen()
121 window.onkey(astronauts_in_space, 'a')
122 window.onkey(current_location, 'b')
123 window.onkey(tracking_ISS, 'c')
124 window.onkey(exit, 'd')
125
126
127def exit():
128 # window.
129 window.clear()
130 window.bgpic('stars.gif')
131 window.title('Thank You')
132 ISS = turtle.Turtle()
133 ISS.speed(0)
134 ISS.color('orange')
135 ISS.penup()
136 ISS.hideturtle()
137 string = 'Thank You! Bye!'
138 import time
139 for index in range(len(string)+1):
140 ISS.clear()
141 ISS.write(string[:index], align='center',
142 font=('Arial', 25, 'bold'))
143 time.sleep(0.18)
144 window.exitonclick()
145
146
147def current_location():
148 url = 'http://api.open-notify.org/iss-now.json'
149
150 # window.
151 window.clear()
152 window.bgpic('Map.gif')
153 window.title('Current Location of the ISS')
154 ISS = turtle.Turtle()
155 ISS.speed(0)
156 ISS.color('orange')
157 ISS.penup()
158 ISS.hideturtle()
159 ISS.goto(0, 75)
160 ISS.right(90)
161 ISS.write('Current Location of the ISS',
162 align='center', font=('Arial', 15, 'bold'))
163
164 response = requests.get(url)
165 data = response.json()
166 latitude = float(data['iss_position']['latitude'])
167 longitude = float(data['iss_position']['longitude'])
168 timestamp = data['timestamp']
169 timestamp = datetime.fromtimestamp(timestamp)
170
171 print(f'Latitude : {latitude}')
172 print(f'Longitude : {longitude}')
173 print(f'Timestamp : {timestamp}')
174 url = 'https://us1.locationiq.com/v1/reverse.php?key=316aed2e9d379d'
175 # parameters
176 parameters = {'lat': latitude, 'lon': longitude, 'format': 'json'}
177
178 response = requests.get(url, params=parameters)
179
180 data = response.json()
181
182 place = 'Ocean'
183 if response.status_code == 200:
184 place = data['address']['country']
185
186 # plotting
187 printer = turtle.Turtle()
188 printer.hideturtle()
189 printer.speed(0)
190 printer.penup()
191 printer.color('red')
192 printer.goto(longitude, latitude)
193 printer.right(90)
194 printer.forward(15)
195 string = 'I am here :)'
196
197 import time
198 for index in range(len(string)+1):
199 printer.clear()
200 printer.write(string[:index], align='center',
201 font=('Arial', 12, 'bold'))
202 time.sleep(0.2)
203 printer.back(15)
204 printer.dot(6)
205 printer.forward(25)
206 printer.write(place, align='center', font=('Arial', 12, 'bold'))
207
208 # timest
209 timest = turtle.Turtle()
210 timest.hideturtle()
211 timest.speed(0)
212 timest.penup()
213 timest.color('red')
214 timest.goto(-140, -70)
215 timestamp = str(timestamp)
216 for index in range(len(timestamp)+1):
217 timest.clear()
218 timest.write(timestamp[:index], align='center',
219 font=('Arial', 12, 'bold'))
220 time.sleep(0.2)
221
222 ISS.goto(0, -85)
223 ISS.color('red')
224 ISS.write('Press m to go back to the main menu',
225 align='center', font=('Arial', 10, 'normal'))
226
227 # database
228 insert(time=timestamp, latitude=latitude, longitude=longitude, place=place)
229 print(show())
230
231 window.listen()
232
233 window.onkey(menu, 'm')
234
235
236def tracking_ISS():
237 # window.
238 window.clear()
239 window.bgpic('Map.gif')
240 window.title('Current Location of the ISS')
241 ISS = turtle.Turtle()
242 ISS.speed(0)
243 ISS.color('orange')
244 ISS.penup()
245 ISS.hideturtle()
246 ISS.goto(0, 75)
247 ISS.right(90)
248 ISS.write('Tracking the ISS',
249 align='center', font=('Arial', 15, 'bold'))
250
251 data = show()
252 if len(data) == 0:
253 ISS.goto(0, 65)
254 ISS.write('Database is Empty!', align='center',
255 font=('Arial', 12, 'normal'))
256 else:
257 ISS.goto(0, 65)
258 ISS.write('Press R to reset Database', align='center',
259 font=('Arial', 12, 'normal'))
260 for each in data:
261 timestamp = each[0]
262 latitude = each[1]
263 longitude = each[2]
264 place = each[3]
265 print(f'Timestamp : {timestamp}')
266 print(f'latitude : {latitude}')
267 print(f'longitude : {longitude}')
268 print(f'place : {place}')
269 print(f'--------------------')
270 ISS.goto(longitude, latitude)
271 ISS.dot(6)
272 ISS.pendown()
273 ISS.penup()
274 ISS.goto(0, -85)
275 ISS.color('red')
276 ISS.write('Press m to go back to the main menu',
277 align='center', font=('Arial', 10, 'normal'))
278
279 window.listen()
280 window.onkey(delete, 'r')
281 window.onkey(menu, 'm')
282
283
284def astronauts_in_space():
285 url = 'http://api.open-notify.org/astros.json'
286 # Get request
287 # Post request
288
289 response = requests.get(url)
290
291 data = response.json()
292 # data -> Dictionary -> {}
293
294 people = data['people']
295 # people - > list
296
297 astronauts_name = []
298 for person in people:
299 # print(person['name'])
300 astronauts_name.append(person['name'])
301
302 # for name in astronauts_name:
303 # print(name)
304 # [print(name) for name in astronauts_name]
305
306 window.clear()
307 window.title('Astronauts in ISS')
308 window.bgpic('stars.gif')
309
310 ISS = turtle.Turtle()
311 ISS.speed(0)
312 ISS.color('white')
313 ISS.penup()
314 ISS.hideturtle()
315 ISS.goto(-100, 65)
316
317 ISS.color('orange')
318 ISS.write('The astronauts in the ISS are: ',
319 align='left', font=('Arial', 20, 'bold'))
320
321 ISS.color('white')
322 ISS.right(90)
323 ISS.forward(45)
324
325 for name in astronauts_name:
326 ISS.write(name,
327 align='left', font=('Arial', 20, 'normal'))
328 ISS.forward(15)
329
330 ISS.goto(0, -80)
331 ISS.color('red')
332 ISS.write('Press m to go back to the main menu',
333 align='center', font=('Arial', 15, 'normal'))
334
335 window.listen()
336
337 window.onkey(menu, 'm')
338
339
340
341
342window = turtle.Screen()
343window.setup(720, 360)
344window.setworldcoordinates(-180, -90, 180, 90)
345
346menu()
347
348while True:
349 window.update()
350