2 ms·
Write a little script that pulls http://news.ycombinator.com/threads?id=<username> http://news.ycombinator.com/threads?id=<username> and does some magic to
by petercooper 18y ago
Write a little script that pulls http://news.ycombinator.com/threads?id=<username> http://news.ycombinator.com/threads?id=<username> and does some magic to pull out the info into an RSS feed (yes, ideally pg can add this as a real feature, but I'm being pragmatic here ;-)).
Just a messy Ruby-based example I threw together. Works for me.
require 'rubygems'
require 'open-uri'
require 'hpricot'
username = 'petercooper'
uri = 'http://news.ycombinator.com/threads?id=' + username
doc = Hpricot(open(uri))
puts %{<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Hacker News responses to #{username}</title>
<link>#{uri}</link>}
(doc/'td td table').each do |post|
content = post.inner_html
next if content =~ />#{username}</ # skip if we posted it
next unless post.inner_html =~ /vote/ # skip if it's not a post
id = content[/\_(\d+)/,1]
comment_text = (post/'.comment').first.inner_text
commenter = content[/user\?id=(\w+)/,1]
puts %{ <item>\n <title>Comment from #{commenter}</title>}
puts %{ <link>http://news.ycombinator.com/item?id=#{id}</link>}
puts %{ <description>#{comment_text}</description>\n </item>}
end
puts %{</channel></rss>}
It'd be better, but the HTML on Hacker News is from 1996. I was too sloppy to bother fetching the title of the current thread ;-)
With just adding a shebang line and a content-type header line, it works no problems on Dreamhost as a CGI script. Example: http://bigbold.dreamhosters.com/hnc.cgi http://bigbold.dreamhosters.com/hnc.cgi
Anyway, thanks for giving me the idea. I'm using that now myself.. lol.
- jamesheroku 18y agoNice work Peter. I wrapped this in two lines of Sinatra and deployed it to Heroku, so anyone can get an RSS feed for their username: http://hackernews.heroku.com/username http://hackernews.heroku.com/username Code is here: http://github.com/jnl/hackernews-thread-rss http://github.com/jnl/hackernews-thread-rss
- dreeves 18y agoHoly crap, that's handy! Thanks so much! Out of curiosity (and in hopes of eliciting a reply to see if it shows up in my new Hacker News Responses feed!) would this also be easy to do with Yahoo!Pipes?
- petercooper 18y agoI wondered about that. I've been using Pipes a little lately but am no expert. I'm not sure if Pipes can deal with operations upon non organized content. You could probably use a service like Feed43 first though.