Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'esa'
require 'json'
require 'pry'
desc ''
task :default do
client = Esa::Client.new(
access_token: "<your access token>",
current_team: '<your team name>'
)
data = JSON.parse(File.read('data.json'), object_class: OpenStruct)
data.articles.each_with_index do |article, i|
created_at = Time.parse(article.created_at.split("+")[0])
year = created_at.year
month = created_at.month
day = created_at.strftime('%F')
category = "qiita/%s-%s-%s" % [year, day, month]
title = article.title.gsub(/[\p{P}]/, '-').gsub(/[[:blank:]]/, '-')
title += ":by #{article.user.id}"
body = "#{article.body}\n\n#{article.tags.map{|t| t.name + " "}}" #検索対策で一応bodyにもタグ文字列を入れておいてみる
tags = article.tags.map{|t| t.name}
# 重複除外
res = client.posts(q: "{title}")
# esa.ioAPIはrpsを12sec以下とする制限があるので @refs https://docs.esa.io/posts/102
sleep(13)
next if res.body["posts"].length > 0
request = {
"category" => category,
"name" => title,
"body_md" => body,
"tags" => tags,
"wip" => false,
"message" => "write from qiita",
"user" => "esa_bot"
}
res = client.create_post(request)
sleep(13)
if article.comments.length > 0
article.comments.each do |comment|
body = "#{comment.body}\n\nComment posted by #{comment.user.id}\n\n"
client.create_comment(
res.body["number"], body_md: body, user: "esa_bot"
)
sleep(13)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.