class BuntoImport::Importers::Mephisto
Constants
- QUERY
This query will pull blog posts from all entries across all blogs. If you've got unpublished, deleted or otherwise hidden posts please sift through the created posts to make sure nothing is accidently published.
Public Class Methods
postgres(c)
click to toggle source
Accepts a hash with database config variables, exports mephisto posts into a csv export PGPASSWORD if you must
# File lib/bunto-import/importers/mephisto.rb, line 6 def self.postgres(c) sql = <<-SQL BEGIN; CREATE TEMP TABLE bunto AS SELECT title, permalink, body, published_at, filter FROM contents WHERE user_id = 1 AND type = 'Article' ORDER BY published_at; COPY bunto TO STDOUT WITH CSV HEADER; ROLLBACK; SQL command = %Q(psql -h #{c[:host] || "localhost"} -c "#{sql.strip}" #{c[:database]} #{c[:username]} -o #{c[:filename] || "posts.csv"}) puts command `#{command}` CSV.process end
process(options)
click to toggle source
# File lib/bunto-import/importers/mephisto.rb, line 59 def self.process(options) dbname = options.fetch('dbname') user = options.fetch('user') pass = options.fetch('password', '') host = options.fetch('host', "localhost") db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8') FileUtils.mkdir_p "_posts" db[QUERY].each do |post| title = post[:title] slug = post[:permalink] date = post[:published_at] content = post[:body] # Ideally, this script would determine the post format (markdown, # html, etc) and create files with proper extensions. At this point # it just assumes that markdown will be acceptable. name = [date.year, date.month, date.day, slug].join('-') + ".markdown" data = { 'layout' => 'post', 'title' => title.to_s, 'mt_id' => post[:entry_id], }.delete_if { |k,v| v.nil? || v == ''}.to_yaml File.open("_posts/#{name}", "w") do |f| f.puts data f.puts "---" f.puts content end end end
require_deps()
click to toggle source
# File lib/bunto-import/importers/mephisto.rb, line 29 def self.require_deps BuntoImport.require_with_fallback(%w[ rubygems sequel fastercsv fileutils ]) end
specify_options(c)
click to toggle source
# File lib/bunto-import/importers/mephisto.rb, line 38 def self.specify_options(c) c.option 'dbname', '--dbname DB', 'Database name' c.option 'user', '--user USER', 'Database user name' c.option 'password', '--password PW', "Database user's password (default: '')" c.option 'host', '--host HOST', 'Database host name (default: "localhost")' end
validate(options)
click to toggle source
# File lib/bunto-import/importers/mephisto.rb, line 21 def self.validate(options) %w[dbname user].each do |option| if options[option].nil? abort "Missing mandatory option --#{option}." end end end