-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
166 lines (138 loc) · 5.7 KB
/
backend.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
import json
import pandas as pd
import requests
import boto3
from datetime import datetime, timedelta
tokens = {}
with open("creds", "r") as creds:
for line in creds:
key, value = line.strip().split("=")
tokens[key] = value.replace('"', '')
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={tokens['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_api(query: str):
query = query.lower()
call = f"https://perenual.com/api/species-list?key={tokens['PERENUAL_TOKEN']}&q={query.lower()}"
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.lower().replace(' ', '_')}.json"
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
return data['data']
else:
print(f"Failed to retrieve plants data, Status Code: {response.status_code}")
return []
s3 = boto3.client('s3')
bucket_name = 'plants-data'
file_name = 'plant_data.json'
def read_from_s3():
try:
data = s3.get_object(Bucket=bucket_name, Key=file_name)
plants_data = json.loads(data['Body'].read().decode('utf-8'))
return plants_data
#return plants_data['plants']
except Exception as e:
print(e)
return None # or handle error appropriately
def write_to_s3(data):
try:
s3.put_object(Bucket=bucket_name, Key=file_name, Body=json.dumps(data))
print("Success!")
except Exception as e:
print(e)
def read_local_plant_data(filename='plants.json'):
with open(filename, 'r') as file:
data = json.load(file)
return data['plants']