Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Imgur logo

Imgur is a free image hosting service. Many people use it. Here is an example of an imgur link: http://i.imgur.com/T55muMD.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!)

share|improve this question
4  
Wait, is your definition of random that the output shouldn't contain repeats? –  Mr Lister Jan 20 '13 at 9:23
4  
Warning: pornographic images may be present on imgur! =:o –  Mr.Wizard Jan 20 '13 at 22:52
add comment

12 Answers

up vote 3 down vote accepted

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
";
share|improve this answer
    
This does not meet the requirements. Many of the URLs it generates are not valid. –  Doorknob Jan 20 '13 at 13:11
    
@Doorknob: Which ones? I checked the first few, and all links were valid. –  Thomas W. Jan 20 '13 at 17:43
    
Doesn't this just print i.imgur.com/AAAA.png, i.imgur.com/AAAB.png, etc.? EDIT: oh wait never mind. I just skimmed the code :P Upvote then! –  Doorknob Jan 21 '13 at 0:07
add comment

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.
share|improve this answer
    
This does not meet the requirements. Many of the images it pops up are not valid. –  Doorknob Jan 20 '13 at 13:10
    
What do you mean by "not valid"? In the console, only images that could be successfully displayed are logged (see my updated description). –  Thomas W. Jan 20 '13 at 14:56
    
Oh, okay. Retrying now. Edit: Chrome seems to be logging the invalid images as well. –  Doorknob Jan 20 '13 at 14:57
    
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. –  Thomas W. Jan 20 '13 at 15:11
1  
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. –  Ilmari Karonen Jan 20 '13 at 23:22
show 1 more comment

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.

share|improve this answer
    
Hehehe, urandom –  beary605 Jan 20 '13 at 14:48
add comment

Mathematica, 108

White-space added.

While[True,
 Import@# /. _Image :> Print@# &[
  "http://i.imgur.com/" <> "a" ~CharacterRange~ "z" ~RandomChoice~ 5 <> ".png"
 ]
]
share|improve this answer
add comment

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

share|improve this answer
    
That's brilliant, and so not fair! –  elssar Jan 21 '13 at 16:53
    
@elssar That's right, at least your second point. ;) –  memowe Jan 21 '13 at 17:49
    
Oh, that's clever :) –  Doorknob Jan 22 '13 at 0:40
add comment

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.

share|improve this answer
    
What part of the spec says you have to use both upper and lower case letters? chr(random.randint(65,90)) –  Peter Taylor Jan 20 '13 at 8:49
    
I ran it and it didn't output anything. Nice trick with 'File' though :) –  Doorknob Jan 20 '13 at 13:14
    
@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! –  beary605 Jan 20 '13 at 14:30
    
@Doorknob: It outputs a newline unless a link is found, then it will give a link. –  beary605 Jan 20 '13 at 14:57
1  
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. –  Strigoides Jan 21 '13 at 1:33
show 10 more comments

Unique Python approach. 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
share|improve this answer
2  
All the images... are animals? There is some trickery going on here :P +1 because I like the cute animals :3 –  Doorknob Jan 22 '13 at 0:38
add comment

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)}
share|improve this answer
add comment

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 http://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;:;};:
share|improve this answer
add comment

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}
share|improve this answer
    
You have to add a .png to the end of the URL. –  Doorknob Jan 22 '13 at 0:36
    
@Doorknob: Whoops, fixed –  Charlie Somerville Jan 25 '13 at 8:42
add comment

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.

share|improve this answer
    
random.sample :) That is a much better way of choosing characters. –  beary605 Jan 21 '13 at 15:38
    
Yep, amazing what Code golf will force you to Google, and learn. –  elssar Jan 21 '13 at 16:51
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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