-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
196 lines (164 loc) · 5.8 KB
/
tools.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import requests
import json
from datetime import datetime, timezone
import pytz
from dotenv import load_dotenv
import os
load_dotenv()
NOTION_DB_ID = os.getenv("NOTION_DB_ID")
NOTION_TOKEN = os.getenv("NOTION_TOKEN")
LOCAL_TIMEZONE = os.getenv("LOCAL_TIMEZONE")
# Helper function: Convert UTC time to Montreal local time
def convert_to_local_time(utc_time):
utc_time = datetime.strptime(utc_time, "%Y-%m-%dT%H:%M:%SZ")
try:
target_tz = pytz.timezone(LOCAL_TIMEZONE)
return utc_time.replace(tzinfo=pytz.utc).astimezone(target_tz)
except pytz.UnknownTimeZoneError:
raise ValueError(f"Invalid timezone: {LOCAL_TIMEZONE}")
def get_weather(lat, lon):
"""
Retrieve weather forecast from The Norwegian Meteorological Institute.
"""
# 1. YR requires HTTP headers including a User-Agent
url = f"https://api.met.no/weatherapi/locationforecast/2.0/complete?lat={lat}&lon={lon}"
headers = {"User-Agent": "PersonalAgentApp/0.01 https://github.com/gvzdv/agent"}
resp = requests.get(url, headers=headers)
data = resp.json()
forecast = process_forecast(data)
return forecast
def process_forecast(weather_json):
"""
Process weather forecast data.
"""
weather = json.dumps(weather_json)
# Load the JSON data
data = json.loads(weather)
# Extract timeseries data
timeseries = data["properties"]["timeseries"]
# Initialize variables for high, low, and current temperature
current_temp = timeseries[0]["data"]["instant"]["details"]["air_temperature"]
high_temp = float("-inf")
low_temp = float("inf")
high_temp_time = None
low_temp_time = None
weather_summary = ""
# Extract weather summary from the first entry
weather_summary = ""
if "next_12_hours" in timeseries[0]["data"]:
weather_summary = timeseries[0]["data"]["next_12_hours"]["summary"][
"symbol_code"
]
# Iterate over the next 12 hours (assume timeseries is ordered by time)
for entry in timeseries[:12]:
time = entry["time"]
temp = entry["data"]["instant"]["details"]["air_temperature"]
if temp > high_temp:
high_temp = temp
high_temp_time = time
if temp < low_temp:
low_temp = temp
low_temp_time = time
# Convert high/low temperature times to local time
high_temp_time_local = convert_to_local_time(high_temp_time).strftime("%H:%M")
low_temp_time_local = convert_to_local_time(low_temp_time).strftime("%H:%M")
# Generate the summary
forecast = (
f"Today's weather:\n"
f"Currently: {current_temp}°C\n"
f"High: {high_temp}°C (at {high_temp_time_local})\n"
f"Low: {low_temp}°C (at {low_temp_time_local})\n"
f"Stuff in the sky: {weather_summary.replace('_', ' ')}"
)
return forecast
def add_diary_entry(entry_text):
"""
Add a diary entry to Notion.
"""
# Notion API endpoint for creating a new page
url = "https://api.notion.com/v1/pages"
# Prepare headers
headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
# 1. Get the current UTC time
now_utc = datetime.now(timezone.utc)
# 2. Convert it to a string in the format "YYYY-MM-DDTHH:MM:SSZ"
now_utc_str = now_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
# 3. Convert that UTC string to local Montreal time
local_now = convert_to_local_time(now_utc_str)
# 4. Format local_now as an ISO8601 string (e.g., "2025-01-16T14:30:00-05:00")
local_now_str = local_now.isoformat()
# Notion will store the local time with the correct offset.
# Construct the request payload.
payload = {
"parent": {
"database_id": NOTION_DB_ID
},
"properties": {
# Title property (often named 'Name' or 'Title')
"Name": {
"title": [
{
"text": {
"content": f"Diary entry: {local_now.strftime('%d/%m')}"
}
}
]
},
# Date property
"Date": {
"date": {
"start": local_now_str
}
}
},
# Add the text as a paragraph block
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {
"content": entry_text
}
}
]
}
}
]
}
# Make the POST request to create the page
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return f"Diary entry saved in Notion, entry preview: {entry_text[:50]}"
except requests.exceptions.RequestException as e:
return f"Error: {str(e)}"
def add_event(summary, start_time, end_time):
"""
Insert event into Google Calendar
"""
# use googleapiclient or direct REST call with credentials
# return success status or event info
return {"status": "Event added", "summary": summary}
def get_events(date):
"""
Retrieve events for a date from Google Calendar
"""
# return list of events
return {"events": ["Event 1 at 10:00", "Event 2 at 15:00"]}
def get_mail():
"""
Retrieve unread mail from Gmail, focusing on importance
"""
# return important unread messages
return {
"unread_important": 2,
"messages": ["Important: Meeting tomorrow", "Important: Check invoice"],
}