From 20bbdb7d638345f922e870ea5ebe576ff71659c0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Einar=20J=C3=B8rgen=20Haraldseid?= Date: Sun, 1 Jun 2014 23:18:21 +0200 Subject: [PATCH 1/1] My take on The imgur game --- imgurgame.bash | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 imgurgame.bash diff --git a/imgurgame.bash b/imgurgame.bash new file mode 100755 index 0000000..66bbe4e --- /dev/null +++ b/imgurgame.bash @@ -0,0 +1,39 @@ +#!/bin/bash +# Because why not? curl must be installed + +readonly BROWSER="google-chrome" + +# Create a random imgur url that may or may not be valid +imgururl() { + # 1/3% chance for length of 5, 6 or 7 length string + STRLN=$((5 + ${RANDOM} % 3)) + local ID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${STRLN} | head -n 1) + echo "http://i.imgur.com/${ID}.jpg" +} + +# Test if we found a valid URL +testimgur() { + local URL=${1} + local RESULT=$(curl -s -o /dev/null -I -w "%{http_code}" ${URL}) + if [[ ${RESULT} == 200 ]]; then + echo true + else + echo false + fi +} + +main() { + local FAIL=0 + while true; do + local URL=$(imgururl) + if [[ $(testimgur ${URL}) == "true" ]]; then + echo "Found ${URL} after ${FAIL} failed tries" + ${BROWSER} ${URL} > /dev/null 2>&1 + local FAIL=0 + else + FAIL=$(( ${FAIL}+1 )) + fi + done +} + +main -- 2.30.2