· 4 years ago · Aug 14, 2021, 10:04 AM
1import turtle
2import json
3import requests
4import sqlite3
5from datetime import datetime
6
7'''
81. Importing libraries
92. Creating the window
104 pages in total
11- Astronauts
12- Current Location
13- Tracking
14- Exit
15All 4 pages will have a separate function
16- Heading
17- Background
18- Title
19- Contents
20'''
21connection = sqlite3.connect('ISSData.db')
22cursor = connection.cursor()
23
24# command to create a table
25# to run any command, we use the cursor
26command = 'create table if not exists TrackData(time text, latitude float, longitude float, place text)'
27cursor.execute(command)
28
29# Write
30
31
32def insert(time, latitude, longitude, place):
33 with connection:
34 cursor.execute('insert into TrackData values(?,?,?,?)',
35 (time, latitude, longitude, place))
36
37# Delete
38
39
40def delete():
41 with connection:
42 cursor.execute('delete from TrackData')
43 tracking()
44
45# Read
46
47
48def read():
49 # * means everything
50 cursor.execute('select * from TrackData')
51 return cursor.fetchall()
52
53
54def display_message():
55 message = turtle.Turtle()
56 message.penup()
57 message.hideturtle()
58 message.color('red')
59 message.sety(-85)
60 message.write('Press M for Main menu', align='center',
61 font=('Courier', 15, 'bold'))
62
63
64def main_menu():
65 window.clearscreen()
66 window.title('ISS App')
67 window.bgpic('stars.gif')
68
69 menu = turtle.Turtle()
70 menu.penup()
71 menu.hideturtle()
72 menu.color('orange')
73
74 menu.sety(65)
75 menu.write('~~~Explore Space with Anish~~~', align='center',
76 font=('Courier', 24, 'bold'))
77 menu.right(90)
78 menu.goto(-120, 20)
79 menu.color('white')
80
81 menu.write('a. Astronauts in Space', align='left',
82 font=('Courier', 20, 'bold'))
83 menu.forward(25)
84 menu.write('b. Current Location', align='left',
85 font=('Courier', 20, 'bold'))
86 menu.forward(25)
87 menu.write('c. Tracking the ISS', align='left',
88 font=('Courier', 20, 'bold'))
89 menu.forward(25)
90 menu.write('d. Exit', align='left',
91 font=('Courier', 20, 'bold'))
92
93 window.listen()
94 window.onkey(astronauts, 'a')
95 window.onkey(location, 'b')
96 window.onkey(tracking, 'c')
97 window.onkey(exitfn, 'd')
98
99def exitfn():
100 window.clearscreen()
101 window.title('ISS App')
102 window.bgpic('stars.gif')
103
104 var = turtle.Turtle()
105 var.penup()
106 var.hideturtle()
107 var.color('orange')
108
109
110 var.write('Thank You', align='center',
111 font=('Courier', 30, 'bold'))
112 window.exitonclick()
113
114def tracking():
115 window.clearscreen()
116 window.title('Tracking the ISS')
117 window.bgpic('Map.gif')
118
119 ISS = turtle.Turtle()
120 ISS.penup()
121 ISS.hideturtle()
122 ISS.color('orange')
123
124 ISS.sety(84)
125 ISS.write('Tracking the ISS', align='center',
126 font=('Courier', 15, 'bold'))
127 ISS.right(90)
128
129 # tracking code
130 data = read()
131
132 if len(data) == 0:
133 ISS.forward(20)
134 ISS.write('Database is Empty!', align='center',
135 font=('Arial', 12, 'bold'))
136 else:
137 ISS.forward(10)
138 ISS.write('Press R to reset', align='center',
139 font=('Arial', 12, 'bold'))
140
141 for single_element in data:
142 time = single_element[0]
143 latitude = single_element[1]
144 longitude = single_element[2]
145 place = single_element[3]
146
147 ISS.goto(longitude, latitude)
148 ISS.dot(6)
149 ISS.pendown()
150
151 display_message()
152 window.listen()
153 window.onkey(delete, 'r')
154 window.onkey(main_menu, 'm')
155
156
157def location():
158 window.clearscreen()
159 window.title('Current Location of ISS')
160 window.bgpic('Map.gif')
161
162 ISS = turtle.Turtle()
163 ISS.penup()
164 ISS.hideturtle()
165 ISS.color('orange')
166
167 ISS.sety(84)
168 ISS.write('Current Location of the ISS', align='center',
169 font=('Courier', 15, 'bold'))
170 ISS.right(90)
171
172 # API Calling
173 url = 'http://api.open-notify.org/iss-now.json'
174 response = requests.get(url)
175 data = response.json()
176 print(json.dumps(data, indent=4))
177
178 latitude = data['iss_position']['latitude']
179 longitude = data['iss_position']['longitude']
180 timestamp = datetime.fromtimestamp(data['timestamp'])
181
182 latitude = float(latitude)
183 longitude = float(longitude)
184
185 key = 'pk.7510fb98384ba4f451338c713b61ef9f'
186 place = 'Ocean'
187
188 url = f'https://us1.locationiq.com/v1/reverse.php?key={key}'
189 parameters = {'lat': latitude, 'lon': longitude, 'format': 'json'}
190
191 # API calling
192 response = requests.get(url, params=parameters)
193 data = response.json()
194 if response.status_code == 200:
195 place = data['address']['country']
196
197 ISS.color('red')
198 ISS.goto(longitude, latitude)
199 ISS.dot(6)
200 ISS.write(place, align='center', font=('Arial', 10, 'normal'))
201
202 # insert into database
203 insert(timestamp, latitude, longitude, place)
204
205 # read
206 print(read())
207
208 # delete
209 # delete()
210
211 display_message()
212 window.listen()
213 window.onkey(main_menu, 'm')
214
215
216def astronauts():
217 window.clearscreen()
218 window.title('Astronauts in ISS')
219 window.bgpic('stars.gif')
220
221 ISS = turtle.Turtle()
222 ISS.penup()
223 ISS.hideturtle()
224 ISS.color('orange')
225
226 ISS.sety(65)
227 ISS.write('Astronauts in ISS', align='center',
228 font=('Courier', 24, 'bold'))
229 ISS.right(90)
230
231 # API Calling
232 url = 'http://api.open-notify.org/astros.json'
233 response = requests.get(url)
234 print(response)
235 data = response.json()
236 # print(json.dumps(data, indent=4))
237 names = []
238 for i in data['people']:
239 if i['craft'] == 'ISS':
240 # print(i['name'])
241 names.append(i['name'])
242 ISS.color('white')
243 ISS.goto(-90, 45)
244 for name in names:
245 ISS.write(name, align='left', font=('Calibri', 20, 'bold'))
246 ISS.forward(20)
247
248 display_message()
249 window.listen()
250 window.onkey(main_menu, 'm')
251
252
253window = turtle.Screen()
254window.setup(720, 360)
255window.setworldcoordinates(-180, -90, 180, 90)
256main_menu()
257
258while True:
259 window.update()
260