-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelperfunctions.py
156 lines (108 loc) · 4.28 KB
/
helperfunctions.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
import requests
import os
from model import (User, Trail, User_Trail, Trip, Trip_User,
Trip_Trail, db, connect_to_db)
MAPS_GEOCODING_KEY = os.environ['MAPS_GEOCODING_KEY']
HIKING_PROJECT_KEY = os.environ['HIKING_PROJECT_KEY']
def call_geocoding_api(search_terms):
"""Query Google Maps Geocoding API to convert search terms to
long/lat coordinates"""
api_url = "https://maps.googleapis.com/maps/api/geocode/json"
payload = {
"address": search_terms,
"key": MAPS_GEOCODING_KEY
}
r = requests.get(api_url, params=payload)
response = r.json()
# Check to see if search was valid
if response.get("results"):
lat_long = response["results"][0]["geometry"]["location"]
return lat_long
# Return error message if message invalid
else:
return "Invalid search terms"
def call_hiking_project_api(lat_long):
"""Query Hiking Project API to retrieve trail list given
lat/long coordinates"""
hiking_api_url = "https://www.hikingproject.com/data/get-trails"
payload = {
"lat": lat_long["lat"],
"lon": lat_long["lng"],
"maxResults": 30,
"key": HIKING_PROJECT_KEY
}
r = requests.get(hiking_api_url, params=payload)
response = r.json()
return response
def call_hiking_project_api_trail_id(trail_id):
"""Query Hiking Project API to retrieve info about one trail"""
hiking_api_url = "https://www.hikingproject.com/data/get-trails-by-id"
payload = {
"ids": trail_id,
"key": HIKING_PROJECT_KEY
}
r = requests.get(hiking_api_url, params=payload)
response = r.json()
return response
def convert_trail_difficulty(color_difficulty):
"""Convert Hiking Project API's difficulty rating from color to words"""
color_dict = {
"green": "Easy",
"greenBlue": "Moderately Easy",
"blue": "Intermediate",
"blueBlack": "Somewhat Difficult",
"black": "Difficult",
"dblack": "Extremely Difficult",
"missing": "Information Missing",
}
return color_dict.get(color_difficulty)
def seed_trails_into_db(api_response):
"""Take Hiking Project API response and seed trail data into database if
trail does not already exist"""
for trail in api_response["trails"]:
trail_id = trail["id"]
trail_name = trail["name"]
length = trail["length"]
difficulty = convert_trail_difficulty(trail["difficulty"])
img_thumb_url = trail["imgSmall"]
img_lg_url = trail["imgSmallMed"]
long = trail["longitude"]
lat = trail["latitude"]
location = trail["location"].split(",")
city = location[0]
state = location[1][1:]
description = trail["summary"]
ascent = trail["ascent"]
descent = trail["descent"]
high_altitude = trail["high"]
low_altitude = trail["low"]
if not Trail.query.filter_by(trail_id=trail_id).all():
new_trail = Trail(trail_id=trail_id, trail_name=trail_name,
length=length, difficulty=difficulty,
img_thumb_url=img_thumb_url,
img_lg_url=img_lg_url,
long=long, lat=lat, city=city, state=state,
description=description, ascent=ascent,
descent=descent, high_altitude=high_altitude,
low_altitude=low_altitude)
db.session.add(new_trail)
db.session.commit()
def delete_trip_users(trip_id):
"""Deletes all Trip_User instances associated with a given trip_id"""
trip_users = Trip_User.query.filter_by(trip_id=trip_id).all()
for tu in trip_users:
db.session.delete(tu)
db.session.commit()
def delete_trip_trails(trip_id):
"""Deletes all Trip_Trail instances associated with a given trip_id"""
trip_trails = Trip_Trail.query.filter_by(trip_id=trip_id).all()
for tt in trip_trails:
db.session.delete(tt)
db.session.commit()
def delete_trip(trip_id):
"""Deletes all Trip instances associated with a given trip_id"""
trip = Trip.query.get(trip_id)
flash_msg = f"{trip.trip_name} has been deleted"
db.session.delete(trip)
db.session.commit()
return flash_msg