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
9
2
from models import ShelterluvPeople
10
3
import structlog
11
4
logger = structlog .get_logger ()
25
18
logger .error ("Couldn't get SHELTERLUV_SECRET_TOKEN from file or environment" )
26
19
27
20
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
52
21
22
+ TEST_MODE = os .getenv ("TEST_MODE" ) # if not present, has value None
53
23
#################################
54
24
# This script is used to fetch data from shelterluv API.
55
25
# Please be mindful of your usage.
@@ -64,39 +34,37 @@ def write_csv(json_data):
64
34
65
35
''' Iterate over all shelterlove people and store in json file in the raw data folder
66
36
We 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 ):
68
38
offset = 0
69
39
LIMIT = 100
70
40
has_more = True
71
- shelterluv_people = []
41
+
42
+ session .execute ("TRUNCATE TABLE shelterluvpeople" )
72
43
73
44
logger .debug ("Start getting shelterluv contacts from people table" )
74
45
75
46
while has_more :
76
47
r = requests .get ("http://shelterluv.com/api/v1/people?limit={}&offset={}" .format (LIMIT , offset ),
77
48
headers = {"x-api-key" : SHELTERLUV_SECRET_TOKEN })
78
49
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
84
67
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 ]
90
68
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" )
100
70
101
- logger .debug ("Uploading shelterluvpeople csv to database" )
102
- ShelterluvPeople .insert_from_df (pd .read_csv (file_path , dtype = "string" ), conn )
0 commit comments