· 4 years ago · Aug 08, 2021, 09:42 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
44# Read
45
46
47def read():
48 # * means everything
49 cursor.execute('select * from TrackData')
50 return cursor.fetchall()
51
52
53def display_message():
54 message = turtle.Turtle()
55 message.penup()
56 message.hideturtle()
57 message.color('red')
58 message.sety(-85)
59 message.write('Press M for Main menu', align='center',
60 font=('Courier', 15, 'bold'))
61
62
63def main_menu():
64 window.clearscreen()
65 window.title('ISS App')
66 window.bgpic('stars.gif')
67
68 menu = turtle.Turtle()
69 menu.penup()
70 menu.hideturtle()
71 menu.color('orange')
72
73 menu.sety(65)
74 menu.write('~~~Explore Space with Anish~~~', align='center',
75 font=('Courier', 24, 'bold'))
76 menu.right(90)
77 menu.goto(-120, 20)
78 menu.color('white')
79
80 menu.write('a. Astronauts in Space', align='left',
81 font=('Courier', 20, 'bold'))
82 menu.forward(25)
83 menu.write('b. Current Location', align='left',
84 font=('Courier', 20, 'bold'))
85 menu.forward(25)
86 menu.write('c. Tracking the ISS', align='left',
87 font=('Courier', 20, 'bold'))
88 menu.forward(25)
89 menu.write('d. Exit', align='left',
90 font=('Courier', 20, 'bold'))
91
92 window.listen()
93 window.onkey(astronauts, 'a')
94 window.onkey(location, 'b')
95
96
97def location():
98 window.clearscreen()
99 window.title('Current Location of ISS')
100 window.bgpic('Map.gif')
101
102 ISS = turtle.Turtle()
103 ISS.penup()
104 ISS.hideturtle()
105 ISS.color('orange')
106
107 ISS.sety(84)
108 ISS.write('Current Location of the ISS', align='center',
109 font=('Courier', 15, 'bold'))
110 ISS.right(90)
111
112 # API Calling
113 url = 'http://api.open-notify.org/iss-now.json'
114 response = requests.get(url)
115 data = response.json()
116 print(json.dumps(data, indent=4))
117
118 latitude = data['iss_position']['latitude']
119 longitude = data['iss_position']['longitude']
120 timestamp = datetime.fromtimestamp(data['timestamp'])
121
122 latitude = float(latitude)
123 longitude = float(longitude)
124
125 key = 'pk.7510fb98384ba4f451338c713b61ef9f'
126 place = 'Ocean'
127
128 url = f'https://us1.locationiq.com/v1/reverse.php?key={key}'
129 parameters = {'lat': latitude, 'lon': longitude, 'format': 'json'}
130
131 # API calling
132 response = requests.get(url, params=parameters)
133 data = response.json()
134 if response.status_code == 200:
135 place = data['address']['country']
136
137 ISS.color('red')
138 ISS.goto(longitude, latitude)
139 ISS.dot(6)
140 ISS.write(place, align='center', font=('Arial', 10, 'normal'))
141
142 # insert into database
143 insert(timestamp, latitude, longitude, place)
144
145 # read
146 print(read())
147
148 # delete
149 # delete()
150
151 display_message()
152 window.listen()
153 window.onkey(main_menu, 'm')
154
155
156def astronauts():
157 window.clearscreen()
158 window.title('Astronauts in ISS')
159 window.bgpic('stars.gif')
160
161 ISS = turtle.Turtle()
162 ISS.penup()
163 ISS.hideturtle()
164 ISS.color('orange')
165
166 ISS.sety(65)
167 ISS.write('Astronauts in ISS', align='center',
168 font=('Courier', 24, 'bold'))
169 ISS.right(90)
170
171 # API Calling
172 url = 'http://api.open-notify.org/astros.json'
173 response = requests.get(url)
174 print(response)
175 data = response.json()
176 # print(json.dumps(data, indent=4))
177 names = []
178 for i in data['people']:
179 if i['craft'] == 'ISS':
180 # print(i['name'])
181 names.append(i['name'])
182 ISS.color('white')
183 ISS.goto(-90, 45)
184 for name in names:
185 ISS.write(name, align='left', font=('Calibri', 20, 'bold'))
186 ISS.forward(20)
187
188 display_message()
189 window.listen()
190 window.onkey(main_menu, 'm')
191
192
193window = turtle.Screen()
194window.setup(720, 360)
195window.setworldcoordinates(-180, -90, 180, 90)
196main_menu()
197
198while True:
199 window.update()
200