1
\$\begingroup\$

The new xkcd feed bot room gave me an idea for a simple challenge.

Your code will have to monitor the xkcd website and output all the new comics to the user.

Specs

  • When first run it should just sit around waiting.
  • Then, when randall posts a new comic, within a minute, it should display the new comic in any of our usual standards for images (display, save, etc.)
  • If the comic is interactive and not a simple image, then print out the string "interactive comic".
  • This does not include the comics which are an image inside a link, those still have to be displayed normally.
  • After displaying the image, it should go back to waiting around for the next update.
  • Your program cannot access any sites except http://xkcd.com

This is , so shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ Link to the feed bot room: chat.stackexchange.com/rooms/44753/xkcd \$\endgroup\$ – TuxCrafting Aug 31 '16 at 18:40
  • \$\begingroup\$ "If the comic is interactive and not a simple image" then how do we know? At present this seems underspecified. \$\endgroup\$ – Peter Taylor Aug 31 '16 at 18:46
  • \$\begingroup\$ A decent strategy would be to only check during the two to three hours around midnight eastern US time on M/W/F, but that would miss those comics he occasionally posts on other days (like April Fools and those week-long series he did in the past). \$\endgroup\$ – Mego Aug 31 '16 at 18:47
  • \$\begingroup\$ @PeterTaylor If it's a single image, there will be a permalink to the image on the page. If not, there won't be. You could also test to see if the main content pane contains an <img> tag. \$\endgroup\$ – Mego Aug 31 '16 at 18:48
  • \$\begingroup\$ @PeterTaylor there will be an image tag in the correct position, not an iframe or whatever else he uses \$\endgroup\$ – Maltysen Aug 31 '16 at 18:51
4
\$\begingroup\$

PowerShell v2+ & Default Browser, 215 217 bytes

$l=date;for(){if(($x=([xml](iwr xkcd.com/rss.xml)).rss.channel.item[0]).pubdate-ge$l){if(($y=$x.description)-match'img src'){saps ($y-replace'<img src="'-replace'" .*')}else{echo "interactive comic"}}$l=date}

WARNING: Running this as-is will likely make Randall very mad at you, due to the high frequency of calls to the RSS feed ... so, y'know, don't do that. (See below for an alternate version that can be used in a one-off manner)

Sets the $last time we displayed a comic to the result of Get-Date. Then, enter an infinite for loop.

Each iteration, we iwr (alias for Invoke-WebRequest) the RSS feed for the site. That's parsed as [xml] (yes, PowerShell can cast as XML), and the .rss.channel.item[0] thereof (i.e., the newest) is stored in $x. If the .pubDate is -ge$l (i.e., newer than the last comic we showed), we have a new comic to display.

So, we first check the .description field (saved in $y), to see if it matches img src. If so, we parse out the img from $y, and feed that to saps (alias for Start-Process). Since it's a URI, this relies on Windows' default behavior for "executing" a URI, that is, it opens the default browser to that location. (If this is too cheaty, let me know and we can iwr down the actual image and save it.)

Otherwise, we echo out that we've got an interactive comic.

In either case, we update our $l date to "now" since we just displayed a comic.

Saved 7 bytes thanks to Matt for reminding me that http:// can be implied


"Safe" version that executes just once and displays the latest comic

$l=(date).AddDays(-3);if(($x=([xml](iwr http://xkcd.com/rss.xml)).rss.channel.item[0]).pubdate-ge$l){if(($y=$x.description)-match'img src'){saps ($y-replace'<img src="'-replace'" .*')}else{echo "interactive comic"}}

You can run this one safely to see how the logic works. It'll only execute once and only display the latest comic.

\$\endgroup\$
  • \$\begingroup\$ Why did you use echo here? echo "interactive comic"? Or am I not seeing the nested parentheses and you are capturing data somewhere else? Also iwr xkcd.com/rss.xml seems to work \$\endgroup\$ – Matt Aug 31 '16 at 23:40
  • \$\begingroup\$ @Matt When doing testing locally, I was getting inconsistent results without an explicit echo -- sometimes the output wouldn't update until the next time the XML updated, and then two "interactive comic"s would be output simultaneously. -- Thanks for the golf on http://, I keep forgetting that can be implied. \$\endgroup\$ – AdmBorkBork Sep 1 '16 at 12:25
  • \$\begingroup\$ I'd like to point out that you could probably DDoS xkcd with this. \$\endgroup\$ – Mathime Sep 1 '16 at 18:47
  • \$\begingroup\$ @Mathime I don't know if it'd reach DDoS levels, but it would definitely be a Bad Thing™, hence my disclaimer. \$\endgroup\$ – AdmBorkBork Sep 1 '16 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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