Last active
August 11, 2018 21:56
-
-
Save stt/a328ba4c5413a1d9e4dd3db3cc4bf85d to your computer and use it in GitHub Desktop.
Quick script to generate retroachievements capable playlist for retroarch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ra_cheevo_lpl.py | |
# 2018, <samuli@tuomola.net> | |
# | |
from urllib.request import Request, urlopen | |
from lxml import html | |
import os | |
def read_lpl(fp): | |
rl = open(fp).readlines() | |
return [rl[i:i+6] for i in range(0,len(rl),6)] | |
def get_ra_gamelist(ra_id): | |
gdoc = html.parse(urlopen('https://retroachievements.org/gameList.php?c=%i' % ra_id)).getroot() | |
return list(map(lambda e: e.text_content().strip(), gdoc.xpath('//div[@class="largelist"]//div[@class="bb_inline"]'))) | |
def find_entry_for_game(lpl, gamename): | |
return list(filter(lambda e: e[1].startswith(gamename), lpl)) | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) != 4 or not os.path.exists(sys.argv[2]): | |
print('Syntax: %s RA_ID source.lpl target.lpl' % sys.argv[0]) | |
print(" RA_ID: check retroachievements.org, e.g 3=SNES") | |
print(" source.lpl: playlist to check for games that might have cheevos") | |
print(" target.lpl: when existing game with cheevo is found it's written here") | |
sys.exit(1) | |
ra_id = int(sys.argv[1]) | |
lpl = read_lpl(sys.argv[2]) | |
if len(lpl) < 1: | |
print("Error: failed to load source playlist") | |
sys.exit(1) | |
ra_games = get_ra_gamelist(ra_id) | |
if len(ra_games) < 1: | |
print("Error: failed to load cheevo list") | |
sys.exit(1) | |
c_dups = 0 | |
c_misses = 0 | |
c_ok = 0 | |
debug = os.getenv('DEBUG') == '1' | |
with open(sys.argv[3], 'w') as fh: | |
for gn in ra_games: | |
ge = find_entry_for_game(lpl, gn) | |
if len(ge) > 1: | |
if debug: print('WARN: more than one game found for %s' % gn) | |
c_dups += 1 | |
elif len(ge) == 0: | |
if debug: print('WARN: no game found for %s' % gn) | |
c_misses += 1 | |
else: | |
fh.writelines(ge[0]) | |
c_ok += 1 | |
print("Tried matching %i games with cheevos to %i games in your playlist" % | |
(len(ra_games), len(lpl)) ) | |
if c_dups: print("issue: Found %i games that matches multiple games in your playlist" % c_dups) | |
if c_misses: print("issue: %i games with cheevos were not found in your playlist" % c_misses) | |
print("ok: %i games with cheevos were copied to the new playlist" % c_ok) | |
if not debug: print("re-run with DEBUG=1 envvar to see which games didn't match") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment