· 4 years ago · Aug 07, 2021, 09:36 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 place = 'Ocean'
126
127 ISS.color('red')
128 ISS.goto(longitude, latitude)
129 ISS.dot(6)
130
131 # insert into database
132 insert(timestamp, latitude, longitude, place)
133
134 # read
135 print(read())
136
137 # delete
138 # delete()
139
140 display_message()
141 window.listen()
142 window.onkey(main_menu, 'm')
143
144
145def astronauts():
146 window.clearscreen()
147 window.title('Astronauts in ISS')
148 window.bgpic('stars.gif')
149
150 ISS = turtle.Turtle()
151 ISS.penup()
152 ISS.hideturtle()
153 ISS.color('orange')
154
155 ISS.sety(65)
156 ISS.write('Astronauts in ISS', align='center',
157 font=('Courier', 24, 'bold'))
158 ISS.right(90)
159
160 # API Calling
161 url = 'http://api.open-notify.org/astros.json'
162 response = requests.get(url)
163 print(response)
164 data = response.json()
165 # print(json.dumps(data, indent=4))
166 names = []
167 for i in data['people']:
168 if i['craft'] == 'ISS':
169 # print(i['name'])
170 names.append(i['name'])
171 ISS.color('white')
172 ISS.goto(-90, 45)
173 for name in names:
174 ISS.write(name, align='left', font=('Calibri', 20, 'bold'))
175 ISS.forward(20)
176
177 display_message()
178 window.listen()
179 window.onkey(main_menu, 'm')
180
181
182window = turtle.Screen()
183window.setup(720, 360)
184window.setworldcoordinates(-180, -90, 180, 90)
185main_menu()
186
187while True:
188 window.update()
189