Skip to content

Instantly share code, notes, and snippets.

@seancribbs
Created February 19, 2009 16:38
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 seancribbs/66990 to your computer and use it in GitHub Desktop.
Save seancribbs/66990 to your computer and use it in GitHub Desktop.
Import Mephisto articles into Radiant via the API.
require 'rubygems'
require '/path/to/radiant/api/lib/radiant/api' # http://github.com/radiant/radiant-api
require 'activerecord'
ActiveRecord::Base.establish_connection(
"adapter" => "mysql",
"host" => "localhost",
"database" => "mephisto",
"username" => "***********",
"password" => "***********")
class Content < ActiveRecord::Base
set_table_name :contents
end
class Article < Content
belongs_to :user
end
class Comment < Content; end
class User < ActiveRecord::Base; end
include Radiant
API.site = "http://yoursite.com/admin"
API.user = "************"
API.password = "************"
articles_to_import = Article.find(:all, :conditions => ["published_at < ?", Time.local(2008,12,5)])
# Replace the 'articles' with whatever the slug is of your Archive page
articles_page_id = API::Page.find(:all).find { |p| p.slug == 'articles' }.id
radiant_users = API::User.find(:all)
articles_to_import.each do |article|
begin
print "Importing '#{article.title}' "; $stdout.flush
new_page = API::Page.new(
:parent_id => articles_page_id,
:title => article.title,
:breadcrumb => article.title,
:slug => article.permalink,
:status_id => 100, # published
:published_at => article.published_at
)
filter_id = case article.filters
when /textile/i
"Textile"
when /markdown/i
"Markdown"
else
nil
end
new_page.parts = [{ :name => "body", :content => article.body, :filter_id => filter_id }]
new_page.save
puts new_page.errors.inspect if new_page.errors
# Now hack to change created_by_id to get the correct author
if user = radiant_users.find {|u| u.login == article.user.login }
new_page.created_by_id = user.id
new_page.save
end
puts
rescue Object => e
print "FAILED #{e.message}"
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment