forked from streeter/recreation-gov-campsite-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamping.py
181 lines (146 loc) · 5.12 KB
/
camping.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
import argparse
import json
import logging
import sys
from datetime import datetime, timedelta
import requests
from fake_useragent import UserAgent
LOG = logging.getLogger(__name__)
formatter = logging.Formatter("%(asctime)s - %(process)s - %(levelname)s - %(message)s")
sh = logging.StreamHandler()
sh.setFormatter(formatter)
LOG.addHandler(sh)
BASE_URL = "https://www.recreation.gov"
AVAILABILITY_ENDPOINT = "/api/camps/availability/campground/"
MAIN_PAGE_ENDPOINT = "/api/camps/campgrounds/"
INPUT_DATE_FORMAT = "%Y-%m-%d"
SUCCESS_EMOJI = "🏕"
FAILURE_EMOJI = "❌"
headers = {"User-Agent": UserAgent().random}
def format_date(date_object):
date_formatted = datetime.strftime(date_object, "%Y-%m-%dT00:00:00Z")
return date_formatted
def generate_params(start, end):
params = {"start_date": format_date(start), "end_date": format_date(end)}
return params
def send_request(url, params):
resp = requests.get(url, params=params, headers=headers)
if resp.status_code != 200:
raise RuntimeError(
"failedRequest",
"ERROR, {} code received from {}: {}".format(
resp.status_code, url, resp.text
),
)
return resp.json()
def get_park_information(park_id, params):
url = "{}{}{}".format(BASE_URL, AVAILABILITY_ENDPOINT, park_id)
return send_request(url, params)
def get_name_of_site(park_id):
url = "{}{}{}".format(BASE_URL, MAIN_PAGE_ENDPOINT, park_id)
resp = send_request(url, {})
return resp["campground"]["facility_name"]
def get_num_available_sites(resp, start_date, end_date):
maximum = resp["count"]
num_available = 0
num_days = (end_date - start_date).days
dates = [end_date - timedelta(days=i) for i in range(1, num_days + 1)]
dates = set(format_date(i) for i in dates)
for site in resp["campsites"].values():
available = bool(len(site["availabilities"]))
for date, status in site["availabilities"].items():
if date not in dates:
continue
if status != "Available":
available = False
break
if available:
num_available += 1
LOG.debug(
"Available site {}: {}".format(
num_available, json.dumps(site, indent=1)
)
)
return num_available, maximum
def valid_date(s):
try:
return datetime.strptime(s, INPUT_DATE_FORMAT)
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise argparse.ArgumentTypeError(msg)
def _main(parks, start_date, end_date, machine=False):
out = []
availabilities = False
for park_id in parks:
params = generate_params(start_date, end_date)
LOG.debug("Querying for {} with these params: {}".format(park_id, params))
park_information = get_park_information(park_id, params)
LOG.debug(
"Information for {}: {}".format(
park_id, json.dumps(park_information, indent=1)
)
)
name_of_site = get_name_of_site(park_id)
current, maximum = get_num_available_sites(
park_information, start_date, end_date
)
if current:
emoji = SUCCESS_EMOJI
availabilities = True
else:
emoji = FAILURE_EMOJI
if not machine or (machine and current):
out.append(
"{} {} ({}): {} site(s) available out of {} site(s)".format(
emoji, name_of_site, park_id, current, maximum
)
)
if not machine:
if availabilities:
print(
"There are campsites available from {} to {}!!!".format(
start_date.strftime(INPUT_DATE_FORMAT),
end_date.strftime(INPUT_DATE_FORMAT),
)
)
else:
print("There are no campsites available :(")
if out:
print("\n".join(out))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", "-d", action="store_true", help="Debug log level")
parser.add_argument(
"--start-date", required=True, help="Start date [YYYY-MM-DD]", type=valid_date
)
parser.add_argument(
"--end-date",
required=True,
help="End date [YYYY-MM-DD]. You expect to leave this day, not stay the night.",
type=valid_date,
)
parser.add_argument(
"--machine",
"-m",
action="store_true",
help="Output in a machine readable format, for use with scripts",
)
parser.add_argument(
dest="parks", metavar="park", nargs="+", help="Park ID(s)", type=int
)
parser.add_argument(
"--stdin",
"-",
action="store_true",
help="Read list of park ID(s) from stdin instead",
)
args = parser.parse_args()
if args.debug:
LOG.setLevel(logging.DEBUG)
parks = args.parks or [p.strip() for p in sys.stdin]
try:
_main(parks, args.start_date, args.end_date, machine=args.machine)
except Exception:
print("Something went wrong")
raise