Skip to content

Instantly share code, notes, and snippets.

@expajp
Created July 14, 2018 05:08
Show Gist options
  • Save expajp/128b83220caaf9bc58963afa73d71903 to your computer and use it in GitHub Desktop.
Save expajp/128b83220caaf9bc58963afa73d71903 to your computer and use it in GitHub Desktop.
GROWI(旧 Crowi-plus) の記事を esa.ioにインポートします
# coding: utf-8
require 'esa'
require 'json'
require 'pp'
require 'net/http'
URL_BASE = 'https://xxx.example.com/' # GROWIのURL, 末尾の/必須 TODO
USER_NAME = 'username' # GROWIのユーザ名 TODO
ACCESS_TOKEN = 'xxx' # GROWIのアクセストークン TODO
class Importer
def initialize(client)
@client = client
@exported_data = JSON.parse(
Net::HTTP.get(
URI.parse(URL_BASE + '_api/pages.list?access_token=' + ACCESS_TOKEN + '&user=' + USER_NAME)
)
)
end
attr_accessor :client, :exported_data
def wait_for(seconds)
(seconds / 10).times do
print '.'
sleep 10
end
puts
end
def import(dry_run: true, start_index: 0)
# インポートしたくないページ名は文字列で以下に設定 TODO
exclusions = []
exported_data['pages'].sort_by{ |page| page['updated_at'] }.each.with_index do |article, index|
next unless index >= start_index
next if exclusions.include?(article['path'])
name = article['path']
# ページ名を変えてインポートしたいページがある場合は以下のように設定 TODO
# name = 'top' if name == '/'
params = {
name: name,
body_md: article['revision']['body'],
wip: false,
message: '[skip notice] Imported from Crowi-plus',
user: 'expajp', # 記事作成者上書き: owner権限が必要 TODO
}
if dry_run
puts "***** index: #{name} *****"
# pp params
next
end
print "[#{Time.now}] index[#{index}] #{article['title']} => "
response = client.create_post(params)
case response.status
when 201
puts "created: #{response.body["full_name"]}"
when 429
retry_after = (response.headers['Retry-After'] || 20 * 60).to_i
puts "rate limit exceeded: will retry after #{retry_after} seconds."
wait_for(retry_after)
redo
else
puts "failure with status: #{response.status}"
exit 1
end
end
end
end
client = Esa::Client.new(
access_token: 'xxx', # APIアクセストークン TODO
current_team: 'username', # 移行先のチーム名(サブドメイン) TODO
)
importer = Importer.new(client)
# dry_run: trueで確認後に dry_run: falseで実際にimportを実行
importer.import(dry_run: true, start_index: 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment