Jump to content
LaunchBox Community Forums

Script to remove incorrect 'Version' info


Recommended Posts

Hi,

This is a python 2.7 script to remove incorrect version info from the LB xml's. Due to a bug in LB, if a game does not have any version info in the file name (eg region etc) that is enclosed in brackets, LB will use the filename as the version info, which causes game names to display twice in LB.

It is possible to use two methods of identifying incorrect version info - searching for version info that does not start with '(' and end with ')', or using fuzzy matching to compare the 'Title' and 'Version' info in the xml. Fuzzy matching is not recommended because if the match score is to low it will return lots of false positives and if it is too high it will miss lots of entries. From what I have seen all version info is enclosed in brackets, so this method works best.

In the script set 'lb_dir = ' to point to your LB platform xml dir (beware of the forward slashes). Script will create backups in a backup dir in the platform dir.  To enable fuzzy matching set the variable to 'yes' and set the lowest score that counts as a match (from 0.1 to 1.0). Best if L is not running when using the script - if it is you'll need to restart it. After running the script select all games in the 'All' platform and hit F5 to refresh the info.

 

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import shutil
import sys
import xml.etree.ElementTree as ET
from difflib import SequenceMatcher as SM

reload(sys)  
sys.setdefaultencoding('utf-8')

lb_dir = 'd:/emulation/LaunchBox/Data/Platforms'
use_fuzzy_matching = 'no' #change to 'yes' to enable fuzzy matching - not recommended
match_score = 0.5

###
lb_dir_backup = os.path.join(lb_dir, 'backup')
for system in os.listdir(lb_dir):
	if system.endswith(".xml"):
		system_name = system[:-4]
		print(system_name)
		xml_lb = os.path.join(lb_dir, system)
		if os.path.exists(xml_lb):
			if not os.path.exists(lb_dir_backup):
				os.makedirs(lb_dir_backup)
			if os.path.exists(os.path.join(lb_dir_backup, system)):
				if os.path.exists(os.path.join(lb_dir_backup, system_name+'.xml.old')):
					os.remove(os.path.join(lb_dir_backup, system_name+'.xml.old'))
				shutil.move(os.path.join(lb_dir_backup, system_name+'.xml'), os.path.join(lb_dir_backup, system_name+'.xml.old'))
			shutil.move(xml_lb, lb_dir_backup)
		tree = ET.parse(os.path.join(lb_dir_backup, system))
		root = tree.getroot()
		for game in root.findall('Game'):
			v = game.find('Version')
			vt = game.find('Version').text
			t = game.find('Title')
			tt = game.find('Title').text
			if v != None and vt:
				if use_fuzzy_matching == 'yes':
					m = SM(None, vt.lower(), tt.lower()).ratio()
					if m > match_score:
						print('*** "'+tt+'" *** "'+vt+'" *** with a match of: '+str(m))
						game.find('Version').text = ''
				if use_fuzzy_matching == 'no':
					if not vt.startswith('(') and not vt.endswith(')'):
						print('*** "'+tt+'" *** "'+vt+'" ***')
						game.find('Version').text = ''
	tree.write(os.path.join(lb_dir, system_name+'.xml'), encoding='utf-8')

 

Edited by teeedubb
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...