From f1c242f61d6a8f586aec2fd5f0d923100a612975 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 17 Jan 2017 17:08:45 -0500 Subject: [PATCH] A generic JSON importer that can be used for simple imports --- script/import_scripts/json_generic.rb | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 script/import_scripts/json_generic.rb diff --git a/script/import_scripts/json_generic.rb b/script/import_scripts/json_generic.rb new file mode 100755 index 00000000000..55b2152f21d --- /dev/null +++ b/script/import_scripts/json_generic.rb @@ -0,0 +1,96 @@ +require "csv" +require File.expand_path(File.dirname(__FILE__) + "/base.rb") + +# Edit the constants and initialize method for your import data. + +class ImportScripts::JsonGeneric < ImportScripts::Base + + JSON_FILE_PATH = ENV['JSON_FILE'] + BATCH_SIZE ||= 1000 + + def initialize + super + + @imported_json = load_json + end + + def execute + puts "", "Importing from JSON file..." + + import_users + import_discussions + + puts "", "Done" + end + + def load_json + JSON.parse(File.read(JSON_FILE_PATH)) + end + + def import_users + puts '', "Importing users" + + users = [] + @imported_json['topics'].each do |t| + t['posts'].each do |p| + users << p['author'] + end + end + users.uniq! + + create_users(users) do |u| + { + id: u, + email: "#{u}@example.com", + created_at: Time.now + } + end + end + + + def import_discussions + puts "", "Importing discussions" + + topics = 0 + posts = 0 + + @imported_json['topics'].each do |t| + first_post = t['posts'][0] + next unless first_post + + topic = { + id: t["id"], + user_id: user_id_from_imported_user_id(first_post["author"]) || -1, + raw: first_post["body"], + created_at: Time.zone.parse(first_post["date"]), + cook_method: Post.cook_methods[:raw_html], + title: t['title'], + category: ENV['CATEGORY_ID'], + custom_fields: { import_id: "pid:#{first_post['id']}" } + } + + topic[:pinned_at] = Time.zone.parse(first_post["date"]) if t['pinned'] + topics += 1 + parent_post = create_post(topic, topic[:id]) + + t['posts'][1..-1].each do |p| + create_post({ + id: p["id"], + topic_id: parent_post.topic_id, + user_id: user_id_from_imported_user_id(p["author"]) || -1, + raw: p["body"], + created_at: Time.zone.parse(p["date"]), + cook_method: Post.cook_methods[:raw_html], + custom_fields: { import_id: "pid:#{p['id']}" } + }, p['id']) + posts += 1 + end + end + + puts "", "Imported #{topics} topics with #{topics + posts} posts." + end +end + +if __FILE__==$0 + ImportScripts::JsonGeneric.new.perform +end