Skip to content

Instantly share code, notes, and snippets.

@sawadashota
Forked from yokozawa/rakefile.rb
Created May 25, 2017 01:54
Show Gist options
  • Save sawadashota/d1d406f06ffeba47bad67e7aff43b491 to your computer and use it in GitHub Desktop.
Save sawadashota/d1d406f06ffeba47bad67e7aff43b491 to your computer and use it in GitHub Desktop.
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