Code clean

This commit is contained in:
2020-12-18 20:58:10 +01:00
parent ea71a178e1
commit 275ce360d3
3 changed files with 112 additions and 127 deletions
+32 -47
View File
@@ -1,15 +1,25 @@
#!/bin/bash
readonly SCRIPT_DIR="$(cd "$(dirname $0)" && pwd)"
readonly DATA_DIR="$SCRIPT_DIR/data"
readonly GAME_LIST_DIR="$SCRIPT_DIR/gamelists"
SYSTEMS="apple2 arcade atari2600 atari7800 atarilynx coleco gamegear gb gba gbc mastersystem megadrive msx n64 nes ngpc pcengine sega32x sg1000 snes vectrex virtualboy wonderswan wonderswancolor"
#check if there is any parameter
if [ "$#" -ne 1 ]; then
echo "You must enter a source file."
echo "You must enter a system name."
exit 0
fi
# check if the parameter is valid
if ! echo "$SYSTEMS" | grep -w "$1" > /dev/null; then
echo "You must enter a valid system name."
echo "Supported systemas are:"
for WORD in $SYSTEMS
do
echo -e "\t$WORD"
done
exit 0
fi
#check if file exists
if ! test -f "$1"; then
echo "$1 does not exists."
@@ -17,57 +27,32 @@ if ! test -f "$1"; then
fi
#variables
hashDir=data
gameListDir=gamelists
systemName="$(basename "$1" .html)"
echo "Processing $systemName."
tempFile1="${systemName}-1.tmp"
tempFile2="${systemName}-2.tmp"
urlList="${systemName}.url"
hashList="./${hashDir}/${systemName}.hashlist"
cookie="cookie.txt"
#update cookie time stamp
#sec=`date +%s`
#sec2=$((sec+600))
#sed -i 's/[0-9]\{10\}/'$sec2'/' cookie.txt
readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
readonly GAME_LIST_DIR="$SCRIPT_DIR/gamelists"
SYSTEM_NAME="$1"
URL_LIST="$SCRIPT_DIR/${SYSTEM_NAME}.url"
COOKIE_FILE="$SCRIPT_DIR/cookie.txt"
#process system html to get game url list
grep -E "Game/[1-9]'" -Eo $1 > $tempFile1
grep -E "Game/[1-9][0-9]'" -Eo $1 >> $tempFile1
grep -E "Game/[1-9][0-9][0-9]'" -Eo $1 >> $tempFile1
grep -E "Game/[1-9][0-9][0-9][0-9]'" -Eo $1 >> $tempFile1
grep -E "Game/[1-9][0-9][0-9][0-9][0-9]'" -Eo $1 >> $tempFile1
grep -E "Game/[1-9][0-9][0-9][0-9][0-9][0-9]'" -Eo $1 >> $tempFile1
sort -u $tempFile1 > $tempFile2
sed 's,Game/,,' $tempFile2 > $tempFile1
sed 's,^,https://retroachievements.org/linkedhashes.php?g=,' $tempFile1 > $tempFile2
sed 's,.$,,' $tempFile2 > $urlList
echo "Found $(wc -l < $urlList) games."
echo "Processing $SYSTEM_NAME."
grep -E "Game/[0-9]+" -Eo "$1" | sort -u | sed 's,Game/,https://retroachievements.org/linkedhashes.php?g=,' "$URL_LIST"
echo "Found $(wc -l < "$URL_LIST") games."
#create system folder for webpages with hashes
if test -d "./${gameListDir}/$systemName"; then
rm -rdf "./${gameListDir}/$systemName"
echo "Deleting ${gameListDir}/$systemName folder."
if test -d "./${GAME_LIST_DIR}/$SYSTEM_NAME"; then
rm -rdf "./${GAME_LIST_DIR}/$SYSTEM_NAME"
echo "Deleting ${GAME_LIST_DIR}/$SYSTEM_NAME folder."
fi
if ! test -d "./${gameListDir}/$systemName"; then
mkdir -p "./${gameListDir}/$systemName"
echo "Create ${gameListDir}/$systemName folder."
if ! test -d "./${GAME_LIST_DIR}/$SYSTEM_NAME"; then
mkdir -p "./${GAME_LIST_DIR}/$SYSTEM_NAME"
echo "Create ${GAME_LIST_DIR}/$SYSTEM_NAME folder."
fi
#download webpages with hashes form url list
echo "Downloading game info ..."
wget --random-wait --no-verbose --load-cookies ${cookie} -i $urlList -P "./${gameListDir}/$systemName"
wget --random-wait --no-verbose --load-COOKIE_FILEs "${COOKIE_FILE}" -i "${URL_LIST}" -P "./${GAME_LIST_DIR}/${SYSTEM_NAME}"
#remove tempfiles
if test -f "$tempFile1"; then
rm "$tempFile1"
fi
if test -f "$tempFile2"; then
rm "$tempFile2"
fi
if test -f "$urlList"; then
rm "$urlList"
if test -f "$URL_LIST"; then
rm "$URL_LIST"
fi