142 lines
4.8 KiB
Python
142 lines
4.8 KiB
Python
|
import re
|
||
|
import json
|
||
|
import shutil
|
||
|
import argparse
|
||
|
import os
|
||
|
|
||
|
def main():
|
||
|
parser = argparse.ArgumentParser(description='Procesar archivos JSON a partir de un archivo de entrada.')
|
||
|
parser.add_argument('input_file', type=str, help='Ruta al archivo de entrada (.txt)')
|
||
|
parser.add_argument('folder', type=str, help='Nombre del directorio para eliminar del URI')
|
||
|
parser.add_argument('output_file', type=str, help='Nombre del archivo de salida (.json)')
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
input_file = args.input_file
|
||
|
folder = args.folder
|
||
|
output_file = args.output_file
|
||
|
|
||
|
if step_1(input_file):
|
||
|
new_data = step_2(input_file, folder)
|
||
|
update_json(output_file, new_data)
|
||
|
create_output_copies(output_file) # Crear copias del archivo JSON
|
||
|
|
||
|
def step_1(file_path):
|
||
|
return file_path.endswith('.txt') or print("Archive is not txt, please contact support.")
|
||
|
|
||
|
def folder_guide(source_uri, folder):
|
||
|
return source_uri.split(folder, 1)[1] if folder in source_uri else source_uri
|
||
|
|
||
|
def step_2(file_path, folder):
|
||
|
with open(file_path, 'r') as file:
|
||
|
content = file.read()
|
||
|
|
||
|
pattern = r'\{[^{}]*"sourceUri"[^{}]*"dataTxId"[^{}]*|[^{}]*"dataTxId"[^{}]*"sourceUri"[^{}]*\}'
|
||
|
data = {}
|
||
|
|
||
|
for match in re.findall(pattern, content):
|
||
|
source_uri_match = re.search(r'"sourceUri"\s*:\s*"([^"]*)"', match)
|
||
|
data_tx_id_match = re.search(r'"dataTxId"\s*:\s*"([^"]*)"', match)
|
||
|
if source_uri_match and data_tx_id_match:
|
||
|
source_uri = folder_guide(source_uri_match.group(1), folder)
|
||
|
data[source_uri] = {'id': data_tx_id_match.group(1)}
|
||
|
|
||
|
print(f'Number of valid pairs found: {len(data)}')
|
||
|
return data
|
||
|
|
||
|
def update_json(output_file, new_data):
|
||
|
try:
|
||
|
with open(output_file, 'r') as file:
|
||
|
existing_data = json.load(file)
|
||
|
print(f'Successfully loaded {output_file}')
|
||
|
except FileNotFoundError:
|
||
|
print(f'File {output_file} not found. Creating a new one.')
|
||
|
existing_data = {
|
||
|
"manifest": "arweave/paths",
|
||
|
"version": "0.1.0",
|
||
|
"index": {
|
||
|
"path": "index.html"
|
||
|
},
|
||
|
"paths": {}
|
||
|
}
|
||
|
except json.JSONDecodeError:
|
||
|
print(f'Error in decoding JSON from {output_file}. Creating a new one.')
|
||
|
existing_data = {
|
||
|
"manifest": "arweave/paths",
|
||
|
"version": "0.1.0",
|
||
|
"index": {
|
||
|
"path": "index.html"
|
||
|
},
|
||
|
"paths": {}
|
||
|
}
|
||
|
|
||
|
index_keys = ['index.html', 'index.xml']
|
||
|
|
||
|
for key in index_keys:
|
||
|
if key in new_data:
|
||
|
existing_data['paths'][key] = new_data.pop(key)
|
||
|
|
||
|
existing_data['paths'].update(new_data)
|
||
|
|
||
|
with open(output_file, 'w') as file:
|
||
|
json.dump(existing_data, file, indent=4)
|
||
|
|
||
|
print(f'Updated JSON file: {output_file}')
|
||
|
|
||
|
def create_output_copies(output_file):
|
||
|
base_name, ext = os.path.splitext(output_file)
|
||
|
html_copy = f'{base_name}-html{ext}'
|
||
|
xml_copy = f'{base_name}-xml{ext}'
|
||
|
|
||
|
try:
|
||
|
shutil.copy(output_file, html_copy)
|
||
|
print(f'Successfully copied {output_file} to {html_copy}')
|
||
|
except FileNotFoundError:
|
||
|
print(f'Error: {output_file} not found. Could not copy to {html_copy}.')
|
||
|
return
|
||
|
except Exception as e:
|
||
|
print(f'Error copying {output_file} to {html_copy}: {e}')
|
||
|
return
|
||
|
|
||
|
try:
|
||
|
shutil.copy(output_file, xml_copy)
|
||
|
print(f'Successfully copied {output_file} to {xml_copy}')
|
||
|
except FileNotFoundError:
|
||
|
print(f'Error: {output_file} not found. Could not copy to {xml_copy}.')
|
||
|
return
|
||
|
except Exception as e:
|
||
|
print(f'Error copying {output_file} to {xml_copy}: {e}')
|
||
|
return
|
||
|
|
||
|
try:
|
||
|
with open(xml_copy, 'r') as file:
|
||
|
data = json.load(file)
|
||
|
print(f'Successfully loaded {xml_copy} for modification.')
|
||
|
except FileNotFoundError:
|
||
|
print(f'Error: {xml_copy} not found. Could not modify.')
|
||
|
return
|
||
|
except json.JSONDecodeError:
|
||
|
print(f'Error: {xml_copy} contains invalid JSON. Could not modify.')
|
||
|
return
|
||
|
except Exception as e:
|
||
|
print(f'Error reading {xml_copy}: {e}')
|
||
|
return
|
||
|
|
||
|
if 'index' in data and 'path' in data['index']:
|
||
|
if data['index']['path'] == 'index.html':
|
||
|
data['index']['path'] = 'index.xml'
|
||
|
print(f'Successfully changed "index.html" to "index.xml" in {xml_copy}.')
|
||
|
else:
|
||
|
print(f'No changes made to "index.html" in {xml_copy}. The path was not "index.html".')
|
||
|
else:
|
||
|
print(f'No "index" or "path" field found in {xml_copy}. No changes made.')
|
||
|
|
||
|
try:
|
||
|
with open(xml_copy, 'w') as file:
|
||
|
json.dump(data, file, indent=4)
|
||
|
print(f'Successfully saved changes to {xml_copy}.')
|
||
|
except Exception as e:
|
||
|
print(f'Error saving changes to {xml_copy}: {e}')
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|