Learning Goal: I’m working on a website development discussion question and need an explanation and answer to help me learn.
Working with JSON /Dictionaries and Application Programming interfaces
Complete the Python program as defined in the attached file: 3900 Activity 5 Working with API’s and JSON -sv.pdf
Download 3900 Activity 5 Working with API’s and JSON -sv.pdf
Links to an external site.
JSON Examples:
Sample Python Example: jsonLocationExample_APIkeyNeeded.pyDownload
jsonLocationExample_APIkeyNeeded.py#https://ipstack.com/
#Simple RESTful API request example
#Request a free API Key at this link: https://ipstack.com/product
#Paste your key at line 14 below
import requests
print(“ISQA 3900 Location API”)
print()
try:
api_start = ‘http://api.ipstack.com/174.74.113.227?access_key=’
api_key = ‘yourAPIkeyHere’
url = api_start + api_key
json_data = requests.get(url).json()
print(json_data)
IP = json_data[‘ip’]
print(“IP = ” + IP)
print(“Country = ” + json_data[‘country_name’])
print(“State = ” + json_data[‘region_name’])
print(“City = ” + json_data[‘city’])
except Exception:
print(“Unable to get json data”)
Another Example – Dog information – no API key needed: DogAPI.pyDownload DogAPI.py#https://ipstack.com/
#Simple RESTful API request example
#Request a free API Key at this link: https://ipstack.com/product
#Paste your key at line 14 below
import requests
print(“ISQA 3900 Location API”)
print()
try:
api_start = ‘http://api.ipstack.com/174.74.113.227?access_key=’
api_key = ‘yourAPIkeyHere’
url = api_start + api_key
json_data = requests.get(url).json()
print(json_data)
IP = json_data[‘ip’]
print(“IP = ” + IP)
print(“Country = ” + json_data[‘country_name’])
print(“State = ” + json_data[‘region_name’])
print(“City = ” + json_data[‘city’])
except Exception:
print(“Unable to get json data”)
NASA Space Station Tracking example: ISSTrackingProgram_Map.pyDownload ISSTrackingProgram_Map.py
# https://www.geeksforgeeks.org/how-to-track-iss-int…
# json convert the python dictionary
# above into a json
from sys import exit
import json
import turtle
import sys
# urllib.request fetch URLs using
# a variety of different protocols
import urllib.request
import time
# webbrowser provides a high-level interface
# to allow displaying Web-based documents
# to users
import webbrowser
# geocoder takes the data and locate these
# locations in the map
import geocoder
url = “http://api.open-notify.org/astros.json”
response = urllib.request.urlopen(url)
result = json.loads(response.read())
file = open(“iss.txt”, “w”)
file.write(“There are currently ” +
# prints number of astronauts
str(result[“number”]) + ” astronauts on the ISS: nn”)
people = result[“people”]
# prints names of crew
for p in people:
file.write(p[‘name’] + ” – on board” + “n”)
# print long and lat of space station
g = geocoder.ip(‘me’)
file.write(“nYour current lat / long is: ” + str(g.latlng))
file.close()
webbrowser.open(“iss.txt”)
# Setup the world map in turtle module
screen = turtle.Screen()
screen.setup(1280, 720)
screen.setworldcoordinates(-180, -90, 180, 90)
# load the world map image
screen.bgpic(“images/map.gif”)
screen.register_shape(“imagesiss.gif”)
iss = turtle.Turtle()
iss.shape(“imagesiss.gif”)
iss.setheading(45)
iss.penup()
try:
while True:
# load the current status of the ISS in real-time – updating position as it moves
url = “http://api.open-notify.org/iss-now.json”
response = urllib.request.urlopen(url)
result = json.loads(response.read())
# Extract the ISS location
location = result[“iss_position”]
lat = location[‘latitude’]
lon = location[‘longitude’]
# Ouput lon and lat to the terminal
lat = float(lat)
lon = float(lon)
print(“nLatitude: ” + str(lat))
print(“nLongitude: ” + str(lon))
# Update the ISS location on the map
iss.goto(lon, lat)
# Refresh each 5 seconds
time.sleep(2)
except:
print(“Ending program”)
sys.exit()
Rubric
Activity 5 Rubric – JSON, API, Output files
Criteria | Ratings | Pts | |||
---|---|---|---|---|---|
This criterion is linked to a Learning OutcomeProgram Functionality |
|
16 pts | |||
This criterion is linked to a Learning OutcomeException Handling |
|
5 pts | |||
This criterion is linked to a Learning OutcomeOutput saved to text fileWrite the valid outputs to a text file. The user should be prompted to enter the output filename. |
|
6 pts | |||
This criterion is linked to a Learning OutcomeGood programming practice followedDescriptive variable and function names; Simple comments including the following REQUIRED program file header at the top of the Python file: Your name; The course name and date; A short description of the code in the file (i.e. purpose of the program) |
|
3 pts | |||
This criterion is linked to a Learning OutcomeLate PenaltyIf applicable |
|
0 pts | |||
Total Points: 30 |