cleaner json output on reports when converting from csv

This commit is contained in:
Dionysus 2025-03-26 15:33:51 -04:00
parent e9795a0177
commit ff89f14a85
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
3 changed files with 18 additions and 7 deletions

View File

@ -2,7 +2,7 @@
# ICANN API for the Centralized Zones Data Service - developed by acidvegas (https://git.acid.vegas/czds)
# czds/__init__.py
__version__ = '1.3.5'
__version__ = '1.3.6'
__author__ = 'acidvegas'
__email__ = 'acid.vegas@acid.vegas'
__github__ = 'https://github.com/acidvegas/czds'

View File

@ -6,6 +6,8 @@ import asyncio
import json
import logging
import os
import csv
import io
try:
import aiohttp
@ -138,15 +140,12 @@ class CZDS:
Downloads the zone report stats from the API and scrubs the report for privacy
:param filepath: Filepath to save the scrubbed report
:param scrub: Whether to scrub the username from the report
:param format: Output format ('csv' or 'json')
'''
logging.info('Downloading zone stats report')
# Send the request to the API
async with self.session.get('https://czds-api.icann.org/czds/requests/report', headers=self.headers) as response:
# Check if the request was successful
if response.status != 200:
raise Exception(f'Failed to download the zone stats report: {response.status} {await response.text()}')
@ -157,9 +156,21 @@ class CZDS:
content = content.replace(self.username, 'nobody@no.name')
logging.debug('Scrubbed username from report')
# Convert the report to JSON format if requested (default is CSV)
# Convert the report to JSON format if requested
if format.lower() == 'json':
content = json.dumps(content, indent=4)
# Parse CSV content
csv_reader = csv.DictReader(io.StringIO(content))
# Convert to list of dicts with formatted keys
json_data = []
for row in csv_reader:
formatted_row = {
key.lower().replace(' ', '_'): value
for key, value in row.items()
}
json_data.append(formatted_row)
content = json.dumps(json_data, indent=4)
logging.debug('Converted report to JSON format')
# Save the report to a file if a filepath is provided

View File

@ -11,7 +11,7 @@ with open('README.md', 'r', encoding='utf-8') as fh:
setup(
name='czds-api',
version='1.3.5',
version='1.3.6',
author='acidvegas',
author_email='acid.vegas@acid.vegas',
description='ICANN API for the Centralized Zones Data Service',