-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchboxscreenscraper.py
272 lines (243 loc) · 10.6 KB
/
launchboxscreenscraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
# lb2am.py - https://github.com/sharkusk/lb2am
# Copyright (C) 2017 - Marcus Kellerman
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import xml.etree.ElementTree as ET
import argparse
import os
import shutil
import codecs
import urllib2
import binascii
import zipfile
import fnmatch
# Local imports
import screenscraper as SS
import lb2am as LB
try:
from ssmap import SS_SYSTEM_MAP
except:
SS_SYSTEM_MAP = {}
SS_SYSTEM_XML_FILE = "screenscraper.fr-systemesListe.xml"
SS_MAP_HEADER = """#
# Generated by lb2am.py
# This file contains the mapping between screenscraper.fr and the platform /
# emulator names used by LaunchBox and Attractmode
#
# "Platform Name": "ScreenScaper ID",
SS_SYSTEM_MAP = {
"""
LB_TO_SS_MEDIA_MAP = {
'Screenshot - Gameplay': ["screenshot",],
'Fanart - Background': ["fanart",],
'Video': ["video",],
'Clear Logo': ["wheel",],
'Box - Front': ["box2d",],
'Box - 3D': ["box3d",],
'Box - Back': ["box2d-back",],
'Arcade - Marquee': ["marquee",],
'Banner': ["screenmarquee",],
'Manual': ["manuel", ],
'Advertisement Flyer - Front': ["flyer",],
'Cart - Front': ["support2d",],
}
ADDITIONAL_MAPPINGS = {
'Super Nintendo': ['Super Nintendo Entertainment System',],
'Mame': ['MAME', 'Final Burn Alpha',],
'NES': ['Nintendo Entertainment System',],
'Daphne': ['LaserDisc',],
'Gamecube': ['Nintendo GameCube',],
'Nintendo 64': ['Nintendo 64-VC',],
}
SS_LOCALE_PREFERENCE = ['us','us1','wor','eu','jp',]
class LaunchBoxScreenScraper(object):
""" """
def __init__(self, lbpath, devid, devpassword, softname, ssid, sspassword, useGameTitle=False, verbose=False):
self.ssparameters = {}
self.ssparameters['devid'] = devid
self.ssparameters['devpassword'] = devpassword
self.ssparameters['softname'] = softname
self.ssparameters['ssid'] = ssid
self.ssparameters['sspassword'] = sspassword
self.verbose = verbose
self.lbPath = lbpath
self.useGameTitle = useGameTitle
if len(SS_SYSTEM_MAP) is 0:
self.ssmap = self.CreateScreenScraperSystemMap('ssmap.py', False)
else:
self.ssmap = SS_SYSTEM_MAP
self.artDirs = self.CreateLaunchBoxArtFolderMap()
self.lbToSsMediaMap = LB_TO_SS_MEDIA_MAP
self.ssLocalePreference = SS_LOCALE_PREFERENCE
def CreateScreenScraperSystemMap(self, ssmapFileName, updateSystems):
"""
Writes the ssmap.py file and returns a system map with entries like this:
{ "Sega Megadrive": "1", ... }
"""
syslist = SS.SystemList( updateCache=updateSystems, **self.ssparameters)
ssmap = syslist.GetSystemList()
for ssplat in ADDITIONAL_MAPPINGS.keys():
for lbplat in ADDITIONAL_MAPPINGS[ssplat]:
ssmap[lbplat] = ssmap[ssplat]
f = open('ssmap.py', 'w')
f.write(SS_MAP_HEADER)
for key in ssmap:
f.write(' "%s": "%s",\n' % (key.encode('utf-8'), ssmap[key]))
f.write('}')
f.close()
return ssmap
def CreateLaunchBoxArtFolderMap( self ):
"""
Returns a dictionary with the following format:
artDirs = { platform: { 'Video': 'd:/...', 'Clear Logo': xxxx, ... }, ... }
"""
artDirs = {}
tree = ET.parse(os.path.join(self.lbPath, 'Data', 'Platforms.xml'))
root = tree.getroot()
for platformFolder in root.iter('PlatformFolder'):
mediaType = platformFolder.find('MediaType').text
platformName = platformFolder.find('Platform').text
if platformName not in artDirs:
artDirs[platformName] = {}
artDirs[platformName][mediaType] = platformFolder.find('FolderPath').text
return artDirs
def ScrapeAllPlatforms( self ):
""" """
count = 0
files = LB.GetLbPlatformFiles( self.lbPath)
for file in files:
platformName = LB.LbFilenameToPlatformName(file)
count += self.ScrapePlatform(platformName)
return count
def ScrapePlatform( self, LbPlatformName, SsPlatformId=None ):
"""
Returns number of items scraped
"""
mediaCount = 0
print("\nScraping: %s" % LbPlatformName)
if SsPlatformId is None:
try:
SsPlatformId = self.ssmap[LbPlatformName]
except:
print(" Unable to find ScreenScraperId.")
return mediaCount
platFileName = os.path.join(self.lbPath, 'Data', 'Platforms', LbPlatformName)+'.xml'
try:
tree = ET.parse(platFileName)
except:
if self.verbose:
print(" Unable to open LB platform file: '%s'"% platFileName)
return mediaCount
platArtDirs = self.artDirs[LbPlatformName]
root = tree.getroot()
for game in root.iter('Game'):
gamePath = game.find("ApplicationPath").text
gamePath = os.path.abspath(os.path.join(self.lbPath,gamePath))
gameFileName = os.path.splitext(os.path.basename(gamePath))[0]
gameTitle = game.find("Title").text
print(" --- %s ---" % gameTitle.encode('utf-8'))
# Search LB media directories for existing artwork
mediaNeeded = []
for mediaType in self.lbToSsMediaMap.keys():
foundMedia = False
ad = platArtDirs[mediaType]
if len(find_files(os.path.join(self.lbPath,ad),gameTitle+'*.*')) > 0:
foundMedia = True
elif len(find_files(os.path.join(self.lbPath,ad),gameFileName+'*.*')) > 0:
foundMedia = True
if foundMedia is False:
if self.useGameTitle:
fn = os.path.join(self.lbPath,ad,gameTitle)
else:
fn = os.path.join(self.lbPath,ad,gameFileName)
mediaNeeded.append((mediaType,fn))
if self.verbose:
print(" Missing a %s" % mediaType)
if len(mediaNeeded) > 0:
systemid = self.ssmap[LbPlatformName]
try:
ss = SS.GameInfo(systemId=systemid, romPath=gamePath, gameTitle=gameTitle, verbose=self.verbose, **self.ssparameters)
except SS.RomNotFoundError:
print(" Not found in ScreenScraper")
continue
availableMedia = ss.GetAvailableMedia()
for mediaToCheck in mediaNeeded:
url = None
# LB media directory may map to multipe SS types
for mediaType in self.lbToSsMediaMap[mediaToCheck[0]]:
if mediaType in availableMedia:
locale = availableMedia[mediaType].keys()[0]
if len(availableMedia[mediaType]) > 1:
# Find our preferred locale
for locale in self.ssLocalePreference:
if locale in availableMedia[mediaType]:
break
else:
print(" Did not find a preferred locale from list %s." % availableMedia[mediaType].keys())
url = availableMedia[mediaType][locale]['url']
if self.verbose:
print(" Getting %s (%s)!" % (mediaType,locale))
break
else:
# Didn't find what we needed, so move on to next
continue
ext = '.'+url.split('&mediaformat=')[1][:3]
filename = mediaToCheck[1]+ext
if os.path.exists(filename) is False:
print(" Saving: %s" % filename.encode('utf-8'))
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
f = open(mediaToCheck[1]+ext,'wb')
response = urllib2.urlopen(url)
f.write(response.read())
f.close()
mediaCount += 1
return mediaCount
###############################################################################
# GLOBAL FUNCTIONS
###############################################################################
def find_files(directory, pattern='*'):
try:
return [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(directory)
for f in fnmatch.filter(files, pattern)]
except:
return []
###############################################################################
# BASIC TESTS
###############################################################################
def main():
import settings
test = 2
verbose = False
# import ipdb; ipdb.set_trace()
lbss = LaunchBoxScreenScraper('..\LaunchBox', settings.devid, settings.devpassword, settings.softname, settings.ssid, settings.sspassword, verbose)
if test == 1:
# lbss.ScrapePlatform("Sega Dreamcast")
# lbss.ScrapePlatform("MAME")
# lbss.ScrapePlatform("Atari 2600")
# lbss.ScrapePlatform("Super Nintendo Entertainment System")
# lbss.ScrapePlatform("Microsoft MSX2")
lbss.ScrapePlatform("Nintendo GameCube")
if test == 2:
lbss.ScrapeAllPlatforms()
if __name__ == "__main__":
main()