Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active September 21, 2022 00:49
Show Gist options
  • Save fallwith/2b7372bacee0c1b6ca2a49b61a3d4666 to your computer and use it in GitHub Desktop.
Save fallwith/2b7372bacee0c1b6ca2a49b61a3d4666 to your computer and use it in GitHub Desktop.
Convert an emulationstation based dir to a miyoo mini v2 compatible one
#!/usr/bin/env ruby
# frozen_string_literal: true
# encoding: utf-8
require 'fileutils'
require 'nokogiri'
SOURCE_IMG_DIR = 'media/covers'
TARGET_IMG_DIR = 'Imgs'
UNUSED_ELEMENTS = %w[desc developer genre kidgame marquee players rating releasedate publisher video]
root = ARGV.first || '.'
source_xml_file = File.join(root, 'gamelist.xml')
target_xml_file = File.join(root, 'miyoogamelist.xml')
xml = Nokogiri.parse(File.read(source_xml_file))
games = xml.xpath('//gameList/game')
games.each do |game|
thumb = game.elements.detect { |e| e.name == 'thumbnail' }
image = game.elements.detect { |e| e.name == 'image' }
if thumb.children.empty?
name_element = game.elements.detect { |e| e.name == 'name' }
name = name_element ? name_element.children.first.content : 'UNKNOWN'
warn "No thumbnail image found for title '#{name}'!"
exit
end
image.children = thumb.children
image.children.first.content = image.children.first.content.gsub(SOURCE_IMG_DIR, TARGET_IMG_DIR)
thumb.remove
UNUSED_ELEMENTS.each do |name|
element = game.elements.detect { |e| e.name == name }
next unless element
element.remove
end
end
File.open(target_xml_file, 'w') { |f| f.puts xml.to_s.gsub(/\n\s+\n/, "\n") }
File.unlink(source_xml_file)
FileUtils.rm_rf('Imgs')
FileUtils.mv(SOURCE_IMG_DIR, TARGET_IMG_DIR)
FileUtils.rm_rf('media')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment