Python Forum
Using the NPS API for Project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the NPS API for Project
#1
Hi, I am working on a project for school where I need to use the National Parks Service API Key (specifically to return campgrounds). I want to write my code so that when a user inputs a specific city within the United States, the program will output the names of the campgrounds located in that city.

I do not know how to make a for loop to do this or how I should go about coding this, any help would be greatly appreciated I am new to Python!

API Key: https://www.nps.gov/subjects/developer/a...ampgrounds
Reply
#2
There are packages for that. I haven't used any of these, so you'll have to try them to see if they can be used in your project.
List is here: https://pypi.org/search/?q=National+%2B+...ce+%2B+API
Reply
#3
(Dec-05-2020, 03:42 AM)Larz60+ Wrote: There are packages for that.
given that it's a homework question it's pretty sure they are expected to make the requests (incl. authentication) themselves and not use third-party package. I hope at least requests is allowed

@belle2008, start by register at the NPS website and obtain API key. Follow the documentation and examples to get understanding of the API - e.g. endpoints, required params when make request at some endpoint, what response you will get, rate limits, etc. There are examples. Make sure you can make a request (with authentication), parse response and so. Then you can think of obtaining user input and make a request based on that input.
If allowed I would recommend using requests package. Of course if allowed to use other third-party package as suggested by @Larz60+ - feel free to do so.
Larz60+ likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Yes I have a registered key and all my params and can also import requests. The issue is turning the user input I have obtained into a request.



I hope this can help if you can see my error, that would be greatly appreciated!


(Dec-05-2020, 07:32 AM)buran Wrote:
(Dec-05-2020, 03:42 AM)Larz60+ Wrote: There are packages for that.
given that it's a homework question it's pretty sure they are expected to make the requests (incl. authentication) themselves and not use third-party package. I hope at least requests is allowed

@belle2008, start by register at the NPS website and obtain API key. Follow the documentation and examples to get understanding of the API - e.g. endpoints, required params when make request at some endpoint, what response you will get, rate limits, etc. There are examples. Make sure you can make a request (with authentication), parse response and so. Then you can think of obtaining user input and make a request based on that input.
If allowed I would recommend using requests package. Of course if allowed to use other third-party package as suggested by @Larz60+ - feel free to do so.
buran write Dec-05-2020, 05:07 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#5
data is complex JSON object

Here is example response from the docs:
Output:
[ { "total": 509, "data": [ [ { "accessibility": [ { "wheelchairaccess": "Some restrooms are wheelchair accessible. The campground is mainly accessed by transit bus, around half of which are wheelchair accessible.", "internetinfo": "", "rvallowed": 0, "cellphoneinfo": "", "firestovepolicy": "Open fires are prohibited at Wonder Lake Campground. Bring a camping stove of some kind to cook your meals.", "rvmaxlength": 0, "additionalinfo": "", "trailermaxlength": 0, "adainfo": "Paths between campsites vary from compact gravel to muddy, rocky surfaces. The campground is laid out with some sites on a small hill above the level of the main campground road, while others are level with or slightly downhill from the level of the road.", "rvinfo": "This is a tent-only campsiteā€”RVs and trailers are prohibited at Wonder Lake.", "accessroads": [ "Unpaved Roads - 4WD/High clearance required" ], "trailerallowed": 0, "classifications": [ "Limited Development Campground" ] } ], "addresses": [ { "postalCode": 99755, "city": "Denali Park", "stateCode": "AK", "line1": "PO Box 9", "line2": "", "line3": "", "type": "Mailing" }, { "postalCode": 99755, "city": "Denali Park", "stateCode": "AK", "line1": "Mile 85, Denali Park Road", "line2": "", "line3": "", "type": "Physical" } ], "amenities": [ { "trashrecyclingcollection": "Yes - seasonal", "toilets": [ "Vault Toilets - year round" ], "internetconnectivity": false, "showers": [ "None" ], "cellphonereception": false, "laundry": false, "amphitheater": "", "dumpstation": false, "campstore": false, "stafforvolunteerhostonsite": "Yes - seasonal", "potablewater": [ "Yes - seasonal" ], "iceavailableforsale": false, "firewoodforsale": false, "ampitheater": "Yes - year round", "foodstoragelockers": "Yes - year round" } ], "campsites": [ { "other": 0, "group": 0, "horse": 0, "totalsites": 28, "tentonly": 28, "electricalhookups": 0, "rvonly": 0, "walkboatto": 0 } ], "contacts": [ { "phoneNumbers": [ { "phoneNumber": "9076839532", "description": "", "extension": "", "type": "Voice" } ] }, { "emailAddresses": [ { "emailAddress": "[email protected]", "description": "" } ] } ], "description": "Wonder Lake Campground is at mile 85 on the Denali Park Road. It offers 28 sites, which are all tent-only.\n\nIt is the closest campground to Denali. Though separated by nearly 26 miles, the sheer size of the mountain makes it loom impressively over Wonder Lake Campground. Cloudy skies can obscure views, and mosquitoes can be fierce, but Wonder Lake on a clear day offers views that will last a lifetime. Bear-proof food lockers are available throughout the campground.", "directionsoverview": "Visitors may not drive to this campground. It is only accessible for campers via specialized park buses. Be sure to book a camper bus ticket in addition to paying your campground fees.", "directionsUrl": "http://www.nps.gov/dena/planyourvisit/campground-wonder.htm", "fees": null, "id": 29, "images": null, "latLong": "{lat:63.4574408, lng:-150.8612003}", "latitude": "63.4574408", "longitude": "-150.8612003", "name": "Wonder Lake", "operatingHours": null, "parkCode": "dena", "regulationsoverview": null, "regulationsurl": "http://www.nps.gov/dena/learn/management/lawsandpolicies.htm", "reservationsdescription": "", "reservationsitesfirstcome": "", "reservationssitesreservable": "", "reservationsurl": "", "weatheroverview": "Wonder Lake tends to be cooler and wetter than the park entrance. Expect lows in the upper 30s or 40s each night. Daytime highs are typically in the upper 50s or 60s, though occasional sunny days can result in temps near 80. Mosquitoes are particularly bad most of the summer - bring a headnet!" }, { "accessibility": [ { "wheelchairAccess": "Restrooms are wheelchair accessible.", "internetinfo": "", "rvallowed": 0, "cellphoneinfo": "", "firestovepolicy": "", "rvmaxlength": 0, "additionalinfo": "", "trailermaxlength": 0, "adainfo": "Surfaces in the campground are largely compacted gravel or native soils with roots and rocks. The path from the campground to the bus stop on the Denali Park Road is compacted gravel. Restrooms are wheelchair accessible.", "rvinfo": "This is a tent-only campground.", "accessroads": [ "Unpaved Roads - 4WD/High clearance required" ], "trailerallowed": 0, "classifications": [ "Primitive Camping Areas" ] } ], "addresses": [ { "postalCode": 99755, "city": "Denali Park", "stateCode": "AK", "line1": "PO Box 9", "line2": "", "line3": "", "type": "Mailing" }, { "postalCode": 99755, "city": "Denali Park", "stateCode": "AK", "line1": "Mile 85, Denali Park Road", "line2": "", "line3": "", "type": "Physical" } ], "amenities": [ { "trashrecyclingcollection": "Yes - seasonal", "toilets": [ "Vault Toilets - year round" ], "internetconnectivity": false, "showers": [ "None" ], "cellphonereception": false, "laundry": false, "amphitheater": "", "dumpstation": false, "campstore": false, "stafforvolunteerhostonsite": "Yes - seasonal", "potablewater": [ "Yes - seasonal" ], "iceavailableforsale": false, "firewoodforsale": false, "ampitheater": "Yes - year round", "foodstoragelockers": "Yes - year round" } ], "campsites": [ { "other": 0, "group": 0, "horse": 0, "totalsites": 7, "tentonly": 7, "electricalhookups": 0, "rvonly": 0, "walkboatto": 0 } ], "contacts": [ { "phoneNumbers": [ { "phoneNumber": "9076839532", "description": "", "extension": "", "type": "Voice" } ] }, { "emailAddresses": [ { "emailAddress": "[email protected]", "description": "" } ] } ], "description": "Igloo Creek Campground is at mile 35 on the Park Road. It offers 7 sites, making it the smallest in Denali (along with Sanctuary River Campground, which also has 7 sites).\n\nIgloo is tent-only - you will not find any RVs or other vehicles in this campground. It is accessible by camper bus. The bus stop is a short walk from the camp sites - less than 50 yards (meters).\n\nAdvance reservations are not possible at this campground. Visitors may only book a site upon arriving in the park.", "directionsoverview": "This campground can only be reached by bus. Be sure to book a camper bus ticket when purchasing a stay at Igloo Creek Campground.", "directionsUrl": "http://www.nps.gov/dena/planyourvisit/campground-igloo.htm", "fees": null, "id": 34, "images": null, "latLong": "{lat:63.6117723, lng:-149.570902}", "name": "Igloo Creek", "operatingHours": null, "parkCode": "dena", "regulationsoverview": null, "regulationsurl": "http://www.nps.gov/dena/learn/management/lawsandpolicies.htm", "reservationsdescription": "", "reservationsitesfirstcome": "", "reservationssitesreservable": "", "reservationsurl": "", "weatheroverview": "We recommend you bring a sleeping bag rated to 20 degrees F. Expect rain, though sunny days do occur periodically throughout the summer. Highs are usually in the 60s, though sometimes can reach the low 80s. Snow is possible any month of the year, however." } ] ], "limit": "50", "start": "0" } ]
as you can see data['data'] is list with one element - list of dicts (don't know if it can have multiple lists - you need to check this. the model in the docs suggest list of dicts, not list of list of dicts, so it may be just an error in the example response - check the actual data you get). each dict representing different campground.
You need to iterate over data['data']

for campground in data['data'][0]: # if there is no second nested list - no need of [0]
    # here you need to access respective key
    # note that there are multiple addressess per campground
    # note that you need to check the city against your user_city variable, 
    # and not against "user_city" string like you do now
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Also, looking at the docs - you can query on ParkCode or StateCode, I don't see option to query by city. I guess you can query by state code and then parse the response to check for city name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Gotcha, that was also the issue I sorta saw. So in my query string for my API, how should I update it? This is something I am confused on


(Dec-05-2020, 05:33 PM)buran Wrote: Also, looking at the docs - you can query on ParkCode or StateCode, I don't see option to query by city. I guess you can query by state code and then parse the response to check for city name
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020