cleaner json output on reports when converting from csv
This commit is contained in:
parent
e9795a0177
commit
ff89f14a85
@ -2,7 +2,7 @@
|
|||||||
# ICANN API for the Centralized Zones Data Service - developed by acidvegas (https://git.acid.vegas/czds)
|
# ICANN API for the Centralized Zones Data Service - developed by acidvegas (https://git.acid.vegas/czds)
|
||||||
# czds/__init__.py
|
# czds/__init__.py
|
||||||
|
|
||||||
__version__ = '1.3.5'
|
__version__ = '1.3.6'
|
||||||
__author__ = 'acidvegas'
|
__author__ = 'acidvegas'
|
||||||
__email__ = 'acid.vegas@acid.vegas'
|
__email__ = 'acid.vegas@acid.vegas'
|
||||||
__github__ = 'https://github.com/acidvegas/czds'
|
__github__ = 'https://github.com/acidvegas/czds'
|
@ -6,6 +6,8 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import csv
|
||||||
|
import io
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -138,15 +140,12 @@ class CZDS:
|
|||||||
Downloads the zone report stats from the API and scrubs the report for privacy
|
Downloads the zone report stats from the API and scrubs the report for privacy
|
||||||
|
|
||||||
:param filepath: Filepath to save the scrubbed report
|
: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')
|
:param format: Output format ('csv' or 'json')
|
||||||
'''
|
'''
|
||||||
|
|
||||||
logging.info('Downloading zone stats report')
|
logging.info('Downloading zone stats report')
|
||||||
|
|
||||||
# Send the request to the API
|
# Send the request to the API
|
||||||
async with self.session.get('https://czds-api.icann.org/czds/requests/report', headers=self.headers) as response:
|
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:
|
if response.status != 200:
|
||||||
raise Exception(f'Failed to download the zone stats report: {response.status} {await response.text()}')
|
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')
|
content = content.replace(self.username, 'nobody@no.name')
|
||||||
logging.debug('Scrubbed username from report')
|
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':
|
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')
|
logging.debug('Converted report to JSON format')
|
||||||
|
|
||||||
# Save the report to a file if a filepath is provided
|
# Save the report to a file if a filepath is provided
|
||||||
|
2
setup.py
2
setup.py
@ -11,7 +11,7 @@ with open('README.md', 'r', encoding='utf-8') as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='czds-api',
|
name='czds-api',
|
||||||
version='1.3.5',
|
version='1.3.6',
|
||||||
author='acidvegas',
|
author='acidvegas',
|
||||||
author_email='acid.vegas@acid.vegas',
|
author_email='acid.vegas@acid.vegas',
|
||||||
description='ICANN API for the Centralized Zones Data Service',
|
description='ICANN API for the Centralized Zones Data Service',
|
||||||
|
Loading…
Reference in New Issue
Block a user