fix: Update get_overpass_result to accept data_file parameter and save response to file
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
				
			|||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
# TODO: consider using OSMPythonTools instead of requests or overpass library
 | 
					# TODO: consider using OSMPythonTools instead of requests or overpass library
 | 
				
			||||||
@@ -76,7 +77,7 @@ def https(value):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# TODO: take note of new get_overpass_result function which does the bulk of the new overpass query work
 | 
					# TODO: take note of new get_overpass_result function which does the bulk of the new overpass query work
 | 
				
			||||||
def get_overpass_result(area):
 | 
					def get_overpass_result(area, data_file):
 | 
				
			||||||
    """Build the Overpass query for fetching animal shelters in the specified area."""
 | 
					    """Build the Overpass query for fetching animal shelters in the specified area."""
 | 
				
			||||||
    overpass_endpoint = "https://overpass-api.de/api/interpreter"
 | 
					    overpass_endpoint = "https://overpass-api.de/api/interpreter"
 | 
				
			||||||
    overpass_query = f"""
 | 
					    overpass_query = f"""
 | 
				
			||||||
@@ -89,14 +90,17 @@ def get_overpass_result(area):
 | 
				
			|||||||
        """
 | 
					        """
 | 
				
			||||||
    r = requests.get(overpass_endpoint, params={'data': overpass_query})
 | 
					    r = requests.get(overpass_endpoint, params={'data': overpass_query})
 | 
				
			||||||
    if r.status_code == 200:
 | 
					    if r.status_code == 200:
 | 
				
			||||||
        result = osmtogeojson.process_osm_json(r.json())
 | 
					        rjson = r.json()
 | 
				
			||||||
 | 
					        with open(data_file, 'w', encoding='utf-8') as f:
 | 
				
			||||||
 | 
					            json.dump(rjson, f, ensure_ascii=False)
 | 
				
			||||||
 | 
					        result = osmtogeojson.process_osm_json(rjson)
 | 
				
			||||||
        return result
 | 
					        return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
    api_token, area, instance, data_file = get_config()
 | 
					    api_token, area, instance, data_file = get_config()
 | 
				
			||||||
    # Query shelters
 | 
					    # Query shelters
 | 
				
			||||||
    overpass_result = get_overpass_result(area)
 | 
					    overpass_result = get_overpass_result(area, data_file)
 | 
				
			||||||
    if overpass_result is None:
 | 
					    if overpass_result is None:
 | 
				
			||||||
        print("Error: get_overpass_result returned None")
 | 
					        print("Error: get_overpass_result returned None")
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user