-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
26 lines (24 loc) · 769 Bytes
/
context.py
File metadata and controls
26 lines (24 loc) · 769 Bytes
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
import requests
from datetime import datetime
def get_location_from_ip():
"""gets country + city based on ip address"""
try:
response = requests.get("http://ip-api.com/json/")
data = response.json()
if data["status"] == "success":
return data["country"], data["city"]
else:
print("failed to get location")
return None, None
except Exception as e:
print(f"error getting location: {e}")
return None, None
def get_time_of_day():
"""Returns the current time of day as a string."""
current_hour = datetime.now().hour
if 5 <= current_hour < 12:
return "morning"
elif 12 <= current_hour < 18:
return "afternoon"
else:
return "evening"