check_weechat - a small script to keep weechat alive, suitable for crontab
[einar-bin] / imgurgame.bash
1 #!/bin/bash
2 # Because why not? curl must be installed
3
4 readonly BROWSER="google-chrome"
5
6 # Create a random imgur url that may or may not be valid
7 imgururl() {
8 # 1/3 chance for length of 5, 6 or 7 length string
9 STRLN=$((5 + ${RANDOM} % 3))
10 local ID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${STRLN} | head -n 1)
11 echo "http://i.imgur.com/${ID}.jpg"
12 }
13
14 # Test if we found a valid URL
15 testimgur() {
16 local URL=${1}
17 local RESULT=$(curl -s -o /dev/null -I -w "%{http_code}" ${URL})
18 if [[ ${RESULT} == "200" ]]; then
19 echo true
20 else
21 echo false
22 fi
23 }
24
25 main() {
26 local FAIL=0
27 while true; do
28 local URL=$(imgururl)
29 if [[ $(testimgur ${URL}) == "true" ]]; then
30 echo "Found ${URL} after ${FAIL} failed tries"
31 ${BROWSER} ${URL} > /dev/null 2>&1
32 local FAIL=0
33 else
34 ((FAIL++))
35 fi
36 done
37 }
38
39 main