Skip to content

Instantly share code, notes, and snippets.

@akitaonrails
Created August 8, 2008 23:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akitaonrails/4640 to your computer and use it in GitHub Desktop.
Save akitaonrails/4640 to your computer and use it in GitHub Desktop.
require 'hpricot'
require 'open-uri'
# update the database with the actual titles from each site
Blog.all.each do |b|
puts b.url
begin
doc = Hpricot(open(b.url.strip))
title = (doc/"html/*/title/").to_s
if title
b.url = b.url.strip
b.blog_name = title.strip
else
b.blog_name = b.author
end
b.save
rescue
b.destroy
end
end
# update the database with feed info
Blog.all.each do |b|
puts b.url
begin
doc = Hpricot(open(b.url.strip))
links = (doc/"link[@rel='alternate']")
href, feed_type = nil, nil
if links
if links.size > 1
tmp = links.select{|l| l.attributes['title'] =~ /All/ || l.attributes['title'] !~ /comment/ }
links = tmp if tmp
end
href = links.first.attributes['href']
feed_type = links.first.attributes['type'] rescue ""
end
if href
puts "feed: #{href} - type: #{feed_type}"
b.feed = href =~ /^http/ ? href.strip : b.url.strip + href.strip
b.feed_type = feed_type.strip
b.save
else
puts "NOT FOUND"
end
rescue => e
puts e
end
end
# prints out each html table line
Blog.all(:order => :random_number).each do |b|
puts <<-EOF
<tr>
<td><a href="#{b.url}" target="_blank">#{b.blog_name}</a></td>
<td>#{b.author}</td>
</tr>
EOF
end
# prints out each OPML outline row
Blog.all(:order => :random_number, :conditions => "feed <> ''").each do |b|
puts <<-EOF
<outline text="#{b.blog_name}"
title="#{b.blog_name}" type="rss"
xmlUrl="#{b.feed}" htmlUrl="#{b.url}"/>
EOF
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment