Created
April 19, 2014 07:49
-
-
Save 1995eaton/11077222 to your computer and use it in GitHub Desktop.
A puu.sh image searcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toBase(c, s) { | |
var l = c.length, | |
_ret = ""; | |
while (s !== 0) { | |
_ret += c[s % l]; | |
s = Math.floor(s / l); | |
} | |
return _ret.split("").reverse().join(""); | |
} | |
function fromBase(c, n) { | |
n = n.split("").reverse().join(""); | |
for (var i = 0, p = n.length, l = c.length, _acc = 0; i < p; ++i) { | |
_acc += Math.pow(l, i) * c.indexOf(n[i]); | |
} | |
return _acc; | |
} | |
var c = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(""); | |
var _c = c.length; | |
function randomId() { | |
var s = (~~(Math.random() * 3) + 6).toString(); // Image Ids seem to always start with 7, 8, or 9 | |
for (var i = 0; i < 4; ++i) { | |
s += c[~~(Math.random() * _c)]; | |
} | |
return s; | |
} | |
document.body.innerHTML = ""; | |
document.body.style.textAlign = "center"; | |
document.body.style.backgroundColor = "#1b1d1e"; | |
document.body.style.color = "#8f8f8f"; | |
document.body.style.fontFamily = "Helvetica Neue, Helvetica, Neue, Sans, Arial"; | |
var streak = 0; | |
var idValues = { | |
low: 0, | |
high: 0, | |
initial: randomId() | |
}; | |
function getPuush(id, reverse) { | |
var h1 = document.createElement("h1"); | |
h1.innerText = id; | |
var img = new Image(); | |
img.onload = function() { | |
if (img.width > window.innerWidth * 0.8) { | |
img.style.width = "80%"; | |
img.style.height = "auto"; | |
} | |
document.body.appendChild(h1); | |
document.body.appendChild(img); | |
streak++; | |
setTimeout(function() { | |
return getPuush(toBase(c, fromBase(c, id) + (reverse ? -1 : 1)), reverse); | |
}, 500); | |
}; | |
img.onerror = function() { | |
if (fromBase(c, id) < fromBase(c, idValues.initial)) { | |
idValues.low = id; | |
h1.innerHTML = id + " – Image not found! – continuing search upwards"; | |
document.body.appendChild(h1); | |
return getPuush(toBase(c, fromBase(c, idValues.initial) + 1), false); | |
} | |
if (idValues.low === 0) { | |
h1.innerHTML = "Game over, no images were found"; | |
} else { | |
h1.innerHTML = "Game over, your streak was " + streak + " and contained the range of numbers " + idValues.low + " to " + id; | |
} | |
document.body.appendChild(h1); | |
}; | |
img.src = "http://puu.sh/" + id + ".jpg"; | |
} | |
getPuush(idValues.initial, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment