-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIconnection.py
More file actions
72 lines (60 loc) · 2.04 KB
/
Copy pathAPIconnection.py
File metadata and controls
72 lines (60 loc) · 2.04 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
import sqlite3
import requests
import os
from env_loader import load_env
# Load environment variables
load_env()
# Function to get optimal growing conditions from Trefle API
def get_growing_conditions(seed_type):
"""
Fetch optimal growing conditions for a given seed type from the Trefle API.
Args:
seed_type (str): The type of seed to search for in the Trefle API.
Returns:
dict: A dictionary containing the common name of the plant and its optimal growing conditions.
Returns 'N/A' for missing values. If no data is found, returns None.
"""
api_key = os.environ.get('TREFLE_API_KEY')
if not api_key:
print("Error: TREFLE_API_KEY environment variable not set")
return None
url = f'https://trefle.io/api/v1/plants/search?token={api_key}&q={seed_type}'
response = requests.get(url)
data = response.json()
if data['data']:
plant = data['data'][0]
return {
'common_name': plant.get('common_name', 'N/A'),
'optimal_conditions': plant.get('growing_conditions', 'N/A')
}
else:
return None
# Function to read seed types from the database
def read_seeds():
"""
Read seed types from the database.
Returns:
list: A list of seed types
"""
conn = sqlite3.connect('seeds.db')
cursor = conn.cursor()
cursor.execute('SELECT seed_type FROM seeds')
seeds = cursor.fetchall()
conn.close()
return [seed[0] for seed in seeds]
# Main function to display optimal growing conditions
def main():
seeds = read_seeds()
if not seeds:
print("No seeds found in the database.")
return
print("Optimal Growing Conditions:")
for seed in seeds:
conditions = get_growing_conditions(seed)
if conditions:
print(f"\nCommon Name: {conditions['common_name']}")
print(f"Optimal Conditions: {conditions['optimal_conditions']}")
else:
print(f"\nNo data found for seed type: {seed}")
if __name__ == "__main__":
main()