require "csv" class Vanilla < Thor desc "import", "Imports posts from a Vanilla export" method_option :file, aliases: '-f', required: true, desc: "The vanilla file to import" def import unless File.exist?(options[:file]) puts "File '#{options[:file]}' not found" exit 1 end load_rails file = read_file(options[:file]) parse_file(file) disable_rate_limiter create_users create_user_memberships create_categories create_topics create_posts update_topic_statuses create_private_topics create_private_posts ensure enable_rate_limiter end no_commands do def load_rails puts "loading rails..." require "./config/environment" end def disable_rate_limiter puts "disabling rate limiter..." RateLimiter.disable end def read_file(file) puts "reading file..." string = File.read(file).gsub("\\N", "").gsub(/\\$\n/m, "\\n").gsub("\\,", ",").gsub(/(?/i, "\n```\n") .gsub(/<\/?code\s*>/i, "`") .gsub("<", "<") .gsub(">", ">") end def enable_rate_limiter puts "enabling rate limiter..." RateLimiter.enable end end end