|
| 1 | +# json convert the python dictionary above into a json |
| 2 | +import json |
| 3 | +import turtle |
| 4 | +# urllib.request fetch URLs using a variety of different protocols |
| 5 | +import urllib.request |
| 6 | +import time |
| 7 | +# webbrowser provides a high-level interface to allow displaying Web-based documents to users |
| 8 | +import webbrowser |
| 9 | +# geocoder takes the data and locate these locations in the map |
| 10 | +import geocoder |
| 11 | + |
| 12 | +url = "http://api.open-notify.org/astros.json" |
| 13 | +response = urllib.request.urlopen(url) |
| 14 | +result = json.loads(response.read()) |
| 15 | +file = open("iss.txt", "w") |
| 16 | +file.write("There are currently " + |
| 17 | + str(result["number"]) + " astronauts on the ISS: \n\n") # prints number of astronauts |
| 18 | +people = result["people"] |
| 19 | +for p in people: |
| 20 | + file.write(p['name'] + " - on board" + "\n") # prints names of crew |
| 21 | +# print long and lat |
| 22 | +g = geocoder.ip('me') |
| 23 | +file.write("\nYour current lat / long is: " + str(g.latlng)) |
| 24 | +file.close() |
| 25 | +webbrowser.open("iss.txt") |
| 26 | + |
| 27 | +# Setup the world map in turtle module |
| 28 | +screen = turtle.Screen() |
| 29 | +screen.setup(1280, 720) |
| 30 | +screen.setworldcoordinates(-180, -90, 180, 90) |
| 31 | + |
| 32 | +# load the world map image |
| 33 | +screen.bgpic("images/map.gif") |
| 34 | +screen.register_shape("images\iss.gif") |
| 35 | +iss = turtle.Turtle() |
| 36 | +iss.shape("images\iss.gif") |
| 37 | +iss.setheading(45) |
| 38 | +iss.penup() |
| 39 | + |
| 40 | +while True: |
| 41 | + # load the current status of the ISS in real-time |
| 42 | + url = "http://api.open-notify.org/iss-now.json" |
| 43 | + response = urllib.request.urlopen(url) |
| 44 | + result = json.loads(response.read()) |
| 45 | + |
| 46 | + # Extract the ISS location |
| 47 | + location = result["iss_position"] |
| 48 | + lat = location['latitude'] |
| 49 | + lon = location['longitude'] |
| 50 | + |
| 51 | + # Ouput lon and lat to the terminal |
| 52 | + lat = float(lat) |
| 53 | + lon = float(lon) |
| 54 | + print("\nLatitude: " + str(lat)) |
| 55 | + print("\nLongitude: " + str(lon)) |
| 56 | + |
| 57 | + # Update the ISS location on the map |
| 58 | + iss.goto(lon, lat) |
| 59 | + |
| 60 | + # Refresh each 5 seconds |
| 61 | + time.sleep(5) |
| 62 | + |
0 commit comments