def restful_api_scraper(request, json_file_name): ''' Scrapes provided restful api into provided json file name ''' try: response = requests.get(str(request)) print("Successful api call") print("Writing response to file") write_json_file(json_file_name, response.json()) print("Successfully written to file: " + json_file_name) except requests.exceptions.Timeout: print('Timeout') except requests.exceptions.TooManyRedirects: print('Try different URL next time') except requests.exceptions.RequestException as exp: print(exp)
def test_json_utils2(self): ''' Testing write_json_file and read_json_file by writing empty dict to file ''' test_dict = {} temp_test_file = './temp_json_test_file.json' # Write test_dict to temp_test_file json_utils.write_json_file(temp_test_file, test_dict) # Read temp_json_test_file.json to get dict dict_in_temp_test_file = json_utils.read_json_file(temp_test_file) # Make sure json reader returns original test_dict from file self.assertEqual(dict_in_temp_test_file, test_dict) # Clean up and delete temp test file os.remove(temp_test_file)
''' Collects all locations of charities into a dict with key=zip code and value=[city, state]. Places this dict into the file ./charity_locations.json ''' import sys # pylint: disable=relative-import from get_all_charities_from_jsons import get_all_charities_from_jsons # Add path to allow importing from same level directory, then disable pylint import warning sys.path.insert(0, "../python_utils") # pylint: disable=import-error, wrong-import-position, wrong-import-order from json_utils import write_json_file CHARITIES = get_all_charities_from_jsons() # collect all locations in dictionary where zip code is key and [city, state] are values ZIP_CODE_DICT = {} for charity in CHARITIES: charity_address = charity['mailingAddress'] charity_zip = int(charity_address['postalCode']) charity_city = charity_address['city'] charity_state = charity_address['stateOrProvince'] ZIP_CODE_DICT[charity_zip] = [charity_city, charity_state] # Create new json file called charity_locations.json write_json_file("../charities/charity_locations.json", ZIP_CODE_DICT)
''' Module used to create the states by number dictionary. Iterates over all states scraped from US census to do this. ''' import sys # Add path to allow importing from same level directory, then disable pylint import warning sys.path.insert(0, "../python_utils") # pylint: disable=import-error, wrong-import-position, wrong-import-order from json_utils import read_json_file, write_json_file # Get all scraped states with numbers STATES = read_json_file("./states.json") # Dict will be writing to json file STATES_BY_NUM_DICT_TO_WRITE = {} # Skip first one STATES_ITER = iter(STATES) STATES_ITER.next() for state in STATES_ITER: state_num = int(state[2]) state_name = state[0] STATES_BY_NUM_DICT_TO_WRITE[state_num] = state_name write_json_file("../states/states_by_num_dict.json", STATES_BY_NUM_DICT_TO_WRITE)