czds

- ICANN Centralized Zone Data Service Tool
git clone git://git.acid.vegas/czds.git
Log | Files | Refs | Archive | README | LICENSE

commit ff89f14a85c3d663573e9dd50f050baae029cb36
parent e9795a01774f703f211cf9a227491222e6eda377
Author: acidvegas <acid.vegas@acid.vegas>
Date: Wed, 26 Mar 2025 15:33:51 -0400

cleaner json output on reports when converting from csv

Diffstat:
Mczds/__init__.py | 2+-
Mczds/client.py | 21++++++++++++++++-----
Msetup.py | 2+-

3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/czds/__init__.py b/czds/__init__.py
@@ -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'
 \ No newline at end of file
diff --git a/czds/client.py b/czds/client.py
@@ -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
diff --git a/setup.py b/setup.py
@@ -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',