Skip to content

Instantly share code, notes, and snippets.

@andrew
Created February 25, 2009 17:24
Show Gist options
  • Save andrew/70297 to your computer and use it in GitHub Desktop.
Save andrew/70297 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'builder'
def get_subreddits(uri = 'http://www.reddit.com/reddits/')
reddits = []
while uri
page = download_page(uri)
reddits += find_reddits_on(page)
uri = get_next(page)
end
reddits
end
def download_page(uri)
Hpricot(open(uri))
end
def get_next(page)
a = page.search("//p[@class='nextprev']/a").last
if a and a.inner_html == 'next'
a.attributes['href']
else
false
end
end
def find_reddits_on(page)
subreddits = []
for div in page.search("div.subreddit")
subreddits << [div.search('.title').inner_html, div.search('.title').first.attributes['href'], div.search('.description').inner_html, div.search('.score').inner_html.split[0]]
end
return subreddits
end
def output_subreddits(subreddits)
# TODO doctype
xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
xml.instruct!
xml.subreddits do
subreddits.each do | subreddit |
xml.reddit{ xml.title(subreddit[0]);
xml.href(subreddit[1]);
xml.description(subreddit[2]);
xml.score(subreddit[3]);}
end
end
end
output_subreddits get_subreddits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment