Skip to content

Instantly share code, notes, and snippets.

@fmarot
Created May 12, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmarot/00a6b092432763b1ffb25a2c09156e5f to your computer and use it in GitHub Desktop.
Save fmarot/00a6b092432763b1ffb25a2c09156e5f to your computer and use it in GitHub Desktop.
This script creates a CSV file from the gamelist.xml files contained in your Recalbox
/**
This script creates a CSV file from the gamelist.xml files contained in your Recalbox
*/
/*
Telechargement automatique des librairies:
- jcifs pour acceder au partage reseau des fichier (protocole samba)
- IOUtils, la librairie contenue dans commons-io et qui permet de lire des fichiers facilement
*/
@Grapes([
@Grab(group='jcifs', module='jcifs', version='1.3.17'),
@Grab(group='commons-io', module='commons-io', version='2.6')
])
import jcifs.smb.SmbFile
import jcifs.smb.SmbFileInputStream
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
/** Demarage du script */
boolean remoteAccess = true
String raspberryIP = "192.168.0.11"
String localDir = "/media/vg1-data/Downloads/groovy-xml/tmp/"
def result = "";
if (remoteAccess) {
new SmbFile("smb://${raspberryIP}/share/roms/").listFiles().each { subdir ->
if (subdir.isDirectory()) {
SmbFile gamelist = new SmbFile(subdir, "gamelist.xml")
if (gamelist.exists()) { // test si le sous-repertoire contient un fichier "gamelist.xml"
String console = subdir.getName()
println console
String xml = IOUtils.toString(new SmbFileInputStream(gamelist), StandardCharsets.UTF_8.name());
result += parseXML(xml, console);
}
}
}
} else {
new File(localDir).eachDirRecurse() { subdir ->
File gamelist = new File(subdir, "gamelist.xml")
if (gamelist.exists()) {
String console = subdir.getName()
println console
String xml = IOUtils.toString(new FileInputStream(gamelist), StandardCharsets.UTF_8.name());
result += parseXML(xml, console);
}
}
}
File resultFile = new File("resultFile.csv")
resultFile.text = result
/** methode qui remplace dans une chaine les ';' par des ',' */
def stripSemicolonAndLineBreak(String s) {
s = s.replace(";", ",").replace("\r", "").replace("\n", " - ")
if (s.length() > 16000) {
println "long string detected !"
s.substring(16000)
}
return s
}
/** Cette methode prend en entree le contenu xml d'un fichier et renvoie la chaine au format CSV (comma-separated value) */
String parseXML(String xml, String console) {
String fileResult = ""
try {
xml = xml.replace("\uFEFF", "") // remove possible BOM from file content
def list = new XmlParser().parseText(xml)
list.game.each { p ->
fileResult += ("${console};"
+ "${stripSemicolonAndLineBreak(p.path.text())};"
+ "${stripSemicolonAndLineBreak(p.name.text())};"
+ "${stripSemicolonAndLineBreak(p.desc.text())};"
+ "${stripSemicolonAndLineBreak(p.image.text())};"
//+ "${stripSemicolonAndLineBreak(p.thumbnail.text())};"
+ "${stripSemicolonAndLineBreak(p.rating.text())};"
+ "${stripSemicolonAndLineBreak(p.releasedate.text())};"
+ "${stripSemicolonAndLineBreak(p.developer.text())};"
+ "${stripSemicolonAndLineBreak(p.publisher.text())};"
+ "${stripSemicolonAndLineBreak(p.genre.text())};"
+ "${stripSemicolonAndLineBreak(p.players.text())};"
+ "${stripSemicolonAndLineBreak(p.playcount.text())};"
+ "${stripSemicolonAndLineBreak(p.lastplayed.text())};"
)
fileResult += System.lineSeparator(); // saut de ligne
}
} catch (Exception e) {
println e;
}
return fileResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment