-
-
Save ins429/5c1b8a9db94c4770ab47 to your computer and use it in GitHub Desktop.
wp to dc posts migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 모르는것 | |
# archetype, post_serializer | |
# incase net/http is not required | |
require "net/http" | |
require "uri" | |
require "json" | |
# ...? | |
# require_dependency 'rate_limiter' | |
# require_dependency 'topic_creator' | |
desc "Import posts and replies from a wordpress" | |
task "import:wp_config" => :environment do | |
# Import configuration file | |
# @config = YAML.load_file('config/wordpress.yml') | |
# TEST_MODE = @config['test_mode'] | |
# Backup Site Settings | |
dc_backup_site_settings | |
# Then set the temporary Site Settings we need | |
dc_set_temporary_site_settings | |
# Fetch wp posts and insert! | |
wp_fetch_and_insert_posts | |
# Restore Site Settings | |
dc_restore_site_settings | |
puts "\n*** DONE".green | |
# DONE! | |
end | |
############################################################ | |
#### Methods | |
############################################################ | |
def wp_fetch_and_insert_posts | |
# set default total page.. | |
total_pages = 10000 | |
page = 1 | |
while page <= total_pages | |
uri = URI.parse("http://tidev.kr/api/get_posts?post_type=topic&page=#{page}") | |
response = Net::HTTP.get_response(uri) | |
# parse json response body into hash | |
chunk = JSON.parse(response.body) | |
# set correct total pages | |
if total_pages == 10000 | |
total_pages = chunk["pages"] | |
end | |
# insert each post into dc | |
chunk["posts"].each do |post| | |
puts "topic id: #{post['id']}" | |
# find user based on from post's author slug, guessing that this slug is the username | |
dc_user = dc_get_user(post["author"]["slug"]) | |
post_creator = PostCreator.new( | |
dc_user, | |
raw: post["content"], | |
title: post["title"], | |
archetype: "regular", | |
created_at: Time.parse(post["date"]) | |
) | |
# create post | |
post = post_creator.create | |
# Fetch wp replies and insert! | |
wp_fetch_and_insert_replies(post.topic.id) | |
end | |
page += 1 | |
end | |
end | |
def wp_fetch_and_insert_replies(topic_id) | |
# set default total page.. | |
total_pages = 10000 | |
page = 1 | |
while page <= total_pages | |
uri = URI.parse("http://tidev.kr/api/get_posts?post_type=reply&post_parent=#{topic_id}&page=#{page}") | |
response = Net::HTTP.get_response(uri) | |
# parse json response body into hash | |
chunk = JSON.parse(response.body) | |
# set correct total pages | |
if total_pages == 10000 | |
total_pages = chunk["pages"] | |
end | |
# insert each post into dc | |
chunk["posts"].each do |post| | |
puts "reply id: #{post['id']}" | |
# need to get correct topic_id, probably we want to find by id | |
# find user based on from post's author slug, guessing that this slug is the username | |
dc_user = dc_get_user(post["author"]["slug"]) | |
post_creator = PostCreator.new( | |
dc_user, | |
raw: post["content"], | |
title: post["title"], | |
archetype: "regular", | |
topic_id: topic_id, | |
created_at: Time.parse(post["date"]) | |
) | |
# create post | |
post = post_creator.create | |
end | |
page += 1 | |
end | |
end | |
# Backup site settings | |
def dc_backup_site_settings | |
@site_settings = {} | |
@site_settings['unique_posts_mins'] = SiteSetting.unique_posts_mins | |
@site_settings['rate_limit_create_topic'] = SiteSetting.rate_limit_create_topic | |
@site_settings['rate_limit_create_post'] = SiteSetting.rate_limit_create_post | |
@site_settings['max_topics_per_day'] = SiteSetting.max_topics_per_day | |
@site_settings['title_min_entropy'] = SiteSetting.title_min_entropy | |
@site_settings['body_min_entropy'] = SiteSetting.body_min_entropy | |
end | |
# Restore site settings | |
def dc_restore_site_settings | |
SiteSetting.send("unique_posts_mins=", @site_settings['unique_posts_mins']) | |
SiteSetting.send("rate_limit_create_topic=", @site_settings['rate_limit_create_topic']) | |
SiteSetting.send("rate_limit_create_post=", @site_settings['rate_limit_create_post']) | |
SiteSetting.send("max_topics_per_day=", @site_settings['max_topics_per_day']) | |
SiteSetting.send("title_min_entropy=", @site_settings['title_min_entropy']) | |
SiteSetting.send("body_min_entropy=", @site_settings['body_min_entropy']) | |
end | |
# Set temporary site settings needed for this rake task | |
def dc_set_temporary_site_settings | |
SiteSetting.send("unique_posts_mins=", 0) | |
SiteSetting.send("rate_limit_create_topic=", 0) | |
SiteSetting.send("rate_limit_create_post=", 0) | |
SiteSetting.send("max_topics_per_day=", 10000) | |
SiteSetting.send("title_min_entropy=", 1) | |
SiteSetting.send("body_min_entropy=", 1) | |
end | |
# Check if user exists | |
# For some really weird reason this method returns the opposite value | |
# So if it did find the user, the result is false | |
def dc_get_user(name) | |
User.where('username = ?', name).first | |
end | |
# Add colors to class String | |
class String | |
def red | |
colorize(self, 31); | |
end | |
def green | |
colorize(self, 32); | |
end | |
def yellow | |
colorize(self, 33); | |
end | |
def blue | |
colorize(self, 34); | |
end | |
def colorize(text, color_code) | |
"\033[#{color_code}m#{text}\033[0m" | |
end | |
end | |
# wp_fetch_and_insert_posts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment