#!/usr/bin/python

# This hack adds the image, video and marquee elements to the gamelist.xml file if they exist.

# To run: python <path to addMedia.py>/addMedia.py <path to rom folder>.

# Assumes:

# videos are in a 'snap' directory

# marquee are in a 'wheel' directory

# images are in a 'boxart' directory

#

# Examples:

# <path>./10-Yard Fight (USA, Europe).zip</path>

# <image>./boxart/10-Yard Fight (USA, Europe).png</image>

# <marquee>./wheel/10-Yard Fight (USA, Europe).png</marquee>

# <video>./snap/10-Yard Fight (USA, Europe).mp4</video>

import sys

import os

import xml . etree . ElementTree as ET

# Get Emulator ROM path IE: /home/pi/RetroPie/roms/snes/

romPath = ''

if sys . argv > 1 :

romPath = sys . argv [ 1 ]

# Parse the gamelist.xml file in the current directory

tree = ET. parse ( 'gamelist.xml' )

# Get the root element for the tree

gamelist = tree. getroot ( )

# For each game in the game list.

for game in gamelist:

# Initialize some variables

romName = ''

boxart = ''

video = ''

marquee = ''

hasVideo = False

hasMarquee = False

hasBoxArt = False

# Scan through the elements for this game.

for ele in game:

# print(ele.tag, ele.text)

if ele. tag == 'video' :

hasVideo = True

if ele. tag == 'marquee' :

hasMarquee = True

if ele. tag == 'image' :

hasBoxArt = True

# Process the path and use it to determine the possible video, marquee and image paths

if ele. tag == 'path' :

# Assume that all paths begin with './' and strip it.

romFile = ele. text [ 2 : ]

# Assume that all paths end with a 4 character extension and strip it.

romName = romFile [ :- 4 ]

# Build the video, marquee and boxart paths.

video = 'snap/' + romName + '.mp4'

marquee = 'wheel/' + romName + '.png'

boxart = 'boxart/' + romName + '.png'

# Debug

#print('Name: ' + romName)

#print(romPath + boxart + ' : ' + str(os.path.isfile(romPath + boxart)))

#print(romPath + video + ' : ' + str(os.path.isfile(romPath + video)))

#print(romPath + marquee + ' : ' + str(os.path.isfile(romPath + marquee)))

# Check to see if the file exists AND that we don't already have that element.

# If we don't have it, add the element to the game.

if os . path . isfile ( romPath + boxart ) and hasBoxArt == False :

vElement = ET. SubElement ( game , 'image' )

vElement. text = './' + boxart

if os . path . isfile ( romPath + video ) and hasVideo == False :

vElement = ET. SubElement ( game , 'video' )

vElement. text = './' + video

if os . path . isfile ( romPath + marquee ) and hasMarquee == False :

mElement = ET. SubElement ( game , 'marquee' )

mElement. text = './' + marquee

# Finally write the gamelist XML to gamelist-mod.xml