Skip to content

Instantly share code, notes, and snippets.

@saicologic
Last active December 25, 2018 08:40
Show Gist options
  • Save saicologic/f90c5312c018c7b6c39603295ec302f0 to your computer and use it in GitHub Desktop.
Save saicologic/f90c5312c018c7b6c39603295ec302f0 to your computer and use it in GitHub Desktop.
過去のesaの記事をまとめてArchiveする

ライブラリのインストール

$ bundle install

修正

teamは、esaで取得したチームID access_tokenは、

アーカイブ対象を取得する

bundle exec ruby ./get_archived_entries.rb

アーカイブする

bundle exec ruby ./get_archived_entries.rb

APIのアクセス制限について

esaのAPIは、75回/15分です。 上限を超えると Rate limit exceeded: will retry after 855 seconds. と表示されsleepされます。 RateLimit用の制御コードを書く必要ありません。

require 'esa'
require 'csv'
team = ''
access_token = ""
client = Esa::Client.new(access_token: access_token, current_team: team)
CSV.foreach('./output.csv', headers: true) do |row|
if row['category']
update_category = 'Archived/' + row['category']
else
update_category = 'Archived'
end
res = client.update_post(row['number'].to_i, category: update_category, message: '[skip notice] Archived')
if res.status != 200
puts "status: #{contents.status}"
exit
end
end
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "esa"
require 'esa'
require 'csv'
def read_contents(csv, contents)
if contents.body['posts'].size == 0
puts 'Not found posts'
exit
end
contents.body['posts'].each do |post|
next if post['category'] && post['category'].include?('Archived')
next if post['category'] && post['category'].include?('templates')
csv << [
post['number'], post['full_name'], post['category'], post['created_at'], post['updated_at'], post['url']
]
end
end
team = ''
access_token = ""
client = Esa::Client.new(access_token: access_token, current_team: team)
CSV.open('output.csv', 'w') do |csv|
csv << ['number', 'full_name', 'category', 'created_at', 'updated_at', 'url']
# とりあえず10ページ=1000件分を取得する
(1..10).each do |page|
puts "Read page: #{page}"
# 例: 2016-12-31までの記事を対象にする
contents = client.posts(q: "updated:<2016-12-31", page: page, per_page: 100)
if contents.status != 200
puts "status: #{contents.status}"
exit
end
read_contents(csv, contents)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment