-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (31 loc) · 1.18 KB
/
app.py
File metadata and controls
45 lines (31 loc) · 1.18 KB
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
from flask import Flask, request, render_template
import requests
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/search", methods=["POST"])
def search():
# Get the search query
query = request.form["q"]
# Add proper headers for Nominatim API
headers = {
'User-Agent': 'Flask World Clock App (your-email@example.com)'
}
# Pass the search query to the Nominatim API to get a location
location = requests.get(
"https://nominatim.openstreetmap.org/search",
params={"q": query, "format": "json", "limit": "1"},
headers=headers
).json()
# If a location is found, pass the coordinate to the Time API to get the current time
if location:
coordinate = [location[0]["lat"], location[0]["lon"]]
time = requests.get(
"https://timeapi.io/api/Time/current/coordinate",
{"latitude": coordinate[0], "longitude": coordinate[1]},
)
return render_template("success.html", location=location[0], time=time.json())
# If a location is NOT found, return the error page
else:
return render_template("fail.html")