Guest User

fixtcx_v1.5.py

a guest
Jan 21st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | Sports | 0 0
  1. ##This script allows the batch repair of corrupted .tcx activity files.
  2. ##Create backups before use and use script at own risk.
  3. ##License: GPLv3
  4.  
  5. import os
  6. import xml.etree.ElementTree as ET
  7.  
  8. def prettify(elem, level=0):
  9.    
  10.     #Add indentation to the XML element
  11.     indent = "  "
  12.     i = "\n" + level * indent
  13.     if len(elem):
  14.         if not elem.text or not elem.text.strip():
  15.             elem.text = i + indent
  16.         if not elem.tail or not elem.tail.strip():
  17.             elem.tail = i
  18.         for sub_elem in elem:
  19.             prettify(sub_elem, level + 1)
  20.         if not sub_elem.tail or not sub_elem.tail.strip():
  21.             sub_elem.tail = i
  22.     else:
  23.         if level and (not elem.tail or not elem.tail.strip()):
  24.             elem.tail = i
  25.  
  26. def convert_tomtom_to_garmin(xml_content):
  27.     root = ET.fromstring(xml_content)
  28.  
  29.     # Update namespace
  30.     ET.register_namespace('', 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2')
  31.     ET.register_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
  32.     ET.register_namespace('x', 'http://www.garmin.com/xmlschemas/ActivityExtension/v2')
  33.  
  34.     # Add indentation to the XML structure
  35.     prettify(root)
  36.    
  37.     # Convert the XML tree to a string
  38.     converted_xml = ET.tostring(root, encoding="utf-8").decode("utf-8")
  39.     return converted_xml
  40.  
  41. def convert_all_files(input_folder, output_folder):
  42.     # Create output folder if it doesn't exist
  43.     os.makedirs(output_folder, exist_ok=True)
  44.  
  45.     # Iterate through each file in the input folder
  46.     for filename in os.listdir(input_folder):
  47.         if filename.endswith(".tcx"):
  48.             input_path = os.path.join(input_folder, filename)
  49.             output_path = os.path.join(output_folder, filename)
  50.             convert_tomtom_to_garmin_and_save(input_path, output_path)
  51.  
  52. def convert_tomtom_to_garmin_and_save(input_path, output_path):
  53.     with open(input_path, 'r', encoding="utf-8") as file:
  54.         # Read all lines
  55.         lines = file.readlines()
  56.  
  57.     # Remove leading whitespace from the first line
  58.     if lines:
  59.         lines[0] = lines[0].lstrip()
  60.  
  61.     xml_content = ''.join(lines)
  62.  
  63.     converted_xml = convert_tomtom_to_garmin(xml_content)
  64.  
  65.     # Check if the XML declaration is present
  66.     if not converted_xml.startswith('<?xml'):
  67.         # Add the XML declaration if not present
  68.         converted_xml = f'<?xml version="1.0" encoding="UTF-8"?>\n{converted_xml}'
  69.  
  70.     with open(output_path, 'w', encoding="utf-8") as file:
  71.         file.write(converted_xml)
  72.  
  73. if __name__ == "__main__":
  74.     input_folder_path = './input'
  75.     output_folder_path = './converted'
  76.  
  77.     convert_all_files(input_folder_path, output_folder_path)
Advertisement
Add Comment
Please, Sign In to add comment