Created
February 7, 2017 03:54
-
-
Save yokozawa/cdd44efcd77b244be33cbffd5b94d905 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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