Advertisement
jxsl13

blight data stuff New Orleans project

Aug 19th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import requests  # needs to be installed
  2.  
  3. from urllib.request import urlopen, Request
  4. import json
  5. import urllib.parse
  6. import string
  7.  
  8. def remove_whitespace(in_string: str):
  9.     return in_string.translate(str.maketrans(dict.fromkeys(string.whitespace)))
  10.  
  11. def url_encode(string: str):
  12.     return urllib.parse.quote(f"{string}")
  13.  
  14.  
  15. def get_blight_data(start_date="05/2019", end_date="08/2019"):
  16.     search_term = "*"
  17.     status = ""
  18.     step = ""
  19.     workflow = "code_enforcement"
  20.     #start_date = ...
  21.     #end_date = ...
  22.     boundary_id = ""
  23.     page_id = 1 # seems to start at 1 and not at 0
  24.  
  25.     url = f"https://blightstatus.nola.gov/api/addresses/search?search_term={url_encode(search_term)}&status={url_encode(status)}&step={url_encode(step)}&workflow={url_encode(workflow)}&start_date={url_encode(start_date)}&end_date={url_encode(end_date)}&boundary_id={url_encode(boundary_id)}&page={url_encode(page_id)}"
  26.  
  27.     result_dataset = []
  28.  
  29.     response = requests.get(url)
  30.     current_json_data = response.json()
  31.  
  32.     # on every iteration
  33.     dataset_size = int(current_json_data['page_total'])
  34.     result_dataset += current_json_data['features']
  35.  
  36.     # get this only once
  37.     #total_dataset_size = int(current_json_data['total'])
  38.  
  39.     # load next page while that page has any data.
  40.     while dataset_size > 0 :
  41.         page_id += 1
  42.  
  43.         url = f"https://blightstatus.nola.gov/api/addresses/search?search_term={url_encode(search_term)}&status={url_encode(status)}&step={url_encode(step)}&workflow={url_encode(workflow)}&start_date={url_encode(start_date)}&end_date={url_encode(end_date)}&boundary_id={url_encode(boundary_id)}&page={url_encode(page_id)}"
  44.         response = requests.get(url)
  45.         current_json_data = response.json()
  46.         dataset_size = int(current_json_data['page_total'])
  47.         result_dataset += current_json_data['features']
  48.  
  49.     return result_dataset
  50.  
  51.  
  52.  
  53. def get_property_data(x,y):
  54.    
  55.     parameters= f"{x},{y}"
  56.     parameters = remove_whitespace(parameters)
  57.  
  58.  
  59.     url = f'https://gis.nola.gov/arcgis/rest/services/CompositePIN3/GeocodeServer/reverseGeocode?location={url_encode(parameters)}&f=json'
  60.    
  61.  
  62.     # I suggest using firefox and look at the network analysis stuff, especially the XHR requests and look at the request and response header.
  63.     headers = {
  64.         'Content-Type': 'application/x-www-form-urlencoded',
  65.         'Origin': 'http://property.nola.gov',
  66.         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0',
  67.         'Referrer' : 'http://property.nola.gov/'
  68.     }
  69.  
  70.     #print(url)
  71.     response = requests.get(url, headers=headers)
  72.  
  73.     if response.ok:
  74.  
  75.         dictionary = response.json()
  76.         return dictionary
  77.     else:
  78.         return None
  79.  
  80.  
  81.  
  82. if __name__ == "__main__":
  83.  
  84.     # maybe one can do some stuff with the address ids?
  85.     # could be found somewhere else, like in the property viewer data or so.
  86.     blight_data = get_blight_data(start_date="08/2019", end_date="08/2019")
  87.  
  88.     print("Retrieved", len(blight_data), "blight datasets")
  89.  
  90.    
  91.     counter = 1
  92.  
  93.     for elem in blight_data:
  94.         x,y = elem['geometry']['coordinates']
  95.        
  96.         print(f"({counter}/{len(blight_data)})({x},{y})", get_property_data(x,y))
  97.         #print(matched_elem)
  98.         counter += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement