muut.rb를 사용하여 가져오기

안녕하세요,

muut 가져오기 스크립트를 실행할 때 다음 오류가 발생합니다:

Importing categories
    2 / 2 (100.0%)  [5170854 items/min]   
Importing discussions
Traceback (most recent call last):
	8: from script/import_scripts/muut.rb:184:in `<main>'
	7: from /var/www/discourse/script/import_scripts/base.rb:47:in `perform'
	6: from script/import_scripts/muut.rb:25:in `execute'
	5: from script/import_scripts/muut.rb:82:in `import_discussions'
	4: from script/import_scripts/muut.rb:82:in `each'
	3: from script/import_scripts/muut.rb:84:in `block in import_discussions'
	2: from script/import_scripts/muut.rb:84:in `each'
	1: from script/import_scripts/muut.rb:110:in `block (2 levels) in import_discussions'
script/import_scripts/muut.rb:162:in `process_muut_post_body': undefined method `gsub!' for nil:NilClass (NoMethodError)

이 오류를 해결하는 방법에 대해 방향을 잡아 주실 수 있으신가요?

어떤 필드, 아마도 게시물 데이터일 것입니다, 누락되어 있습니다. 스크립트가 잘못된 필드 이름을 참조하고 있기 때문일 수 있습니다.

아래는 제가 사용했을 때 작동했던 muut.rb 업데이트입니다:

# frozen_string_literal: true

require "csv"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")

# 가져오기 데이터를 위해 상수와 initialize 메서드를 편집하세요.

class ImportScripts::Muut < ImportScripts::Base

  JSON_FILE_PATH = "/path/to/json/file"
  CSV_FILE_PATH = "/path/to/csv/file"

  def initialize
super

@imported_users = load_csv
@imported_json = load_json
  end

  def execute
puts "", "Muut에서 가져오는 중..."

import_users
import_categories
import_discussions

puts "", "완료"
  end

  def load_json
JSON.parse(repair_json(File.read(JSON_FILE_PATH)))
  end

  def load_csv
CSV.parse(File.read(CSV_FILE_PATH))
  end

  def repair_json(arg)
arg.gsub!(/^\(/, "")     # 파일 내용은 ( )로 둘러싸여 있음
arg.gsub!(/\)$/, "")

arg.gsub!(/\]\]$/, "]")  # 끝에 추가적인 ]가 있을 수 있음

arg.gsub!(/\}\{/, "},{") # 때때로 쉼표가 누락됨!

arg.gsub!("}]{", "},{")  # 의외의 대괄호
arg.gsub!("}[{", "},{")  # :troll:

arg
  end

  def import_users
puts '', "사용자 가져오는 중"

create_users(@imported_users) do |u|
  {
    id: u[0],
    email: u[1],
    created_at: Time.now
  }
end
  end

  def import_categories
puts "", "카테고리 가져오는 중"

create_categories(@imported_json['categories']) do |category|
  {
    id: category['path'], # muut에는 카테고리 ID가 없으므로 path를 사용
    name: category['title'],
    slug: category['path']
  }
end
  end

  def import_discussions
puts "", "토론 가져오는 중"

topics = 0
posts = 0

@imported_json['categories'].each do |category|

  @imported_json['threads'][category['path']].each do |thread|

    next if thread["seed"]["key"] == "skip-this-topic"

    mapped = {}
    mapped[:id] = "#{thread["seed"]["key"]}-#{thread["seed"]["date"]}"

    if thread["seed"]["author"] && user_id_from_imported_user_id(thread["seed"]["author"]["path"]) != ""
      mapped[:user_id] = user_id_from_imported_user_id(thread["seed"]["author"]["path"]) || -1
    else
      mapped[:user_id] = -1
    end

    # 사용자 표시 이름 업데이트
    if thread["seed"]["author"] && thread["seed"]["author"]["displayname"] != "" && mapped[:user_id] != -1
      user = User.find_by(id: mapped[:user_id])
      if user
        user.name = thread["seed"]["author"]["displayname"]
        user.save!
      end
    end

    mapped[:created_at] = Time.zone.at(thread["seed"]["date"])
    mapped[:category] = category_id_from_imported_category_id(thread["seed"]["path"])
    mapped[:title] = CGI.unescapeHTML(thread["seed"]["title"])

    if thread["seed"]["body"] == ""
      thread["seed"]["body"] = " ";
    end
    mapped[:raw] = process_muut_post_body(thread["seed"]["body"])
    mapped[:raw] = CGI.unescapeHTML(thread["seed"]["title"]) if mapped[:raw] == ""

    parent_post = create_post(mapped, mapped[:id])
    unless parent_post.is_a?(Post)
      puts "토픽 #{mapped[:id]} 생성 중 오류 발생. 건너뜁니다."
      puts parent_post.inspect
    end

    # 영구 링크를 생성하려면 아래 주석을 해제하세요
    # Permalink.create(url: "#{thread["seed"]["path"]}:#{thread["seed"]["key"]}", topic_id: parent_post.topic_id)

    # 답변 생성
    if thread["replies"].present? && thread["replies"].count > 0
      thread["replies"].reverse_each do |post|

        if post_id_from_imported_post_id(post["id"])
          next # 이미 이 게시물을 가져옴
        end

        if post["body"] == ""
          post["body"] = " "
        end

        new_post = create_post({
            id: "#{post["key"]}-#{post["date"]}",
            topic_id: parent_post.topic_id,
            # author/path 값에서 고유한 user_id를 얻으려면 다음 줄을 수정하세요.
            user_id: user_id_from_imported_user_id(post["author"]["path"]) || -1,
            raw: process_muut_post_body(post["body"]),
            created_at: Time.zone.at(post["date"])
          }, post["id"])

        if new_post.is_a?(Post)
          posts += 1
        else
          puts "게시물 #{post["id"]} 생성 중 오류 발생. 건너뜁니다."
          puts new_post.inspect
        end

      end

    end

    topics += 1
  end
end

puts "", "#{topics}개의 토픽과 #{topics + posts}개의 게시물을 가져왔습니다."
  end

  def process_muut_post_body(arg)
raw = arg.dup
raw = raw.to_s
raw = raw[0..-1]

# 새 줄
if raw != nil 
  raw.gsub!(/\\n/, "\n")

# 코드 블록
  raw.gsub!("---", "```\n")

# 탭
  raw.gsub!(/\\t/, '  ')

# 이중 인용부호
  raw.gsub!(/\\\"/, '"')

  raw = CGI.unescapeHTML(raw)
end
raw
  end

  def file_full_path(relpath)
File.join JSON_FILES_DIR, relpath.split("?").first
  end

end

if __FILE__ == $0
  ImportScripts::Muut.new.perform
end