-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomitch.sh
147 lines (127 loc) · 3.93 KB
/
randomitch.sh
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
#!/bin/bash
#
# randomitch.sh
#
# Terrible Coding done by:
#
# DragonStuff @DragonStuff
#
#### CONSTANTS ####
USAGE="Usage: ./randomitch.sh [genre] [browser]\n Standard Genres: Free/Newest\n Specific Genres: Action/Platformer/Shooter/Adventure/RPG/Simulation/Strategy/Other/Puzzle\n"
#### FLAGS ####
#All flags are = 0 for on
#Quiet mode
QUIET=1
#### GLOBAL VARS ####
echo Free="http://itch.io/games/price-free.xml" > types.txt
echo Newest="http://itch.io/games/newest/price-free.xml" >> types.txt
# Genres
echo Action="http://itch.io/games/genre-action/price-free.xml" >> types.txt
echo Platformer="http://itch.io/games/genre-platformer/price-free.xml" >> types.txt
echo Shooter="http://itch.io/games/genre-shooter/price-free.xml" >> types.txt
echo Adventure="http://itch.io/games/genre-adventure/price-free.xml" >> types.txt
echo RPG="http://itch.io/games/genre-rpg/price-free.xml" >> types.txt
echo Simulation="http://itch.io/games/genre-simulation/price-free.xml" >> types.txt
echo Strategy="http://itch.io/games/genre-strategy/price-free.xml" >> types.txt
echo Other="http://itch.io/games/genre-other/price-free.xml" >> types.txt
echo Puzzle="http://itch.io/games/genre-puzzle/price-free.xml" >> types.txt
# Browser Variable to Global
Browser=$2
#### FUNCTIONS ####
function echoerr {
if [[ ! ${QUIET} = 0 ]]; then
if [[ ! -z ${TERM} ]]; then
tput setaf 1
fi
echo "ERROR: "${@} 1>&2
if [[ ! -z ${TERM} ]]; then
tput sgr0
fi
fi
}
function echowarn {
if [[ ! ${QUIET} = 0 ]]; then
if [[ ! -z ${TERM} ]]; then
tput setaf 3
fi
echo "WARNING: "${@}
if [[ ! -z ${TERM} ]]; then
tput sgr0
fi
fi
}
function echosucc {
if [[ ! ${QUIET} = 0 ]]; then
if [[ ! -z ${TERM} ]]; then
tput setaf 2
fi
echo ${@}
if [[ ! -z ${TERM} ]]; then
tput sgr0
fi
fi
}
# Variables
#@param:
# $1 Genre
# $2 Browser
function randomGame() {
local f=$1
rm -f database_raw.txt
# Check if the genre even exists
if [ -n "$(cat types.txt | grep $f | sed 's/^$f\=//')" ]; then
wget -q -O database_raw.txt $(cat types.txt | grep $f | sed "s/^$f\=//")
else
echo "This genre does not exist!"
exit
fi
# XML Prettifier - Works perfectly!
FILE_SIZE=$(du -k database_raw.txt | cut -f1)
TMPNAME=$(basename $0)-$(basename database_raw.txt)
TMPFILE=$(mktemp $TMPNAME.XXX) || exit 1
echo "Here are the games we can choose from in this genre:"
xmllint --format database_raw.txt --output $TMPFILE
mv $TMPFILE database_raw.txt
# Now replace the database with links
cat database_raw.txt | grep "<link>" | sed 's|</b>|-|g' | sed 's|<[^>]*>||g' > database_parsed.txt
# Remove first line as it is a link of the actual page:
tail -n +2 "database_parsed.txt"
# It appears that the XML pages display 30 games, so let's generate a random number less than or equal to this.
elementNumber=$(shuf -i 1-30 -n 1)
# Now grab the Game($elementNumber)
gameID=$(awk "NR==$elementNumber" database_parsed.txt)
# Then call openGameInBrowser(url)
# UNCOMMENT BELOW TO OPEN BROWSER!
# openGameInBrowser($url);
echo -e YOUR GAME IS: $gameID
}
function openGameInBrowser() {
# TODO (DragonStuff): Check whether the site is actually operating or not. Whatever.
local f=$1
# Check for existance of browser and open the game
command -v $Browser >/dev/null && $Browser $f || echo "Browser: $Browser not found."
}
#### GETOPTS ####
while getopts ":q" opt; do
case $opt in
q)
QUIET=0
;;
*)
echoerr ${USAGE}
exit 1
;;
esac
done
#### MAIN ####
function main {
#shift after reading getopts
shift $(($OPTIND - 1))
#no args after flags, present usage message
if [[ ${#@} = 0 ]]; then
echo -e ${USAGE}
exit 1
fi
randomGame $1
}
main "${@}"