From 7c48b67132aee57fe4ed4babe81387c6ce85ef51 Mon Sep 17 00:00:00 2001 From: "Tanner Collin (aider)" Date: Thu, 22 May 2025 19:09:27 -0600 Subject: [PATCH] feat: Process JSON entries to extract media part info --- har_parser.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/har_parser.py b/har_parser.py index 5ff51bf..c1a6bee 100644 --- a/har_parser.py +++ b/har_parser.py @@ -50,6 +50,65 @@ def main(): # Ensure response_text is not None (it can be an empty string for 0-byte files) data[key_from_query_param] = response_text + # Second loop to process JSON entries + for entry in entries: + response = entry.get('response', {}) + content = response.get('content', {}) + mime_type = content.get('mimeType', '') + + # Check if the mimeType indicates JSON + if 'json' not in mime_type.lower(): # Make check case-insensitive and broader + continue + + response_text = content.get('text') + if not response_text: + continue + + try: + json_data = json.loads(response_text) + except json.JSONDecodeError: + # If JSON parsing fails, skip this entry + continue + + media_container = json_data.get('MediaContainer', {}) + metadata_array = media_container.get('Metadata', []) + + if not isinstance(metadata_array, list): + continue # Skip if Metadata is not a list + + for metadata_element in metadata_array: + if not isinstance(metadata_element, dict): + continue # Skip if metadata_element is not a dict + media_array = metadata_element.get('Media', []) + + if not isinstance(media_array, list): + continue # Skip if Media is not a list + + for media_element in media_array: + if not isinstance(media_element, dict): + continue # Skip if media_element is not a dict + part_array = media_element.get('Part', []) + + if not isinstance(part_array, list): + continue # Skip if Part is not a list + + for part_element in part_array: + if not isinstance(part_element, dict): + continue # Skip if part_element is not a dict + + # Save 'key' and 'file' values to variables + # These variables will be overwritten in each iteration + # if multiple parts exist. + part_key = part_element.get('key') + part_file = part_element.get('file') + + # At this point, part_key and part_file are available. + # The user has not specified what to do with them yet. + # For example, to print them: + # if part_key and part_file: + # print(f"Found part: key='{part_key}', file='{part_file}'") + + print("\nProcessed data (truncated values):") if not data: print("No data was processed and stored.")