1- import csv
2- import os
3- import time
4-
5- import requests
6- import pandas as pd
7- from api .API_ingest .dropbox_handler import upload_file_to_dropbox
8- from constants import RAW_DATA_PATH
1+ import requests , os
92from models import ShelterluvPeople
103import structlog
114logger = structlog .get_logger ()
2518 logger .error ("Couldn't get SHELTERLUV_SECRET_TOKEN from file or environment" )
2619
2720
28- def write_csv (json_data ):
29- now = time .localtime ()
30- now_date = time .strftime ("%Y-%m-%d--%H-%M-%S" , now )
31-
32- path = RAW_DATA_PATH + "shelterluvpeople-" + now_date + ".csv" # store file name to use for dropbox
33-
34- file_handle = open (path , "w" )
35-
36- csv_writer = csv .writer (file_handle )
37-
38- count = 0
39- for item in json_data :
40- if count == 0 :
41- # Writing headers of CSV file
42- header = item .keys ()
43- csv_writer .writerow (header )
44- count += 1
45-
46- # Writing data of CSV file
47- csv_writer .writerow (item .values ())
48-
49- file_handle .close ()
50-
51- return path
5221
22+ TEST_MODE = os .getenv ("TEST_MODE" ) # if not present, has value None
5323#################################
5424# This script is used to fetch data from shelterluv API.
5525# Please be mindful of your usage.
@@ -64,39 +34,37 @@ def write_csv(json_data):
6434
6535''' Iterate over all shelterlove people and store in json file in the raw data folder
6636We fetch 100 items in each request, since that is the limit based on our research '''
67- def store_shelterluv_people_all (conn ):
37+ def store_shelterluv_people_all (session ):
6838 offset = 0
6939 LIMIT = 100
7040 has_more = True
71- shelterluv_people = []
41+
42+ session .execute ("TRUNCATE TABLE shelterluvpeople" )
7243
7344 logger .debug ("Start getting shelterluv contacts from people table" )
7445
7546 while has_more :
7647 r = requests .get ("http://shelterluv.com/api/v1/people?limit={}&offset={}" .format (LIMIT , offset ),
7748 headers = {"x-api-key" : SHELTERLUV_SECRET_TOKEN })
7849 response = r .json ()
79- shelterluv_people += response ["people" ]
80- has_more = response ["has_more" ]
81- offset += 100
82-
83- logger .debug ("Finish getting shelterluv contacts from people table" )
50+ for person in response ["people" ]:
51+ #todo: Does this need more "null checks"?
52+ session .add (ShelterluvPeople (firstname = person ["Firstname" ],
53+ lastname = person ["Lastname" ],
54+ id = person ["ID" ] if "ID" in person else None ,
55+ internal_id = person ["Internal-ID" ],
56+ associated = person ["Associated" ],
57+ street = person ["Street" ],
58+ apartment = person ["Apartment" ],
59+ city = person ["City" ],
60+ state = person ["State" ],
61+ zip = person ["Zip" ],
62+ email = person ["Email" ],
63+ phone = person ["Phone" ],
64+ animal_ids = person ["Animal_ids" ]))
65+ offset += LIMIT
66+ has_more = response ["has_more" ] if not TEST_MODE else response ["has_more" ] and offset < 1000
8467
85- logger .debug ("Start storing latest shelterluvpeople results to container" )
86- if os .listdir (RAW_DATA_PATH ):
87- for file_name in os .listdir (RAW_DATA_PATH ):
88- file_path = os .path .join (RAW_DATA_PATH , file_name )
89- file_name_striped = file_path .split ('-' )[0 ].split ('/' )[- 1 ]
9068
91- if file_name_striped == "shelterluvpeople" :
92- os .remove (file_path )
93-
94- file_path = write_csv (shelterluv_people )
95- logger .debug ("Finish storing latest shelterluvpeople results to container" )
96-
97- logger .debug ("Start storing " + '/shelterluv/' + "results to dropbox" )
98- upload_file_to_dropbox (file_path , '/shelterluv/' + file_path .split ('/' )[- 1 ])
99- logger .debug ("Finish storing " + '/shelterluv/' + "results to dropbox" )
69+ logger .debug ("Finish getting shelterluv contacts from people table" )
10070
101- logger .debug ("Uploading shelterluvpeople csv to database" )
102- ShelterluvPeople .insert_from_df (pd .read_csv (file_path , dtype = "string" ), conn )
0 commit comments