79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
import csv
|
|
import requests
|
|
import sys
|
|
|
|
def get_api_endpoint_map():
|
|
# Define a mapping of names to API endpoints and keys
|
|
return {
|
|
"member": {
|
|
"url": "https://api.congress.gov/v3/member",
|
|
"key": "NYjZ0jWEeOuLI5by1vfa7Z7orA717mvd2oIvalQe"
|
|
},
|
|
# Add more endpoints as needed
|
|
}
|
|
|
|
def fetch_json_data(endpoint_name):
|
|
api_endpoint_map = get_api_endpoint_map()
|
|
|
|
if endpoint_name not in api_endpoint_map:
|
|
print(f"Error: Endpoint '{endpoint_name}' is not defined.")
|
|
sys.exit(1)
|
|
|
|
endpoint_info = api_endpoint_map[endpoint_name]
|
|
url = endpoint_info["url"]
|
|
key = endpoint_info["key"]
|
|
|
|
# Make the HTTP GET request to fetch JSON data
|
|
headers = {
|
|
"X-API-KEY": key
|
|
}
|
|
response = requests.get(url, headers=headers)
|
|
|
|
if response.status_code != 200:
|
|
print(f"Error: Failed to fetch data. Status code {response.status_code}")
|
|
sys.exit(1)
|
|
|
|
json_data = response.json()
|
|
|
|
# Print the JSON response for debugging
|
|
print("JSON Response:")
|
|
print(json_data)
|
|
|
|
return json_data
|
|
|
|
def write_json_to_csv(json_data, filename="output.csv"):
|
|
# Open the CSV file for writing
|
|
with open(filename, mode='w', newline='', encoding='utf-8') as csvfile:
|
|
writer = csv.writer(csvfile, delimiter='^')
|
|
|
|
# Write the header row
|
|
if json_data and isinstance(json_data, dict) and 'members' in json_data:
|
|
data_list = json_data['members']
|
|
if data_list and len(data_list) > 0:
|
|
headers = data_list[0].keys()
|
|
writer.writerow(headers)
|
|
|
|
# Write each item to a new row
|
|
for item in data_list:
|
|
print(f"Writing row: {list(item.values())}") # Print statement added here
|
|
writer.writerow(item.values())
|
|
else:
|
|
print("No data to write.")
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python script.py <endpoint_name>")
|
|
sys.exit(1)
|
|
|
|
endpoint_name = sys.argv[1]
|
|
try:
|
|
json_data = fetch_json_data(endpoint_name)
|
|
write_json_to_csv(json_data)
|
|
print(f"Data has been written to output.csv.")
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|