<commit_before>require_relative './quandora_question.rb'
require File.expand_path(File.dirname(__FILE__) + "/../base.rb")

class ImportScripts::Quandora < ImportScripts::Base

  JSON_FILES_DIR = "output"

  def initialize
    super
    @system_user = Discourse.system_user
    @questions = []
    Dir.foreach(JSON_FILES_DIR) do |filename|
      next if filename == '.' or filename == '..'
      question = File.read JSON_FILES_DIR + '/' + filename
      @questions << question
    end
  end

  def execute
    puts "", "Importing from Quandora..."
    import_questions @questions
    EmailToken.delete_all
    puts "", "Done"
  end

  def import_questions questions
    topics = 0
    total = questions.size

    questions.each do |question|
      q = QuandoraQuestion.new question
      import_users q.users
      created_topic = import_topic q.topic
      if created_topic
        import_posts q.replies, created_topic.topic_id
      end
      topics += 1
      print_status topics, total
    end
    puts "", "Imported #{topics} topics."
  end

  def import_users users
    users.each do |user|
      create_user user, user[:id]
    end
  end

  def import_topic topic
    post = nil
    if post_id = post_id_from_imported_post_id(topic[:id])
      post = Post.find(post_id) # already imported this topic
    else
      topic[:user_id] = user_id_from_imported_user_id(topic[:author_id]) || -1
      topic[:category] = 'quandora-import' 

      post = create_post(topic, topic[:id])

      unless created_topic.is_a?(Post)
        puts "Error creating topic #{topic[:id]}. Skipping."
        puts created_topic.inspect
      end
    end

    post
  end

  def import_posts posts, topic_id
    posts.each do |post|
      import_post post, topic_id
    end
  end

  def import_post post, topic_id
    if post_id_from_imported_post_id(post[:id])
      return # already imported
    end
    post[:topic_id] = topic_id
    post[:user_id] = user_id_from_imported_user_id(post[:author_id])
    new_post = create_post post, post[:id] 
    unless new_post.is_a?(Post)
      puts "Error creating post #{post[:id]}. Skipping."
      puts new_post.inspect
    end
  end
  
  def file_full_path(relpath)
    File.join JSON_FILES_DIR, relpath.split("?").first
  end
end

if __FILE__==$0
  ImportScripts::Quandora.new.perform
end
<commit_msg>Fix issue setting post author during import from Quandora<commit_after>require_relative './quandora_question.rb'
require File.expand_path(File.dirname(__FILE__) + "/../base.rb")

class ImportScripts::Quandora < ImportScripts::Base

  JSON_FILES_DIR = "output"

  def initialize
    super
    @system_user = Discourse.system_user
    @questions = []
    Dir.foreach(JSON_FILES_DIR) do |filename|
      next if filename == '.' or filename == '..'
      question = File.read JSON_FILES_DIR + '/' + filename
      @questions << question
    end
  end

  def execute
    puts "", "Importing from Quandora..."
    import_questions @questions
    EmailToken.delete_all
    puts "", "Done"
  end

  def import_questions questions
    topics = 0
    total = questions.size

    questions.each do |question|
      q = QuandoraQuestion.new question
      import_users q.users
      created_topic = import_topic q.topic
      if created_topic
        import_posts q.replies, created_topic.topic_id
      end
      topics += 1
      print_status topics, total
    end
    puts "", "Imported #{topics} topics."
  end

  def import_users users
    users.each do |user|
      create_user user, user[:id]
    end
  end

  def import_topic topic
    post = nil
    if post_id = post_id_from_imported_post_id(topic[:id])
      post = Post.find(post_id) # already imported this topic
    else
      topic[:user_id] = user_id_from_imported_user_id(topic[:author_id]) || -1
      topic[:category] = 'quandora-import'

      post = create_post(topic, topic[:id])

      unless post.is_a?(Post)
        puts "Error creating topic #{topic[:id]}. Skipping."
        puts post.inspect
      end
    end

    post
  end

  def import_posts posts, topic_id
    posts.each do |post|
      import_post post, topic_id
    end
  end

  def import_post post, topic_id
    if post_id_from_imported_post_id(post[:id])
      return # already imported
    end
    post[:topic_id] = topic_id
    post[:user_id] = user_id_from_imported_user_id(post[:author_id]) || -1
    new_post = create_post post, post[:id]
    unless new_post.is_a?(Post)
      puts "Error creating post #{post[:id]}. Skipping."
      puts new_post.inspect
    end
  end

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

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

{'fraction_non_alphanumeric': 0.09752454118651302, 'fraction_numerical': 0.0019206145966709346, 'mean_word_length': 2.938655462184874, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '963328', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1525, 'n_words': 369}