Skip to content

Instantly share code, notes, and snippets.

@TheDiscordian
Last active July 17, 2024 18:16
Show Gist options
  • Save TheDiscordian/c1ce7cccad3359e14cc84d54be75904a to your computer and use it in GitHub Desktop.
Save TheDiscordian/c1ce7cccad3359e14cc84d54be75904a to your computer and use it in GitHub Desktop.
For EmulationStation. Used for merging sets or restoring hand-crafted edits after a new scrape.
import xml.etree.ElementTree as ET
import sys
def update_game_data(old_file, new_file, output_file):
old_tree = ET.parse(old_file)
old_root = old_tree.getroot()
new_tree = ET.parse(new_file)
new_root = new_tree.getroot()
old_games = {game.find('path').text: game for game in old_root.findall('game')}
new_games = {game.find('path').text: game for game in new_root.findall('game')}
for path, new_game in new_games.items():
if path in old_games:
old_game = old_games[path]
old_name = old_game.find('name')
new_name = new_game.find('name')
old_description = old_game.find('desc')
new_description = new_game.find('desc')
if old_name is not None and old_name.text is not None and old_name.text.strip() and old_description is not None and old_description.text is not None and old_description.text.strip():
new_name.text = old_name.text
new_description.text = old_description.text
old_image = old_game.find('image')
new_image = new_game.find('image')
if (new_image is None or new_image.text is None) and old_image is not None and old_image.text is not None and old_image.text.strip():
new_image = ET.SubElement(new_game, 'image')
new_image.text = old_image.text
new_tree.write(output_file, encoding='utf-8', xml_declaration=True)
print(f"Updated file saved as {output_file}")
def print_usage():
print("Usage: python script.py <old_data.xml> <new_data.xml> [<output_file.xml>]")
print("Example: python script.py old_data.xml new_data.xml merged_data.xml")
print("If <output_file.xml> is not provided, the default 'merged_gamelist.xml' will be used.")
if __name__ == "__main__":
if len(sys.argv) < 3 or len(sys.argv) > 4:
print_usage()
else:
old_file = sys.argv[1]
new_file = sys.argv[2]
output_file = sys.argv[3] if len(sys.argv) == 4 else 'merged_gamelist.xml'
update_game_data(old_file, new_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment