36
6
xkcd is everyone's favorite webcomic, and you will be writing a program that will bring a little bit more humor to us all.
Your objective in this challenge is to write a program which will take a number as input and display that xkcd and its title-text (mousover text).
Input
Your program will take a positive integer as input (not necessarily one for which there exists a valid comic) and display that xkcd: for example, an input of 1500 should display the comic "Upside-Down Map" at xkcd.com/1500, and then either print its title-text to the console or display it with the image.
Due to their proximity across the channel, there's long been tension between North Korea and the United Kingdom of Great Britain and Southern Ireland.
Test case 2, for n=859:
Brains aside, I wonder how many poorly-written xkcd.com-parsing scripts will break on this title (or ;;"''{<<[' this mouseover text."
Your program should also be able to function without any input, and perform the same task for the most recent xkcd found at xkcd.com, and it should always display the most recent one even when a new one goes up.
You do not have to get the image directly from xkcd.com, you can use another database as long as it is up-to-date and already existed before this challenge went up. URL shortners, that is, urls with no purpose other than redirecting to somewhere else, are not allowed.
You may display the image in any way you chose, including in a browser. You may not, however, directly display part of another page in an iframe or similar. CLARIFICATION: you cannot open a preexisting webpage, if you wish to use the browser you have to create a new page. You must also actually display an image - outputting an image file is not allowed.
You can handle the case that there isn't an image for a particular comic (e.g. it is interactive or the program was passed a number greater than the amount of comics that have been released) in any reasonable way you wish, including throwing an exception, or printing out an at least single-character string, as long as it somehow signifies to the user that there isn't an image for that input.
You can only display an image and output its title-text, or output an error message for an invalid comic. Other output is not allowed.
This is a code-golf challenge, so the fewest bytes wins.
Is output aside from the image/alt-text allowed? i.e. the entire source of the page? – Luke – 2016-10-27T17:00:25.437
1@LukeFarritor You can only display the image and output the title text or output some form of error message for an invalid comic. – Pavel – 2016-10-27T17:01:45.807
9If your sample size is 1,
import antigravity
in Python ;) – Wayne Werner – 2016-10-27T18:11:20.59715
Funny fact
– Magic Octopus Urn – 2016-10-27T18:15:45.707n=404
http://xkcd.com/404 is a 404 page.Is simply finding and outputting the
<img src="" alt=""/>
to an HTML file okay? – Magic Octopus Urn – 2016-10-27T18:19:04.15311
xkcd is everyone's favorite webcomic
[Citation needed] – Sanchises – 2016-10-27T18:21:47.307@carusocomputing the source would have to be a file downloaded to your computer and the program would have to open the html file in a browser. – Pavel – 2016-10-27T18:22:50.467
11
Test case: 859
– betseg – 2016-10-27T18:24:08.210@betseg Lol, QUICK! Everyone use use
.eval()
statements. – Magic Octopus Urn – 2016-10-27T20:07:32.087If we can't open a pre-existing webpage, would it be cheating to download the HTML remotely into a local HTML file and open that? – JAL – 2016-10-27T20:10:42.577
@JAL you A) cannot display anything but the image and title text, and B) can only use files downloaded by the program to your computer as the src for img tags. – Pavel – 2016-10-27T20:43:57.993
Is it acceptable to print out the alt text after the image window is closed? – a spaghetto – 2016-10-27T23:13:08.603
@quartata Yes, that would be acceptable. – Pavel – 2016-10-27T23:28:12.927
Something that people may not be aware of but which may be relevant: there's an XKCD API.
– Mac – 2016-10-28T11:38:57.343@EriktheGolfer I just figured that I'd accept the current lowest answer and then change it if anyone comes up with a lower one. I'm fairly new here, and was not aware that that was how it was supposed work. – Pavel – 2016-10-28T22:40:19.460