improve GetSatisfaction import script

This commit is contained in:
Arpit Jalan 2016-09-08 14:57:20 +05:30
parent 19ddf95efa
commit bade41db42

View File

@ -7,10 +7,11 @@
# - topics.csv is the topics table export # - topics.csv is the topics table export
# #
# #
# note, the importer will import all topics into a new category called 'Old Forum' and close all the topics # note, the importer will import all topics into a new category called 'Old Forum' and optionally close all the topics
# #
require 'csv' require 'csv'
require File.expand_path(File.dirname(__FILE__) + "/base.rb") require File.expand_path(File.dirname(__FILE__) + "/base.rb")
require 'reverse_markdown' # gem 'reverse_markdown'
# Call it like this: # Call it like this:
# RAILS_ENV=production bundle exec ruby script/import_scripts/getsatisfaction.rb # RAILS_ENV=production bundle exec ruby script/import_scripts/getsatisfaction.rb
@ -22,6 +23,7 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
@path = path @path = path
super() super()
@bbcode_to_md = true @bbcode_to_md = true
@topic_slug = {}
puts "loading post mappings..." puts "loading post mappings..."
@post_number_map = {} @post_number_map = {}
@ -43,7 +45,10 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
import_users import_users
import_posts(c) import_posts(c)
Topic.where(category: c).update_all(closed: true) create_permalinks
# uncomment if you want to close all the topics
# Topic.where(category: c).update_all(closed: true)
end end
class RowResolver class RowResolver
@ -81,6 +86,8 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
current_row = ""; current_row = "";
double_quote_count = 0 double_quote_count = 0
# In case of Excel export file, I converted it to CSV and used:
# CSV.open(filename, encoding:'iso-8859-1:utf-8').each do |raw|
File.open(filename).each_line do |line| File.open(filename).each_line do |line|
line.strip! line.strip!
@ -125,6 +132,8 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
end end
def total_rows(table) def total_rows(table)
# In case of Excel export file, I converted it to CSV and used:
# CSV.foreach("#{@path}/#{table}.csv", encoding:'iso-8859-1:utf-8').inject(0) {|c, line| c+1} - 1
File.foreach("#{@path}/#{table}.csv").inject(0) {|c, line| c+1} - 1 File.foreach("#{@path}/#{table}.csv").inject(0) {|c, line| c+1} - 1
end end
@ -191,6 +200,7 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
end end
def normalize_raw!(raw) def normalize_raw!(raw)
return "<missing>" if raw.nil?
raw = raw.dup raw = raw.dup
# hoist code # hoist code
@ -215,6 +225,8 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
raw.gsub!(hoist, "\n```\n" << code << "\n```\n") raw.gsub!(hoist, "\n```\n" << code << "\n```\n")
end end
raw = CGI.unescapeHTML(raw)
raw = ReverseMarkdown.convert(raw)
raw raw
end end
@ -270,6 +282,8 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
topic_map = {} topic_map = {}
csv_parse("topics") do |topic| csv_parse("topics") do |topic|
@topic_slug[topic.id.to_i] = topic.url
topic_map[topic.id] = { topic_map[topic.id] = {
id: topic.id, id: topic.id,
topic_id: topic.id, topic_id: topic.id,
@ -320,6 +334,20 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
import_post_batch!(posts, topic_map, count - posts.length, total) if posts.length > 0 import_post_batch!(posts, topic_map, count - posts.length, total) if posts.length > 0
end end
def create_permalinks
puts '', 'Creating Permalinks...', ''
topic_mapping = []
Topic.listable_topics.find_each do |topic|
tcf = topic.first_post.custom_fields
if tcf && tcf["import_id"]
slug = @topic_slug[tcf["import_id"].to_i]
slug = slug.gsub("https://community.unbounce.com/", "")
Permalink.create(url: slug, topic_id: topic.id)
end
end
end
end end