|
import json |
|
import sys |
|
|
|
def extract_addresses(json_data, output_filename="addresses.txt"): |
|
"""Extracts server addresses from JSON data and writes them to a text file. |
|
|
|
Args: |
|
json_data: The JSON object containing the server data. |
|
output_filename: The name of the output text file (default: addresses.txt). |
|
""" |
|
try: |
|
data = json_data.get("Data", []) |
|
|
|
with open(output_filename, 'w', encoding='utf-8') as outfile: |
|
for server in data: |
|
address = server.get("Address") |
|
if address: |
|
outfile.write(address.strip() + '\n') |
|
|
|
|
|
except (IOError, json.JSONDecodeError) as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|
|
if __name__ == "__main__": |
|
if len(sys.argv) > 1: |
|
json_filename = sys.argv[1] |
|
|
|
try: |
|
with open(json_filename, 'r', encoding='utf-8') as f: |
|
json_data = json.load(f) |
|
|
|
except FileNotFoundError: |
|
print(f"Error: File '{json_filename}' not found.") |
|
sys.exit(1) |
|
except json.JSONDecodeError: |
|
print(f"Error: Invalid JSON format in '{json_filename}'.") |
|
sys.exit(1) |
|
|
|
|
|
if len(sys.argv) > 2: |
|
output_filename = sys.argv[2] |
|
extract_addresses(json_data, output_filename) |
|
print(f"Addresses extracted to '{output_filename}'") |
|
|
|
else: |
|
extract_addresses(json_data) |
|
print("Addresses extracted to 'addresses.txt'") |
|
|
|
|
|
|
|
else: |
|
print("Usage: python script_name.py <json_filename> <output_filename>(optional)") |
|
default_json = {"Data": []} |
|
extract_addresses(default_json) |
|
print("Addresses (from default JSON) extracted to 'addresses.txt'") |