44import requests
55
66from api .API_ingest import shelterluv_db
7+ from server .api .API_ingest .shelterluv_db import insert_animals
78
89
910# from config import engine
1011# from flask import current_app
1112# from sqlalchemy.sql import text
1213
1314BASE_URL = 'http://shelterluv.com/api/'
14-
15+ MAX_COUNT = 100 # Max records the API will return for one call
1516
1617try :
1718 from secrets_dict import SHELTERLUV_SECRET_TOKEN
@@ -62,6 +63,35 @@ def get_animal_count():
6263 return - 5 # AFAICT, this means URL was bad
6364
6465
66+ def get_updated_animal_count (last_update ):
67+ """Test that server is operational and get total animal count."""
68+ animals = 'v1/animals&offset=0&limit=1&sort=updated_at&since=' + str (last_update )
69+ URL = path .join (BASE_URL ,animals )
70+
71+ try :
72+ response = requests .request ("GET" ,URL , headers = headers )
73+ except Exception as e :
74+ logger ('get_updated_animal_count failed with ' , e )
75+ return - 2
76+
77+ if response .status_code != 200 :
78+ logger ("get_updated_animal_count " , response .status_code , "code" )
79+ return - 3
80+
81+ try :
82+ decoded = json .loads (response .text )
83+ except json .decoder .JSONDecodeError as e :
84+ logger ("get_updated_animal_count JSON decode failed with" , e )
85+ return - 4
86+
87+ if decoded ['success' ]:
88+ return decoded ['total_count' ]
89+ else :
90+ return - 5 # AFAICT, this means URL was bad
91+
92+
93+
94+
6595def filter_animals (raw_list ):
6696 """Given a list of animal records as returned by SL, return a list of records with only the fields we care about."""
6797
@@ -72,7 +102,13 @@ def filter_animals(raw_list):
72102 for r in raw_list :
73103 f = {}
74104 for k in good_keys :
75- f [k ] = r [k ]
105+ try :
106+ f [k ] = r [k ]
107+ except :
108+ if k in ('DOBUnixTime' ,'LastUpdatedUnixTime' ):
109+ f [k ] = 0
110+ else :
111+ f [k ] = ''
76112 filtered .append (f )
77113
78114 return filtered
@@ -83,21 +119,21 @@ def filter_animals(raw_list):
83119def get_animals_bulk (total_count ):
84120 """Pull all animal records from SL """
85121
86- MAX_COUNT = 100 # Max records the API will return for one call
87-
88122 # 'Great' API design - animal record 0 is the newest, so we need to start at the end,
89123 # back up MAX_COUNT rows, make our request, then keep backing up. We need to keep checking
90124 # the total records to ensure one wasn't added in the middle of the process.
91125 # Good news, the API is robust and won't blow up if you request past the end.
92126
93127 raw_url = path .join (BASE_URL , 'v1/animals&offset={0}&limit={1}' )
94128
95- start_record = total_count - 1
129+ start_record = int ( total_count )
96130 offset = (start_record - MAX_COUNT ) if (start_record - MAX_COUNT ) > - 1 else 0
131+ limit = MAX_COUNT
97132
98133 while offset > - 1 :
99134
100- url = raw_url .format (offset ,MAX_COUNT )
135+ logger ("getting at offset" , offset )
136+ url = raw_url .format (offset ,limit )
101137
102138 try :
103139 response = requests .request ("GET" ,url , headers = headers )
@@ -116,26 +152,55 @@ def get_animals_bulk(total_count):
116152 return - 4
117153
118154 if decoded ['success' ]:
119- return decoded ['animals' ]
155+ insert_animals ( filter_animals (decoded ['animals' ]) )
156+ if offset == 0 :
157+ break
158+ offset -= MAX_COUNT
159+ if offset < 0 :
160+ limit = limit + offset
161+ offset = 0
120162 else :
121163 return - 5 # AFAICT, this means URL was bad
122164
165+ return 'zero'
166+
167+
168+ def update_animals (last_update ):
169+ """Get the animals inserted or updated since last check, insert/update db records. """
170+
171+ updated_records = get_updated_animal_count (last_update )
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
123189
124190
125191
126192def sla_test ():
127193 total_count = get_animal_count ()
128194 print ('Total animals:' ,total_count )
129195
130- b = get_animals_bulk (200 )
196+ b = get_animals_bulk (total_count )
131197 print (len (b ))
132198
133- f = filter_animals (b )
134- print (f )
135-
136- count = shelterluv_db .insert_animals (f )
137- return count
199+ # f = filter_animals(b)
200+ # print(f)
138201
202+ # count = shelterluv_db.insert_animals(f)
203+ return len (b )
139204
140205# if __name__ == '__main__' :
141206
0 commit comments