Created
December 29, 2024 20:37
-
-
Save rudolphos/738302b1ed33fcffdd243e7280d1dd62 to your computer and use it in GitHub Desktop.
Drag and drop the .json exported SessionBuddy session onto the .py file containing this script and it will process it by removing the chrome-extension prefixes in the JSON url fields like this "chrome-extension://noogafoofpebimajpfpamcfhoaifemoa/suspended.html#ttl=Marvellous%20Suspender%20-%20Google%20Search&pos=0&uri="
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
import json | |
import re | |
import sys | |
import os | |
# Function to process the URLs in the JSON data | |
def process_json(data): | |
pattern = re.compile(r"chrome-extension://.*?/suspended\.html#ttl=.*?&uri=") | |
def clean_url(url): | |
return pattern.sub('', url) | |
if isinstance(data, dict): | |
for key, value in data.items(): | |
if key == "url" and isinstance(value, str): | |
data[key] = clean_url(value) | |
else: | |
process_json(value) | |
elif isinstance(data, list): | |
for item in data: | |
process_json(item) | |
# Main function to handle file operations | |
def main(): | |
if len(sys.argv) != 2: | |
print("Please drag and drop a JSON file onto this script.") | |
return | |
file_path = sys.argv[1] | |
if not os.path.isfile(file_path): | |
print("The provided file path is invalid.") | |
return | |
try: | |
# Read the JSON file | |
with open(file_path, 'r', encoding='utf-8') as file: | |
data = json.load(file) | |
# Process the JSON data | |
process_json(data) | |
# Save the processed JSON back to a file | |
output_file = os.path.join(os.path.dirname(file_path), "processed_" + os.path.basename(file_path)) | |
with open(output_file, 'w', encoding='utf-8') as file: | |
json.dump(data, file, indent=4) | |
print(f"Processed file saved as {output_file}") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment