Skip to content

Instantly share code, notes, and snippets.

@yoshihara
Last active August 7, 2018 02:48
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yoshihara/0da1862cd5f49b78530958d09bf0a52f to your computer and use it in GitHub Desktop.
esaのあるカテゴリ配下の記事を端末上で順番に見ていく
ESA_TOKEN=esaのAPIトークン
ESA_TEAMNAME=esaのチーム名
source "https://rubygems.org"
gem "dotenv"
gem "esa"
gem "tty-pager"
GEM
remote: https://rubygems.org/
specs:
dotenv (2.5.0)
esa (1.13.1)
faraday (~> 0.9)
faraday_middleware
mime-types (~> 2.6)
multi_xml (~> 0.5.5)
faraday (0.15.2)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
mime-types (2.99.3)
multi_xml (0.5.5)
multipart-post (2.0.0)
strings (0.1.1)
unicode-display_width (~> 1.3.0)
unicode_utils (~> 1.4.0)
tty-pager (0.11.0)
strings (~> 0.1.0)
tty-screen (~> 0.6.4)
tty-which (~> 0.3.0)
tty-screen (0.6.5)
tty-which (0.3.0)
unicode-display_width (1.3.3)
unicode_utils (1.4.0)
PLATFORMS
ruby
DEPENDENCIES
dotenv
esa
tty-pager
BUNDLED WITH
1.16.3
#!/usr/bin/env ruby
require "optparse"
require "time"
require "dotenv/load"
require "tty-pager"
require "esa"
ESA_TOKEN = ENV["ESA_TOKEN"].freeze
ESA_TEAMNAME = ENV["ESA_TEAMNAME"].freeze
params = ARGV.getopts("c:s:", "category:/", "start:")
category = params["category"] || params["c"]
start_date = params["start"] || params["s"] || Time.now.strftime("%Y-%m-%d")
esa_options = {
access_token: ESA_TOKEN,
current_team: ESA_TEAMNAME,
}
client = Esa::Client.new(esa_options)
first = true
response = nil
pager = TTY::Pager.new
while first || response.body["next_page"] do
# NOTE: default sortはupdated_at
esa_params = {q: "category:#{category} created:>#{start_date}", sort: "updated", order: "asc", include: "comments"}
esa_params[:page] = response.body["next_page"] unless first
response = client.posts(esa_params)
response.body["posts"].each do |post|
full_name = post["full_name"]
contents = full_name.dup
contents << " [WIP]" if post["wip"]
contents << "\n\n#{post['body_md']}"
post['comments'].each do |comment|
comment = <<COMMENT
---------------
#{comment['created_by']['screen_name']} commented:
#{comment['body_md']}
COMMENT
contents << comment
end
pager.page(contents)
print "#{full_name} was shown. You want to see it in browser? (y/N)"
ans = nil
begin
ans = gets.chomp
rescue Interrupt
exit(false)
end
if ans == "y"
`open #{post['url']}`
end
end
first = false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment