Created
February 14, 2025 08:02
-
-
Save shdwkl/0045cc66f37390d0a4f5a74685a22ae6 to your computer and use it in GitHub Desktop.
Convert a conversation from aistudio (Gemini) to markdown format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| from pathlib import Path | |
| import sys | |
| def is_json_file(file_path): | |
| """ | |
| Check if file contains valid JSON regardless of extension. | |
| Args: | |
| file_path (Path): Path to the file | |
| Returns: | |
| bool: True if file contains valid JSON, False otherwise | |
| """ | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| json.load(f) | |
| return True | |
| except (json.JSONDecodeError, UnicodeDecodeError): | |
| return False | |
| except FileNotFoundError: | |
| return False | |
| def find_json_file(name): | |
| """ | |
| Find the JSON file with or without extension. | |
| Args: | |
| name (str): File name or path | |
| Returns: | |
| Path: Path to the JSON file | |
| """ | |
| path = Path(name).resolve() | |
| # If the exact path exists and is valid JSON, use it | |
| if path.exists() and is_json_file(path): | |
| return path | |
| # Try with .json extension | |
| json_path = path.with_suffix('.json') | |
| if json_path.exists() and is_json_file(json_path): | |
| return json_path | |
| # If neither exists or is valid JSON | |
| print(f"Error: Could not find valid JSON file for '{name}'") | |
| sys.exit(1) | |
| def load_json_file(file_path): | |
| """ | |
| Load and parse JSON file. | |
| Args: | |
| file_path (Path): Path to the JSON file | |
| Returns: | |
| dict: Parsed JSON data | |
| """ | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return json.load(f) | |
| except Exception as e: | |
| print(f"Error reading file: {str(e)}") | |
| sys.exit(1) | |
| def write_chunks_to_file(chunks, output_file): | |
| """ | |
| Write chunks to output file. | |
| Args: | |
| chunks (list): List of chunk dictionaries | |
| output_file (Path): Path to output file | |
| """ | |
| try: | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| for chunk in chunks: | |
| text = chunk.get('text', '') | |
| if text: | |
| f.write(f"{text}\n\n{'=' * 20}\n") | |
| except Exception as e: | |
| print(f"Error writing to output file: {str(e)}") | |
| sys.exit(1) | |
| def get_output_filename(input_path): | |
| """ | |
| Generate output filename based on input filename in the same directory. | |
| Args: | |
| input_path (Path): Input file path | |
| Returns: | |
| Path: Output file path | |
| """ | |
| return input_path.parent / f"{input_path.stem}.md" | |
| def main(): | |
| # Get input file name from command line argument or prompt | |
| if len(sys.argv) > 1: | |
| input_name = sys.argv[1] | |
| else: | |
| input_name = input("Enter the input file name: ").strip() | |
| # Find and validate JSON file | |
| input_path = find_json_file(input_name) | |
| # Load and parse JSON data | |
| data = load_json_file(input_path) | |
| # Extract chunks, with error handling for missing keys | |
| try: | |
| chunks = data["chunkedPrompt"]["chunks"] | |
| except KeyError: | |
| print("Error: Invalid JSON structure. Missing required keys.") | |
| sys.exit(1) | |
| # Get output file path in the same directory as input file | |
| output_file = get_output_filename(input_path) | |
| # Write chunks to output file | |
| write_chunks_to_file(chunks, output_file) | |
| print(f"Successfully wrote chunks to {output_file}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment