Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
2
Indexable
Never
import json
import requests
import csv
from collections import OrderedDict
import sys
import os

interfaces_file_name = "sample.txt"
webservice_url = "https://jsonplaceholder.typicode.com/users"
nodes_to_write_on_csvs = ['company']


def read_file_by_line_to_list(filename):
    lines = []
    with open(filename, 'r') as file:
        for line in file:
            lines.append(line.strip())
    return lines

def request_json_webservice_uri_prepend(url, string_to_prepend):
    try:
        response = requests.get(url + string_to_prepend)
        response.raise_for_status()
        data = response.json()
        return data
    except requests.exceptions.RequestException as e:
        print(f"Error: Failed to fetch - {e}")
    except ValueError:
        print("Error: Invalid JSON response.")


def to_csv_flatten_dict(d, parent_key='', sep='_'):
    print(d)
    items = []
    for k, v in d.items():
        new_key = f"{parent_key}{sep}{k}" if parent_key else k
        if isinstance(v, dict):
            items.extend(to_csv_flatten_dict(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return OrderedDict(items)

def to_csv_dict_to_csv(data, output_file):
    flattened_data = []
    if isinstance(data, list):
        flattened_data.extend([to_csv_flatten_dict(item) for item in data])
    elif isinstance(data, dict):
        flattened_data.append(to_csv_flatten_dict(data))
    else:
        print("Error: Input data must be a dictionary or a list of dictionaries.")
        return

    if not flattened_data:
        print("Error: No dictionaries found in the data.")
        return

    keys = set().union(*flattened_data)

    with open(output_file, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(keys)
        for row in flattened_data:
            writer.writerow(row.values())


# Loop in file with interfaces list
for l in read_file_by_line_to_list(interfaces_file_name):
    # Read JSON Response with interface URL prepend
    response_json = request_json_webservice_uri_prepend(webservice_url, "/" +  l)
    # Write specific nodes to csv, loop on list to get nodes
    for n in nodes_to_write_on_csvs:
        #print(response_json[n])
        to_csv_dict_to_csv(response_json[n], "output-" + l + "-" + n + ".csv")