Skip to content

Instantly share code, notes, and snippets.

@spiceee
Created February 24, 2010 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spiceee/313681 to your computer and use it in GitHub Desktop.
Save spiceee/313681 to your computer and use it in GitHub Desktop.
namespace :open_directory do
# http://www.google.com/Top/
# http://rdf.dmoz.org/rdf/structure.rdf.u8.gz
desc "tasks that deal with Google Open Directory importing"
task :import => :environment do
seeds = []
Category.delete_all
File.open(RAILS_ROOT + "/assets/structure.rdf.u8").grep(/<Topic r:id="Top\/Business\//) do |ln|
parts = ln.sub(/<Topic r:id=\"Top\/Business\/([^>]+)">$/, '\1').gsub('_', " ").split('/').collect(&:strip)
parent_id = nil
parts.each do |part|
unless (categ = Category.find(:first, :conditions => {:name => part, :parent_id => parent_id}))
categ = Category.create!(:name => part, :parent_id => parent_id)
puts "new category: #{part} - father: #{categ.parent.andand.name}"
seeds.push "Category.create(:name => \"#{part}\", :parent_id => #{parent_id.nil? ? 'nil' : parent_id})"
end
parent_id = categ.id
end
end
open(RAILS_ROOT + "/db/seeds.rb","a").write seeds.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment