-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
174 lines (135 loc) · 5.66 KB
/
test.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
import json
import pandas as pd
import requests
from backend import read_from_s3, write_to_s3, read_local_plant_data
from datetime import datetime, timedelta
def trefle_find_plant(query: str):
call = f"https://trefle.io/api/v1/plants/search?q={query}&token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = "trefle_jsons/" + query.replace(" ", "_") + "_plant.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
def trefle_find_species(query: str):
call = f"https://trefle.io/api/v1/species/search?q={query}&token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = "trefle_jsons/" + query.replace(" ", "_") + "_species.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve species data, Status Code: {response.status_code}")
def trefle_pull_request(query: str):
response = requests.get(query)
if response.status_code == 200:
data = response.json()
filename = "trefle_jsons/" + "request.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve request, Status Code: {response.status_code}")
def trefle_pull_plant(slug: str):
call = f"https://trefle.io/api/v1/plants/{slug}?token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = f"trefle_jsons/{slug}_plant.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
def trefle_pull_species(slug: str):
call = f"https://trefle.io/api/v1/species/{slug}?token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = f"trefle_jsons/{slug}_species.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve species data, Status Code: {response.status_code}")
def trefle_pull_plant_id(id: int):
id_str = str(id)
print(f"Finding plant id: {id_str}")
call = f"https://trefle.io/api/v1/plants/{id_str}?token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = f"trefle_jsons/{id_str}_plant.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
def trefle_pull_species_id(id: int):
id_str = str(id)
print(f"Finding species id: {id_str}")
call = f"https://trefle.io/api/v1/species/{id_str}?token={TREFLE_TOKEN}"
print(f"Running trefle API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = f"trefle_jsons/{id_str}_species.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve species data, Status Code: {response.status_code}")
def perenual_pull_species_list(page=1):
call = f"https://perenual.com/api/species-list?key={PERENUAL_TOKEN}&page={page}"
print(f"Running perenual API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = "perenual_jsons/plant_list.json"
if page != 1:
filename = f"perenual_jsons/plant_list_page_{page}.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
def perenual_query_plants(query: str):
call = f"https://perenual.com/api/species-list?key={PERENUAL_TOKEN}&q={query}"
print(f"Running perenual API search:\n{call}")
response = requests.get(call)
if response.status_code == 200:
data = response.json()
filename = f"perenual_jsons/query_{query.replace(' ', '_')}.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
def read_plant_data(filename='plants.json'):
with open(filename, 'r') as file:
data = json.load(file)
return data['plants']
def write_plants_data(plants_data):
with open('plants_TEST.json', 'w') as file:
json.dump(plants_data, file, indent=4)
#data = read_from_s3()
#write_plants_data(data)
#write_to_s3(data)
#new_data = {
# 'water_schedule': 5,
# 'position': 1
# }
#plants = read_plant_data("plants.json")
#breakpoint() # REMOVE
#for plant in plants:
# if plant['id'] == 0:
# plant.update(new_data)
#with open("perenual_jsons/query_philodendron_short_no_indent.json", 'w') as file:
# json.dump(data, file)
#perenual_query_plants("philodendron")
#data = read_plant_data("perenual_jsons/query_hong_kong_orchid.json")
#print(len(data))
#for plant in data:
# print(f"common_name: {plant['common_name']}")
# print(f"watering: {plant['watering']}")