-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_code_recreation_api.py
141 lines (107 loc) · 3.79 KB
/
python_code_recreation_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""python_code_recreation_api.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/10o2mnQ3KY1xi8cRtYIPX1LKtfmAs3tI3
"""
# establish libraries
import requests
import json
import pandas as pd
import gspread
# Set your API key
users_api_key = input("enter the api key: ")
#function to use the facilities endpoint to find the campground ID by name
def get_campground_id(api_key, campground_name):
base_url = "https://ridb.recreation.gov/api/v1"
endpoint = "/facilities"
url = base_url + endpoint
headers = {
"Accept": "application/json",
"apikey": api_key
}
params = {
"query": campground_name,
"limit": 1
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
print(f"http response code: {response.status_code}")
campgrounds_info = response.json()
if "RECDATA" in campgrounds_info and len(campgrounds_info["RECDATA"]) > 0:
campground_id = campgrounds_info["RECDATA"][0]["FacilityID"]
return campground_id
else:
print("Campground not found.")
return None
except requests.exceptions.RequestException as e:
print("Error occurred:", e)
return None
#enter a campground name
campground_name = input("enter a campground name: ")
# run the get_campground_id function to get the campground ID
campground_id_found = get_campground_id(users_api_key, campground_name)
#conditional statement to print the campground id
if campground_id_found:
print("Hey, you found the Campground ID:", campground_id_found)
else:
print("Failed to pull the campground ID.")
#function to use the facilities endpoint for details on campsites
def get_available_campsites(api_key, campground_id, start_date, end_date):
base_url = "https://ridb.recreation.gov/api/v1"
endpoint = f"/facilities/{campground_id}/campsites"
url = base_url + endpoint
headers = {
"Accept": "application/json",
"apikey": api_key
}
params = {
"start_date": start_date,
"end_date": end_date,
"current": "true"
#"limit": 10 # Change the limit as needed
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
print(f"http response code: {response.status_code}")
campsites_info = response.json()
return campsites_info
except requests.exceptions.RequestException as e:
print("Error occurred:", e)
return None
#set date parameters
START_DATE = "2023-07-01"
END_DATE = "2023-07-07"
# use the function
campsites_info = get_available_campsites(users_api_key, campground_id_found, START_DATE, END_DATE)
campsites_data = campsites_info["RECDATA"]
#convert results to a pandas dataframe
campsites_df = pd.DataFrame(campsites_data)
#clean up the data
c2 = campsites_df.drop(["ATTRIBUTES", "ENTITYMEDIA", "PERMITTEDEQUIPMENT"], axis=1)
"""Exporting to Google Sheets"""
#authenticate to google drive
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
#create a google sheet
spreadsheet = gc.create('Acadia Campsite Data')
#get the sheet
sheet = spreadsheet.get_worksheet(0)
# Convert the DataFrame to a list
data_values = [c2.columns.tolist()] + c2.values.tolist()
# fill google sheet with data
sheet.update(data_values)
#list of spreadsheets in google drive
spreadsheet_list = gc.list_spreadsheet_files()
print(spreadsheet_list)
#use the spreadsheet ID
spreadsheet2 = gc.open_by_key("XXX")
#add a new sheet
new_sheet = spreadsheet2.add_worksheet(title="Blackwoods", rows=100, cols=13)
new_sheet.update(data_values)