Aquí tienes una actualización de muut.rb que funcionó para mí:
# frozen_string_literal: true
require "csv"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
# Edita las constantes y el método initialize para tus datos de importación.
class ImportScripts::Muut < ImportScripts::Base
JSON_FILE_PATH = "/ruta/al/archivo/json"
CSV_FILE_PATH = "/ruta/al/archivo/csv"
def initialize
super
@imported_users = load_csv
@imported_json = load_json
end
def execute
puts "", "Importando desde Muut..."
import_users
import_categories
import_discussions
puts "", "Listo"
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!(/^\(/, "") # el contenido del archivo está rodeado por ( )
arg.gsub!(/\)$/, "")
arg.gsub!(/\]\]$/, "]") # puede haber un ] extra al final
arg.gsub!(/\}\{/, "},{") # ¡a veces faltan comas!
arg.gsub!("}]{", "},{") # corchetes sorpresa
arg.gsub!("}[{", "},{") # :troll:
arg
end
def import_users
puts '', "Importando usuarios"
create_users(@imported_users) do |u|
{
id: u[0],
email: u[1],
created_at: Time.now
}
end
end
def import_categories
puts "", "Importando categorías"
create_categories(@imported_json['categories']) do |category|
{
id: category['path'], # muut no tiene id para categorías, así que usamos el path
name: category['title'],
slug: category['path']
}
end
end
def import_discussions
puts "", "Importando discusiones"
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
# actualizar el nombre de visualización del usuario
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 "Error al crear el tema #{mapped[:id]}. Saltando."
puts parent_post.inspect
end
# descomenta la línea de abajo para crear el enlace permanente
# Permalink.create(url: "#{thread["seed"]["path"]}:#{thread["seed"]["key"]}", topic_id: parent_post.topic_id)
# crear respuestas
if thread["replies"].present? && thread["replies"].count > 0
thread["replies"].reverse_each do |post|
if post_id_from_imported_post_id(post["id"])
next # ya se importó este mensaje
end
if post["body"] == ""
post["body"] = " "
end
new_post = create_post({
id: "#{post["key"]}-#{post["date"]}",
topic_id: parent_post.topic_id,
# Modifica la siguiente línea para obtener el user_id único desde el valor author/path.
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 "Error al crear el mensaje #{post["id"]}. Saltando."
puts new_post.inspect
end
end
end
topics += 1
end
end
puts "", "Importados #{topics} temas con #{topics + posts} mensajes."
end
def process_muut_post_body(arg)
raw = arg.dup
raw = raw.to_s
raw = raw[0..-1]
# nueva línea
if raw != nil
raw.gsub!(/\\n/, "\n")
# bloque de código
raw.gsub!("---", "```\n")
# tabulación
raw.gsub!(/\\t/, ' ')
# comillas dobles
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