Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onorbumbum/fefee94a74f184ae37682520f7bc2f0f to your computer and use it in GitHub Desktop.
Save onorbumbum/fefee94a74f184ae37682520f7bc2f0f to your computer and use it in GitHub Desktop.
This script: Takes JSON files from an input directory and combines them into a single master output file Transforms each station's data to match your desired output format Handles both single objects and lists of objects in the input JSON files Includes error handling for file operations Processes the following transformations: Converts tags to …
import json
import os
from pathlib import Path
def transform_station(station):
"""Transform a single station record to the desired output format."""
# Extract tags and convert to list
genres = station.get('tags', '').split(',')
# Clean up empty strings and strip whitespace
genres = [g.strip() for g in genres if g.strip()]
# Add empty string at the end as per example
genres.append("")
# Create description from available fields
description = []
if station.get('language'):
description.append(station['language'].capitalize())
if station.get('country'):
description.append(f"Radio from {station['country']}")
if station.get('codec') and station.get('bitrate'):
description.append(f"{station['codec']} {station['bitrate']}kbps")
# If no description was created, add a placeholder
if not description:
description = ["No description available"]
return {
"name": station['name'],
"website": station.get('homepage', ''),
"stream": station.get('url_resolved', station.get('url', '')),
"genres": genres,
"description": description,
"discontinued": "false" if station.get('lastcheckok') == 1 else "true"
}
def process_json_files(input_directory, output_file):
"""Process all JSON files in the input directory and create a master output file."""
master_data = {}
# Iterate through all JSON files in the directory
for file_path in Path(input_directory).glob('*.json'):
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Handle both single objects and lists of objects
if isinstance(data, list):
stations = data
else:
stations = [data]
# Process each station
for station in stations:
if 'name' in station: # Only process if station has a name
transformed_station = transform_station(station)
master_data[station['name']] = transformed_station
except Exception as e:
print(f"Error processing {file_path}: {str(e)}")
# Write the master output file
try:
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(master_data, f, indent=2, ensure_ascii=False)
print(f"Successfully created master file: {output_file}")
except Exception as e:
print(f"Error writing output file: {str(e)}")
def main():
# Configure these paths according to your needs
input_directory = "/Users/onorbumbum/Downloads/InternetRadioList" # Directory containing input JSON files
output_file = "master_stations.json" # Name of the output file
# Create input directory if it doesn't exist
os.makedirs(input_directory, exist_ok=True)
# Process the files
process_json_files(input_directory, output_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment