-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_database_en.py
More file actions
412 lines (374 loc) · 14.1 KB
/
Copy pathcreate_database_en.py
File metadata and controls
412 lines (374 loc) · 14.1 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Creates the Garden Care database in English in the specified page
"""
import os
import sys
import io
# Fix Windows Console Encoding
if sys.platform == 'win32':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
from notion_client import Client
from dotenv import load_dotenv
from datetime import datetime, timedelta
# Load environment variables
load_dotenv()
NOTION_TOKEN = os.getenv('NOTION_TOKEN')
# Page-ID from URL: https://www.notion.so/Gardening-23bd91ab65f980168a04d071ba597442
PARENT_PAGE_ID = "23bd91ab65f980168a04d071ba597442"
if not NOTION_TOKEN:
print("❌ ERROR: NOTION_TOKEN not found!")
sys.exit(1)
# Initialize Notion Client
notion = Client(auth=NOTION_TOKEN)
def create_garden_database(parent_page_id):
"""Creates the Garden Care database with all properties"""
print("\n🌱 Creating Garden Care Database...")
try:
database = notion.databases.create(
parent={
"type": "page_id",
"page_id": parent_page_id
},
title=[
{
"type": "text",
"text": {
"content": "Garden Care"
}
}
],
properties={
# Basic Information
"Name": {
"title": {}
},
"Type": {
"select": {
"options": [
{"name": "Plant", "color": "green"},
{"name": "Tree", "color": "brown"},
{"name": "Shrub", "color": "yellow"},
{"name": "Vegetable", "color": "orange"},
{"name": "Herb", "color": "pink"},
{"name": "Lawn", "color": "green"}
]
}
},
"Location": {
"select": {
"options": [
{"name": "Garden", "color": "green"},
{"name": "Balcony", "color": "blue"},
{"name": "Terrace", "color": "purple"},
{"name": "Conservatory", "color": "yellow"},
{"name": "Indoor", "color": "gray"}
]
}
},
"Active": {
"checkbox": {}
},
# Fertilizing
"Fertilize Interval (days)": {
"number": {
"format": "number"
}
},
"Last Fertilized": {
"date": {}
},
"Next Fertilize": {
"formula": {
"expression": 'dateAdd(prop("Last Fertilized"), prop("Fertilize Interval (days)"), "days")'
}
},
"Fertilizer Type": {
"rich_text": {}
},
# Watering
"Water Interval (days)": {
"number": {
"format": "number"
}
},
"Last Watered": {
"date": {}
},
"Next Water": {
"formula": {
"expression": 'dateAdd(prop("Last Watered"), prop("Water Interval (days)"), "days")'
}
},
"Water Amount": {
"select": {
"options": [
{"name": "Low", "color": "yellow"},
{"name": "Medium", "color": "blue"},
{"name": "High", "color": "blue"}
]
}
},
# Pruning
"Prune Months": {
"multi_select": {
"options": [
{"name": "January", "color": "gray"},
{"name": "February", "color": "gray"},
{"name": "March", "color": "green"},
{"name": "April", "color": "green"},
{"name": "May", "color": "green"},
{"name": "June", "color": "yellow"},
{"name": "July", "color": "yellow"},
{"name": "August", "color": "orange"},
{"name": "September", "color": "orange"},
{"name": "October", "color": "brown"},
{"name": "November", "color": "brown"},
{"name": "December", "color": "gray"}
]
}
},
"Prune Instructions": {
"rich_text": {}
},
"Last Pruned": {
"date": {}
},
# Care & Notes
"Care Instructions": {
"rich_text": {}
},
"Special Notes": {
"rich_text": {}
},
"Notes": {
"rich_text": {}
}
}
)
database_id = database['id']
database_url = database['url']
print(f"\n✅ Database successfully created!")
print(f"\n📊 Database Details:")
print(f" ID: {database_id}")
print(f" URL: {database_url}")
# Save the Database-ID in .env
update_env_file(database_id)
return database_id
except Exception as e:
print(f"\n❌ Error creating database: {e}")
import traceback
traceback.print_exc()
return None
def update_env_file(database_id):
"""Updates the .env file with the Database-ID"""
try:
env_path = '.env'
# Read existing .env
with open(env_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Update Database-ID
updated = False
for i, line in enumerate(lines):
if line.startswith('NOTION_DATABASE_ID='):
lines[i] = f'NOTION_DATABASE_ID={database_id}\n'
updated = True
break
if not updated:
lines.append(f'NOTION_DATABASE_ID={database_id}\n')
# Write back
with open(env_path, 'w', encoding='utf-8') as f:
f.writelines(lines)
print(f"\n✅ .env file updated with Database-ID")
except Exception as e:
print(f"\n⚠️ Warning: Could not update .env: {e}")
print(f" Please add manually: NOTION_DATABASE_ID={database_id}")
def add_example_plants(database_id):
"""Adds example plants to the database"""
print("\n🌱 Adding example plants...")
example_plants = [
{
"Name": "Tomatoes",
"Type": "Vegetable",
"Location": "Garden",
"Active": True,
"Fertilize Interval (days)": 14,
"Last Fertilized": (datetime.now() - timedelta(days=10)).strftime("%Y-%m-%d"),
"Fertilizer Type": "Tomato fertilizer, NPK 5-6-8",
"Water Interval (days)": 2,
"Last Watered": (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d"),
"Water Amount": "High",
"Prune Months": ["March", "September"],
"Prune Instructions": "Remove suckers regularly. Cut back to ground level after harvest.",
"Care Instructions": "Fertilize regularly and water sufficiently. Provide support.",
"Special Notes": "Requires sunny location"
},
{
"Name": "Rose Bush",
"Type": "Plant",
"Location": "Garden",
"Active": True,
"Fertilize Interval (days)": 30,
"Last Fertilized": (datetime.now() - timedelta(days=25)).strftime("%Y-%m-%d"),
"Fertilizer Type": "Rose fertilizer",
"Water Interval (days)": 7,
"Last Watered": (datetime.now() - timedelta(days=3)).strftime("%Y-%m-%d"),
"Water Amount": "Medium",
"Prune Months": ["February", "March"],
"Prune Instructions": "Cut back to 3-5 buds in spring. Remove weak and diseased shoots.",
"Care Instructions": "Remove spent blooms after flowering",
"Special Notes": "Frost-sensitive, protect in winter"
},
{
"Name": "Apple Tree",
"Type": "Tree",
"Location": "Garden",
"Active": True,
"Fertilize Interval (days)": 90,
"Last Fertilized": (datetime.now() - timedelta(days=60)).strftime("%Y-%m-%d"),
"Fertilizer Type": "Fruit tree fertilizer",
"Water Interval (days)": 14,
"Last Watered": (datetime.now() - timedelta(days=8)).strftime("%Y-%m-%d"),
"Water Amount": "High",
"Prune Months": ["February", "March"],
"Prune Instructions": "Thinning cut: Remove diseased, inward-growing and crossing branches.",
"Care Instructions": "Check regularly for pests",
"Special Notes": "Needs pollinator nearby"
},
{
"Name": "Basil",
"Type": "Herb",
"Location": "Balcony",
"Active": True,
"Fertilize Interval (days)": 21,
"Last Fertilized": (datetime.now() - timedelta(days=15)).strftime("%Y-%m-%d"),
"Fertilizer Type": "Liquid fertilizer for herbs",
"Water Interval (days)": 3,
"Last Watered": (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d"),
"Water Amount": "Medium",
"Prune Months": ["May", "June", "July", "August"],
"Prune Instructions": "Harvest leaves regularly to encourage branching. Remove flowers.",
"Care Instructions": "Keep warm and sunny, not too wet",
"Special Notes": "Not winter-hardy, harvest in autumn"
}
]
for plant_data in example_plants:
try:
properties = {
"Name": {
"title": [
{
"text": {
"content": plant_data["Name"]
}
}
]
},
"Type": {
"select": {
"name": plant_data["Type"]
}
},
"Location": {
"select": {
"name": plant_data["Location"]
}
},
"Active": {
"checkbox": plant_data["Active"]
},
"Fertilize Interval (days)": {
"number": plant_data["Fertilize Interval (days)"]
},
"Last Fertilized": {
"date": {
"start": plant_data["Last Fertilized"]
}
},
"Fertilizer Type": {
"rich_text": [
{
"text": {
"content": plant_data["Fertilizer Type"]
}
}
]
},
"Water Interval (days)": {
"number": plant_data["Water Interval (days)"]
},
"Last Watered": {
"date": {
"start": plant_data["Last Watered"]
}
},
"Water Amount": {
"select": {
"name": plant_data["Water Amount"]
}
},
"Prune Months": {
"multi_select": [{"name": month} for month in plant_data["Prune Months"]]
},
"Prune Instructions": {
"rich_text": [
{
"text": {
"content": plant_data["Prune Instructions"]
}
}
]
},
"Care Instructions": {
"rich_text": [
{
"text": {
"content": plant_data["Care Instructions"]
}
}
]
},
"Special Notes": {
"rich_text": [
{
"text": {
"content": plant_data["Special Notes"]
}
}
]
}
}
notion.pages.create(
parent={"database_id": database_id},
properties=properties
)
print(f" ✅ {plant_data['Name']} added")
except Exception as e:
print(f" ❌ Error adding {plant_data['Name']}: {e}")
print("\n✅ Example plants added!")
def main():
"""Main function"""
print("=" * 60)
print("🌱 Notion Garden Care Database Setup (English)")
print("=" * 60)
print(f"\nUsing Page-ID: {PARENT_PAGE_ID}")
# Create database
database_id = create_garden_database(PARENT_PAGE_ID)
if not database_id:
print("\n❌ Setup failed.")
sys.exit(1)
# Add example plants
add_example_plants(database_id)
print("\n" + "=" * 60)
print("🎉 Setup completed successfully!")
print("=" * 60)
print("\n📋 Next steps:")
print(" 1. Open the database in Notion")
print(" 2. Check the example plants")
print(" 3. Continue with Home Assistant integration")
print(f"\n Database-ID: {database_id}")
print("\n")
if __name__ == "__main__":
main()