Imgur logo
Imgur is a free image hosting service. Many people use it. Here is an example of an imgur link: https://i.sstatic.net/II8CA.png. Write a program that continually outputs random (valid) imgur links. For example, here is some sample output of my progam (not shown because it contains some tricks you will have to figure out yourself):
http://i.imgur.com/uFmsA.png
http://i.imgur.com/FlpHS.png
http://i.imgur.com/eAbsZ.png
http://i.imgur.com/lEUsq.png
http://i.imgur.com/RuveH.png
http://i.imgur.com/BoEwB.png
http://i.imgur.com/HVFGQ.png
http://i.imgur.com/PZpMg.png
http://i.imgur.com/DezCY.png
Helpful hints:
- When imgur was new, 5-letter links were used.
- When imgur was new, numbers weren't used.
- You can use this to your advantage: only find 5-letter link images with only letters. That is what my program does.
- Also, all images are saved as
.png
.
Requirements:
- Continually output random imgur links
- Links considered sufficiently "random" if 50 are outputted with no repeats
- When visited, links must be an image
- Links must start with
http://i.imgur.com/
and end with.png
- Score is amount of characters
I did it in Java (TERRIBLE for golfing) in 452 chars. Not shown here because it contains some tricks you will have to figure out for yourself!)
-
6\$\begingroup\$ Wait, is your definition of random that the output shouldn't contain repeats? \$\endgroup\$Mr Lister– Mr Lister2013年01月20日 09:23:25 +00:00Commented Jan 20, 2013 at 9:23
-
8\$\begingroup\$ Warning: pornographic images may be present on imgur! =:o \$\endgroup\$Mr.Wizard– Mr.Wizard2013年01月20日 22:52:13 +00:00Commented Jan 20, 2013 at 22:52
-
\$\begingroup\$ Are all 5-letter image valid? \$\endgroup\$l4m2– l4m22019年01月06日 10:51:04 +00:00Commented Jan 6, 2019 at 10:51
13 Answers 13
HTML (152)
<img src=x
onload=console.log(s);g()
onerror=g=function(){s='http://i.imgur.com/'+Math.random().toString(36).substr(2,6)+'.png';event.target.src=s};g()>
This logs all found images on the JavaScript console using console.log()
. Works in all tested browsers (Firefox, Chrome, IE9, Safari and Opera).
The fun part is that all sorts of funny images are flashing up for the blink of an eye :).
Try it! (jsFiddle wraps this into a more complete HTML page, but browsers also accept the single element.)
Props to the amazing random string method by doubletap!
Where can I see the JavaScript console and the logged images?
- Firefox: Press Control-Shift-K (Command-Option-K on the Mac). Unselect the Net, CSS and JS buttons there, only select the Logging button.
- Opera: Press Control+Shift+i, select the Console tab.
- Chrome: Press Control+Shift+i, select the Console tab. On the bottom, select Logs.
- Safari: Basically like Chrome, but make sure first that Safari's developer tools are activated. Then press Control+Alt+C (on Windows, not sure on the Mac) instead of Control+Shift+i, select the Console tab. On the bottom, select Logs.
- IE: Press F12, select the console tab.
-
\$\begingroup\$ This does not meet the requirements. Many of the images it pops up are not valid. \$\endgroup\$Doorknob– Doorknob2013年01月20日 13:10:47 +00:00Commented Jan 20, 2013 at 13:10
-
\$\begingroup\$ What do you mean by "not valid"? In the console, only images that could be successfully displayed are logged (see my updated description). \$\endgroup\$Thomas W.– Thomas W.2013年01月20日 14:56:59 +00:00Commented Jan 20, 2013 at 14:56
-
\$\begingroup\$ Oh, okay. Retrying now. Edit: Chrome seems to be logging the invalid images as well. \$\endgroup\$Doorknob– Doorknob2013年01月20日 14:57:33 +00:00Commented Jan 20, 2013 at 14:57
-
\$\begingroup\$ Chrome by default mixes error messages and logs. At the bottom of the console tab you have All, Errors, Warnings, Logs. If an image could not be loaded, it displays a 404 error with the image URL. If you select Logs, then only the output generated by my code should display. \$\endgroup\$Thomas W.– Thomas W.2013年01月20日 15:11:05 +00:00Commented Jan 20, 2013 at 15:11
-
3\$\begingroup\$ A lot of the logged image URLs just return the standard imgur error image that says "The image you are requesting does not exist or is no longer available." I suppose one could argue that those are still valid results under the rules as given, since the returned document is an image. \$\endgroup\$Ilmari Karonen– Ilmari Karonen2013年01月20日 23:22:31 +00:00Commented Jan 20, 2013 at 23:22
Perl (93 + 4 = 97)
Using imgur's own random mechanism to get their image URLs, which aren't png URLs most of the time:
$ perl -Mojo -E 'say+g("http://imgur.com/gallery/random")->dom->at("[rel=image_src]")->attrs("href")for+1..50'
http://i.imgur.com/7cNoA.jpg
...
(You need Mojolicious for this.)
-
1\$\begingroup\$ That's brilliant, and so not fair! \$\endgroup\$elssar– elssar2013年01月21日 16:53:01 +00:00Commented Jan 21, 2013 at 16:53
-
\$\begingroup\$ @elssar That's right, at least your second point. ;) \$\endgroup\$memowe– memowe2013年01月21日 17:49:27 +00:00Commented Jan 21, 2013 at 17:49
-
\$\begingroup\$ Oh, that's clever :) \$\endgroup\$Doorknob– Doorknob2013年01月22日 00:40:11 +00:00Commented Jan 22, 2013 at 0:40
PHP 5.4, 76 characters
URLs are generated in sequential order using only uppercase letters and never repeat, meeting the letter of the specification.
<?for($t=@ZZZZ;$t++;)file($u="http://i.imgur.com/$t.png")[0]>@F&&print"$u
";
Perl (87)
perl -pe's/\W//g;$_="http://i.imgur.com/$_.png\n";$_=""if`curl $_`=~/^</'</dev/urandom
Finding images with uppercase, lowercase and digits from 0 up to any characters, some day.
-
\$\begingroup\$ Hehehe, urandom \$\endgroup\$beary605– beary6052013年01月20日 14:48:43 +00:00Commented Jan 20, 2013 at 14:48
Mathematica, 108
White-space added.
While[True,
Import@# /. _Image :> Print@# &[
"http://i.imgur.com/" <> "a" ~CharacterRange~ "z" ~RandomChoice~ 5 <> ".png"
]
]
Python ((削除) 174 (削除ここまで) (削除) 158 (削除ここまで) 156)
I want shorter module names in Python. Also an easier method of getting random letters. :)
import urllib,random
while 1:a='http://i.imgur.com/%s.png'%''.join(chr(random.randint(65,90))for i in'AAAAA');print('File'not in urllib.urlopen(a).read())*a
Explanation:
The modulus operator on a string is the formatting command, in this case it replaces '%s' in the string with 5 random uppercase letters
a
is the website name (type str
)
('File'not in urllib.urlopen(a).read())
is True when 'File' (from 'File not found!') is not found in the the HTML of the URL. (type bool
)
bool
* str
= str
if bool
= True, so it will only output a
if 'File' is not found in the HTML code.
-
\$\begingroup\$ What part of the spec says you have to use both upper and lower case letters?
chr(random.randint(65,90))
\$\endgroup\$Peter Taylor– Peter Taylor2013年01月20日 08:49:56 +00:00Commented Jan 20, 2013 at 8:49 -
\$\begingroup\$ I ran it and it didn't output anything. Nice trick with
'File'
though :) \$\endgroup\$Doorknob– Doorknob2013年01月20日 13:14:01 +00:00Commented Jan 20, 2013 at 13:14 -
\$\begingroup\$ @PeterTaylor: :) I didn't know that there were any links on imgur with only uppercase letters, but I guess that would be obvious enough. Thanks! \$\endgroup\$beary605– beary6052013年01月20日 14:30:52 +00:00Commented Jan 20, 2013 at 14:30
-
\$\begingroup\$ @Doorknob: It outputs a newline unless a link is found, then it will give a link. \$\endgroup\$beary605– beary6052013年01月20日 14:57:01 +00:00Commented Jan 20, 2013 at 14:57
-
1\$\begingroup\$ Any sequence of length 5 will work for your 5 random characters, so you can shave a few characters off by using something like
'XXXXX'
rather thanrange(5)
. This also has the advantage of not requiring whitespace between it and thein
. \$\endgroup\$Strigoides– Strigoides2013年01月21日 01:33:16 +00:00Commented Jan 21, 2013 at 1:33
Bash (削除) (129, 121) (削除ここまで) (117, 109)
I've got two versions: an iterative and an endless recursive one (which will slowly eat up all memory). Both versions check if there actually is a PNG file present (jpg's, gif's and other file types are ignored).
Iterative(old) (129):
while true;do u=http://i.imgur.com/$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -s 2>&1|head -c4|grep PNG$ -q&&echo $u;done
Recursive(old) (121):
:(){ u=http://i.imgur.com/$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -s 2>&1|head -c4|grep PNG$ -q&&echo $u;:;};:
Note:
There might be a compatability issue with grep. My grep manual states that -s
silents grep's output but it does nothing. However, using --quiet
, --silent
or -q
instead works.
EDIT:
Using content headers now after reading https://codegolf.stackexchange.com/a/10499/7195 :)
Iterative (117):
while true;do u=http://i.imgur.com/$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -sI|grep image -q&&echo $u;done
Recursive (109):
:(){ u=http://i.imgur.com/$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -sI|grep image -q&&echo $u;:;};:
Python, (削除) 361 (削除ここまで) (削除) 355 (削除ここまで) (削除) 334 (削除ここまで) (削除) 332 (削除ここまで) (削除) 322 (削除ここまで) 314 characters
A little obfuscated, nothing too difficult. May result in unusually high density of cat pictures, you have been warned.
import json as j,urllib as o,time as t;a=0
while 1:
q="i.imgur";y,p=('data','children');r="njj";h="erqqvg.pbz/";u="uggc://"+h+"e/"+r;c=j.loads(o.urlopen(u.decode('rot13')+".json?sorted=new&after=%s"%a).read())[y]
for s in c[p]:
f=s[y];w=f['url'].strip('?1')
if w.find(q)!=-1:print w
a=c['after'];t.sleep(3)
Output:
http://i.imgur.com/u3vyMCW.jpg
http://i.imgur.com/zF7rPAf.jpg
http://i.imgur.com/aDTl7OM.jpg
http://i.imgur.com/KONVsYw.jpg
http://i.imgur.com/RVM2pYi.png
http://i.imgur.com/tkMhc9T.jpg
http://i.imgur.com/KxUrZkp.gif
http://i.imgur.com/mnDTovy.jpg
http://i.imgur.com/WpuXbHb.jpg
http://i.imgur.com/qZA3mCR.jpg
http://i.imgur.com/AxMS1Fs.png
http://i.imgur.com/TLSd571.jpg
http://i.imgur.com/VfMhLIQ.jpg
http://i.imgur.com/Wu32582.jpg
http://i.imgur.com/hrmQL2F.jpg
http://i.imgur.com/Clg8N.jpg
http://i.imgur.com/7Wsko.jpg
http://i.imgur.com/Rhb0UNx.jpg
http://i.imgur.com/LAXAf45.gif
http://i.imgur.com/jhOLJ9B.jpg
http://i.imgur.com/FQ9NeAl.jpg
http://i.imgur.com/oqzf6tE.jpg
http://i.imgur.com/rnpXs1A.jpg
http://i.imgur.com/DfUIz6k.jpg
http://i.imgur.com/orfGA5I.jpg
http://i.imgur.com/wBT7JNt.jpg
http://i.imgur.com/RycK1m2.jpg
http://i.imgur.com/7j21FIR.jpg
http://i.imgur.com/z2tVnNC.jpg
http://i.imgur.com/mnsAGuF.jpg
http://i.imgur.com/vIZM1NY.jpg
http://i.imgur.com/JT3XRI4.jpg
http://i.imgur.com/SNpwTmp.jpg
http://i.imgur.com/u9ynLb9.jpg
http://i.imgur.com/DrFWsBP.jpg
http://i.imgur.com/rU6oyup.jpg
http://i.imgur.com/XxBD5nl.jpg
http://i.imgur.com/d09qQzP.jpg
http://i.imgur.com/vvPSbqI.jpg
http://i.imgur.com/1hdfobQ.jpg
http://i.imgur.com/4LLC6Vs.jpg
http://i.imgur.com/RfasxO2.jpg
http://i.imgur.com/BBcpOos.jpg
http://i.imgur.com/zMH8mgG.jpg
http://i.imgur.com/7g8k2Ww.jpg
-
3\$\begingroup\$ All the images... are animals? There is some trickery going on here :P +1 because I like the cute animals :3 \$\endgroup\$Doorknob– Doorknob2013年01月22日 00:38:04 +00:00Commented Jan 22, 2013 at 0:38
R, 182 characters
library(httr);while(0<1){s=paste("http://i.imgur.com/",paste(sample(c(LETTERS,letters),5),collapse=""),".png",sep="");if(HEAD(s)$headers$'content-type'=='text/html')'' else print(s)}
Python, 153 chars
import string as s,random as w,requests as r
while 1:
t='http://i.imgur.com/%s.png'%''.join(w.sample(s.letters,5))
if'not'not in r.get(t).text:print t
While this works, it is bloody slow and might take many seconds before returning any output.
Inspired by @beary605's solution - he saved me a bunch of characters as I was planning to check for image by content-type header.
-
\$\begingroup\$
random.sample
:) That is a much better way of choosing characters. \$\endgroup\$beary605– beary6052013年01月21日 15:38:49 +00:00Commented Jan 21, 2013 at 15:38 -
\$\begingroup\$ Yep, amazing what Code golf will force you to Google, and learn. \$\endgroup\$elssar– elssar2013年01月21日 16:51:53 +00:00Commented Jan 21, 2013 at 16:51
Ruby (103 chars)
require"open-uri";loop{u="http://i.imgur.com/#{rand(1e9).to_s(36)[0,5]}.png";open u rescue next;puts u}
-
\$\begingroup\$ You have to add a
.png
to the end of the URL. \$\endgroup\$Doorknob– Doorknob2013年01月22日 00:36:52 +00:00Commented Jan 22, 2013 at 0:36 -
\$\begingroup\$ @Doorknob: Whoops, fixed \$\endgroup\$anonymous coward– anonymous coward2013年01月25日 08:42:28 +00:00Commented Jan 25, 2013 at 8:42
Bash/command-line tools, 72 chars
Borrowing @memowe's clever technique:
curl -sL http://imgur.com/gallery/random|grep e_sr|cut -d\" -f4;exec 0ドル
This achieves a continuous loop by re-execing itself within the same process space.
Jelly, 41 bytes
ØẠẊs5Ḣ
"http://i.imgur.com/";¢;".png"ṄƲÐḶ
How?
ØẠẊs5Ḣ : Helper Link
ØẠ : Upper and Lowercase Alphabet
Ẋ : Shuffle
s5 : Split Into Lengths of 5
Ḣ : Head; pop from front of list
-----------------------------------------------------------------------------------------
"http://i.imgur.com/";¢;".png"ṄƲÐḶ : Main Link
"http://i.imgur.com/" : String Literal
¢ : Previous link as a nilad.
".png" : String Literal
; ; : Concat
Ṅ : Print with a linefeed
Ʋ : Last four links as a monad
ÐḶ : Loop; Repeat until the results are no longer unique.