public
Description: An example of a stand-alone Ruby web filter
Clone URL: git://github.com/JackDanger/hacker_news.git
Search Repo:
JackDanger (author)
Mon Apr 21 14:18:06 -0700 2008
commit  bdfaf91fbc087c36e0dea0a6fbc430634ddcaa6a
tree    c8fa362ddbd15d9d2e36fe7d105f0832d959dcf5
parent  65adb49daed40bd7138798310780c2e51ba79d79
hacker_news / hackernews.rb
100755 41 lines (36 sloc) 1.758 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'activesupport'
require 'rss/maker'
require 'net/http'
require 'rack'
 
news = lambda do |env|
  blog = Hpricot.parse(Net::HTTP.get(URI.parse('http://news.ycombinator.com')))
  main_table = (blog / 'td').find {|td| td.attributes['class'] == 'title' }.parent.parent
  
  feed = RSS::Maker.make('1.0') do |rss|
    rss.channel.about = "Hacker News"
    rss.channel.title = "Hacker News"
    rss.channel.description = "Hacker News"
    rss.channel.link = "http://news.ycombinator.com"
    (main_table / 'tr td.title a').each do |link|
      next if link.attributes['rel'] == 'nofollow'
      item = rss.items.new_item
      item.author = (link.parent.parent.next_sibling / 'td a').first.inner_text
      comments_link = (link.parent.parent.next_sibling / 'td a').last.attributes['href']
      comments_number = (link.parent.parent.next_sibling / 'td a').last.inner_text.split.first
      item.title = link.inner_text
      item.link = link.attributes['href'] =~ /^http/ ? link.attributes['href'] : "http://news.ycombinator.com/#{link.attributes['href']}"
      article_cache = File.join(File.dirname(__FILE__), 'cache', item.link.gsub(/:|\/|=|&|\[|\]|\?/, '_'))
      if File.exist?(article_cache)
        article = File.read(article_cache)
      else
        article = `links -dump #{item.link}`
        File.open(article_cache, 'w') {|f| f.write article }
      end
      item.description = " <pre>#{article} </pre><p><a href='#{comments_link}'>#{comments_number} Comments</a></p>"
    end
  end.to_s
 
  [200, {"Content-Type"=>"text/xml"}, feed]
end
 
Rack::Handler::Mongrel.run(news, {:Host => "127.0.0.1", :Port => 7025})