#Programmer: James Goudy #Date: July 25, 2019 import urllib.request, json import requests import csv """ In order to run this program you need a api key. You can acquire one for free at the website below. https://www.nps.gov/subjects/digital/nps-data-api.htm https://www.nps.gov/subjects/developer/api-documentation.htm https://www.nps.gov/subjects/developer/get-started.htm Helpful websites https://www.nps.gov/subjects/developer/api-documentation.htm#/visitorcenters/getVisitorCenters https://nps-vsp.warnercnr.colostate.edu/showallparkunits.php https://github.com/nationalparkservice/nps-api-samples https://www.nps.gov/subjects/developer/guides.htm#CP_JUMP_5617008 """ def connect_to_nps(park): """ Connect to the National Park Service API""" # Configure API request - this is documented on the NPS site #Use the website to generate the url - put your parkcode and api #where appropriate park = park yourAPIkey = "ENTER YOU KEY HERE" endpoint = "https://developer.nps.gov/api/v1/visitorcenters?parkCode="+ park +"&stateCode=&fields=name&fields=description&api_key=" + yourAPIkey HEADERS = {"Authorization":yourAPIkey} req = urllib.request.Request(endpoint,headers=HEADERS) # Execute request and parse response response = urllib.request.urlopen(req).read() data = json.loads(response.decode('utf-8')) #--------------------------------------------------------------- #print(data) #print(data["data"][0]["title"]) #unpack data1 = data["data"] return data1 def displaydata(data,parkname): """ Display the data retreieved by the Natioan Park """ label = parkname + " " print("\n------ " + label + "-------\n") # Check to see if there is a visitor center if not data: print("Location does not have a visitor center") return; for x in data: print(x.get('name')) print() print("Description - ",x.get('description')) print() print("Directions - ",x.get('directionsInfo')) print() print("Website - ",x.get('url')) print() print("\n---------------\n") def csv_to_dict(file): """ Read CSV to object then turn it into a dictionary """ newDict = {} #note that input file acts like a dictionary but it is not #it is an object that behaves like a dictionary input_file = csv.DictReader(open(file)) #add items to the dictionary for row in input_file: newDict.update({row['Abbreviation'] : row['ParkUnitName']}) return newDict def show_park_codes(parkdict): """ Show the national park codes """ print("This is a listing of the Park Codes") print("PARK CODE\tNATIONAL PARK") print("--------- -------------") for key, value in parkdict.items(): print(key + "\t\t" + value) def main(): #Read in park codes file = 'NPCodes.csv' NPCodeDict = csv_to_dict(file) answer = "yes" while (answer == "yes") or (answer == "y"): print("National Park Vistor Center Information") show = input('Show park codes? (y / n) ').lower() if (show == "yes") or (show == "y"): show_park_codes(NPCodeDict) print("Example Park Codes") print("Glacier - glac, Grand Teton - grte, Yellowstone - yell") parkcode = input("Enter Park Code ") parkcode = parkcode.lower() parkname = NPCodeDict.get(parkcode.upper(),"Not Found") thedata = connect_to_nps(parkcode) displaydata(thedata,parkname) answer = input("Would you like to continue? y / n ") main()