14
\$\begingroup\$

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!)

\$\endgroup\$
3
  • 6
    \$\begingroup\$ Wait, is your definition of random that the output shouldn't contain repeats? \$\endgroup\$
    – Mr Lister
    Commented Jan 20, 2013 at 9:23
  • 8
    \$\begingroup\$ Warning: pornographic images may be present on imgur! =:o \$\endgroup\$
    – Mr.Wizard
    Commented Jan 20, 2013 at 22:52
  • \$\begingroup\$ Are all 5-letter image valid? \$\endgroup\$
    – l4m2
    Commented Jan 6, 2019 at 10:51

13 Answers 13

5
\$\begingroup\$

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.
\$\endgroup\$
6
  • \$\begingroup\$ This does not meet the requirements. Many of the images it pops up are not valid. \$\endgroup\$
    – Doorknob
    Commented 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.
    Commented Jan 20, 2013 at 14:56
  • \$\begingroup\$ Oh, okay. Retrying now. Edit: Chrome seems to be logging the invalid images as well. \$\endgroup\$
    – Doorknob
    Commented 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.
    Commented 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\$ Commented Jan 20, 2013 at 23:22
4
\$\begingroup\$

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.)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ That's brilliant, and so not fair! \$\endgroup\$
    – elssar
    Commented Jan 21, 2013 at 16:53
  • \$\begingroup\$ @elssar That's right, at least your second point. ;) \$\endgroup\$
    – memowe
    Commented Jan 21, 2013 at 17:49
  • \$\begingroup\$ Oh, that's clever :) \$\endgroup\$
    – Doorknob
    Commented Jan 22, 2013 at 0:40
3
\$\begingroup\$

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
";
\$\endgroup\$
0
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Hehehe, urandom \$\endgroup\$
    – beary605
    Commented Jan 20, 2013 at 14:48
3
\$\begingroup\$

Mathematica, 108

White-space added.

While[True,
 Import@# /. _Image :> Print@# &[
  "http://i.imgur.com/" <> "a" ~CharacterRange~ "z" ~RandomChoice~ 5 <> ".png"
 ]
]
\$\endgroup\$
0
2
\$\begingroup\$

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.

\$\endgroup\$
11
  • \$\begingroup\$ What part of the spec says you have to use both upper and lower case letters? chr(random.randint(65,90)) \$\endgroup\$ Commented Jan 20, 2013 at 8:49
  • \$\begingroup\$ I ran it and it didn't output anything. Nice trick with 'File' though :) \$\endgroup\$
    – Doorknob
    Commented 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
    Commented 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
    Commented 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 than range(5). This also has the advantage of not requiring whitespace between it and the in. \$\endgroup\$
    – Strigoides
    Commented Jan 21, 2013 at 1:33
2
\$\begingroup\$

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;:;};:
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
  • 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
    Commented Jan 22, 2013 at 0:38
1
\$\begingroup\$

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)}
\$\endgroup\$
0
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ random.sample :) That is a much better way of choosing characters. \$\endgroup\$
    – beary605
    Commented Jan 21, 2013 at 15:38
  • \$\begingroup\$ Yep, amazing what Code golf will force you to Google, and learn. \$\endgroup\$
    – elssar
    Commented Jan 21, 2013 at 16:51
1
\$\begingroup\$

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}
\$\endgroup\$
2
  • \$\begingroup\$ You have to add a .png to the end of the URL. \$\endgroup\$
    – Doorknob
    Commented Jan 22, 2013 at 0:36
  • \$\begingroup\$ @Doorknob: Whoops, fixed \$\endgroup\$ Commented Jan 25, 2013 at 8:42
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.