Skip to content

Instantly share code, notes, and snippets.

@karel1980
Last active December 19, 2015 01:19
Show Gist options
  • Save karel1980/5874870 to your computer and use it in GitHub Desktop.
Save karel1980/5874870 to your computer and use it in GitHub Desktop.
Host your own dilbert feed (image only) 1. slap this gist in a crontab 2. host the feed.xml file It's probably not legal to do this, so don't. I just wanted to make a point.
require 'nokogiri'
require 'open-uri'
def get_image_url()
# Find the url for today's dilbert comic on the home page
doc = Nokogiri::HTML(open("http://www.dilbert.com/"))
doc.css('div.STR_Image img').attr('src')
end
def update_feed(image_url)
if !File.exists? 'feed.xml'
File.open('feed.xml', 'w') do |f|
f.write(<<eos
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Dilbert Daily Strip</title>
<link>http://dilbert.com</link>
</channel>
</rss>
eos
)
end
end
# Open the feed
feed = Nokogiri::XML(File.open("feed.xml"))
channel = feed.root.children.first
# Check if this is a new entry
if feed.xpath('//item/link').map{|e|e.text}.include? image_url.text
puts "Not a new comic: ", image_url
return
end
# Add the new entry
add_item(image_url, feed)
# Keep only the last 10 entries - a bit clumsy
feed.search("//item")[0..-11].each{|n|n.remove}
# Save the feed
File.open('feed.xml','w') {|f| feed.write_xml_to f, :indent => 4}
end
def add_item(image_url, feed)
today = Time.now.strftime('%Y %d %d')
builder = Nokogiri::XML::Builder.with feed.at('channel') do |xml|
xml.item {
xml.title "Comic for "+today
xml.link image_url
xml.description "<img src=\""+image_url+"\"/>"
xml.pubDate today
xml.guid(:isPermaLink => "false") {
xml.text image_url
}
}
end
end
update_feed get_image_url()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment