Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added rom cover support #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.gen
*.nes
*.sms
*.gb
*.gbc
*.gg
*.snes
*.nds
*.png
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The boxarts are compatible with [TWiLightMenu++](https://github.com/RocketRobz/T

## Requirements

To run, `getcover` requires `awk`, `wget` and `xxd`.
To run, `getcover` requires `awk`, `wget`, `xxd`, `sed` and `crc32`.

-----

Expand Down
112 changes: 112 additions & 0 deletions getcover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/python3

import requests
import binascii
import os
import sys


def searchcover(rom):
baseurl = "https://art.gametdb.com/ds/coverS/"
langList = ['EN/', 'US/', 'JA/', 'DE/', 'KO/']
filename = tid + '.png'
for lang in langList:
if not nds_rom:
baseurl = 'https://github.com/DefKorns/rom-cover-generator/raw/master/boxart/'
lang = ''

url = baseurl + lang + filename

r = requests.get(url)
if r.status_code == 200:
if not nds_rom:
filename = rom_title + rom_extension + '.png'

with open(filename, 'wb') as f:
f.write(r.content)
if not nds_rom:
continue
else:
break


def gettid(romfile):
if os.path.isfile(romfile):
with open(romfile, 'rb') as f:
f.seek(12)
return f.read(4).decode("utf-8")


def romCRC32(romfile):
with open(romfile, "rb") as f:
buf = (binascii.crc32(f.read()) & 0xFFFFFFFF)
return "%08X" % buf


def rename(old_file, new_file):
if os.path.isfile(old_file):
os.rename(old_file, new_file)


def remove(filetodelete):
if os.path.isfile(filetodelete):
os.remove(filetodelete)


def valid_arg(arg, multilist):
isValid = any(arg in sublist for sublist in multilist)
return isValid


def help():
print("\nUsage: \n python ", os.path.basename(
__file__), "titleID | ndsfile [titleID] | ndsfile | files | [options]...")
print("\nFiles: \n Rom files of type: \"nes\", \"snes\", \"gb\", \"gbc\", \"sms\", \"gen\", \"gg\", \"md\", \"nds\"")
print("\nOptions: \n -?, -h, -help This information screen")
print(" md Convert Mega Drive roms \".md\" to nds compatible roms \".gen\" and gets it's artwork")
print(" -png Optimize png files")
print("\nExamples: \n python ", os.path.basename(
__file__), ".nes Rename matching title \"png\" files to the rom CRC")
print(" python ", os.path.basename(
__file__), "-? Display help info")
print(" python ", os.path.basename(
__file__), "-png Compress \"png\" files")
sys.exit()


if len(sys.argv) == 1:
help()

for arg in sys.argv:
helpArgs = ['help', '-h', '-?']
romArgs = ['snes', 'gbc', 'nes', 'gb', 'sms', 'gen', 'gg', 'md']
allRoms = ['all', '-a', '*.*']
arg_list = [helpArgs, romArgs, 'nds', allRoms]

if not valid_arg(arg, arg_list):
help()

if arg in helpArgs:
help()

if arg == 'nds':
nds_rom = True
for romfile in os.listdir("."):
if romfile.endswith('.'+arg):
tid = gettid(romfile)
searchcover(tid)

if arg in romArgs:
nds_rom = False
for romfile in os.listdir("."):
if romfile.endswith('.' + arg):
tid = romCRC32(romfile)
rom_title, rom_extension = os.path.splitext(romfile)
if arg == 'md':
rom_extension = 'gen'
rename(romfile, rom_title + rom_extension)
searchcover(tid)

if arg in allRoms:
print('soon')
sys.exit()
124 changes: 102 additions & 22 deletions getcover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

checkdeps() {
local deps
deps="awk wget xxd"
# on windows git bash must download:
# CRC32: http://esrg.sourceforge.net/utils_win_up/md5sum/crc32.exe to "C:\Program Files\Git\usr\bin"
# WGET: https://gist.github.com/evanwill/0207876c3243bbb6863e65ec5dc3f058
deps="awk wget xxd crc32 sed"
unameOut="$(uname -s)"
case "${unameOut}" in
CYGWIN* | MINGW*)
OS=Windows
;;
esac

for dep in $deps; do
if [ -z "$(command -v "$dep")" ]; then
Expand All @@ -17,24 +26,31 @@ searchcover() {
local lid
local langs
baseurl="https://art.gametdb.com/ds/coverS"
lid="EN"
langs=(US JA DE FR KO)

if ! getcover "$baseurl/$lid/$1.png"; then
for i in "${langs[@]}"; do
lid="$i"
getcover "$baseurl/$lid/$1.png"
status=$?
# exits for loop on first success preventing failed message per array language
if [ "${status:-0}" -eq 0 ]; then
break
fi
done

if [ "$romtype" = "nds" ]; then
lid="EN"
langs=(US JA DE FR KO)

if ! getcover "$baseurl/$lid/$1.png"; then
for i in "${langs[@]}"; do
lid="$i"
getcover "$baseurl/$lid/$1.png"
status=$?
# exits for loop on first success preventing failed message per array language
if [ "${status:-0}" -eq 0 ]; then
break
fi
done
fi
else
# baseurl="http://hakchicloud.com/Hakchi_Themes/dsart"
baseurl="https://github.com/DefKorns/rom-cover-generator/raw/master/boxart/"
getcover "$baseurl/$tid.png"
fi

# file not found or no connection available
# "${status:-0}" fix [: -ne: unary operator expected and [: : integer expression expected
if [ "${status:-0}" -ne 0 ]; then
if [ "${status:-0}" -ne 0 ]; then
echo "$1: cover download failed." 1>&2
fi
}
Expand All @@ -47,27 +63,91 @@ gettid() {

getcover() {
# '-nc' ||'--no-clobber', overwrites the file instead of creating multiple files if they already exits
wget -q -nc --compression=auto "$1"
if [ "$romtype" = "nds" ]; then
wget -q -nc --compression=auto "$1"
else
if [ $romtype = "md" ]; then
romtype="gen"
fi
wget -q -nc --compression=auto "$1" -O "$title.$romtype.png"
fi
}

rename() {
if [ -f "$1" ]; then
mv "$1" "$2"
fi
}

help() {
echo ""
echo "Usage:
./$(basename "$0") titleID | ndsfile [titleID] | ndsfile | files | [options]..."
echo ""
echo "Files:
Rom files of type: \"*.nes\", \"*.snes\", \"*.gb\", \"*.gbc\", \"*.sms\", \"*.gen\", \"*.gg\""
echo ""
echo "Options:
-?, -h, -help This information screen
game_name.nds Downloads boxart for a expecific NDS game
titleID Same as above
*.* Downloads boxart to all roms on the directory
*.(extension) Downloads boxart for supported roms on the directory"
echo ""
echo "Examples:
./$(basename "$0") *.nds Downloads boxart for all NDS games
./$(basename "$0") Mario Bros.nds Downloads boxart by game title
./$(basename "$0") AMCE Download boxart by titleID (this case \"Mario Kart DS\")
./$(basename "$0") *.nes Downloads boxart for NES roms on the directory
./$(basename "$0") *.snes Downloads boxart for SNES roms on the directory
./$(basename "$0") *.md Downloads boxart for Mega Drive and also converts the roms to *.gen
./$(basename "$0") -? Display help info"
echo ""
exit 1
}

checkdeps

if [ $# -lt 1 ]; then
echo "Usage: $(basename "$0") titleID | ndsfile [titleID | ndsfile ...]"
exit 1
help
fi

for i in "$@"; do
if [[ "$i" == *.nds ]]; then
case $i in
*.nds)
romtype="nds"
tid=$(gettid "$i")

if [ -z "$tid" ]; then
echo "$i: not a .nds file"
continue
fi
else
tid="$(echo "$i" | awk '{print toupper($0)}')"
fi
;;
*.nes | *.gbc | *.snes | *.gb | *.sms | *.gen | *.gg | *.md)
fulltitle=$(basename -- "$i")
romtype="${fulltitle##*.}"
title="${fulltitle%.*}"

if [[ "$i" == *.md ]]; then
rename "$i" "$title.gen"
i="$title.gen"
fi

tid=$(crc32 "$i")
if [ $OS = "Windows" ]; then
tid=$(crc32 "$i" | sed -e 's/^0x\(.\{8\}\).*/\1/')
fi
;;
*.*)
romfamily=(*.nes *.gbc *.snes *.gb *.sms *.gen *.gg *.md)
for ext in "${romfamily[@]}"; do
i="$ext"
done
;;
'-?' | -h | -help)
help
;;
esac

searchcover "$tid"
done