Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Created May 24, 2015 13:07
Show Gist options
  • Save fukayatsu/c69dca3cecd83a8dbfb6 to your computer and use it in GitHub Desktop.
Save fukayatsu/c69dca3cecd83a8dbfb6 to your computer and use it in GitHub Desktop.
Import from Qiita:Team to esa.io
require 'esa'
require 'json'
require 'pp'
class Importer
def initialize(client, file_path)
@client = client
@items = JSON.parse(File.read(file_path))
end
attr_accessor :client, :items
def wait_for(seconds)
(seconds / 10).times do
print '.'
sleep 10
end
puts
end
def import(dry_run: true, start_index: 0)
items.each.with_index do |item, index|
next unless index >= start_index
params = {
name: item['title'],
category: "Imports/Qiita/#{item['user']['url_name']}",
tags: item['tags'].map{ |tag| tag['name'].gsub('/', '-') },
body_md: item['raw_body'],
wip: false,
message: '[skip notice] Imported from Qiita'
}
if dry_run
puts "***** index: #{index} *****"
pp params
puts
next
end
print "[#{Time.now}] index[#{index}] #{item['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: 'xxxxx',
current_team: 'your-team-name', # 移行先のチーム名(サブドメイン)
)
importer = Importer.new(client, '/path/to/export.json')
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