require "esapad/version" require "time" require "esa" require "denv" require "pry" Denv.load class Esapad DEFAULT_PER_PAGE = 20 def initialize @client = Esa::Client.new(access_token: ENV["ESA_ACCESS_TOKEN"], current_team: ENV["ESA_TEAM"]) end def update_pages_list(target_page_id) flow_updated_md = generate_updated_md("flow") stock_updated_md = generate_updated_md("stock") recently_liked_md = generate_recently_liked_md target_page = @client.post(ENV["ESA_TARGET_PAGE_ID"]) target_page_md = target_page.body["body_md"] target_page_md = replace_pages_list_md(target_page_md, "flow", flow_updated_md) target_page_md = replace_pages_list_md(target_page_md, "stock", stock_updated_md) target_page_md = replace_liked_posts_md(target_page_md, recently_liked_md) if target_page_md != target_page.body["body_md"] target_page_md = replace_updated_time(target_page_md) message = skip_notice? ? "[skip notice]" : "" @client.update_post(target_page_id, body_md: target_page_md, updated_by: "esa_bot", message: message) puts "Updated: #{ target_page.body["url"] }" end end def blog_query ENV["BLOG_CATEGORY"] || "category:日報/" end def fetch_updated_pages(kind) query = case kind when "flow" "wip:false #{blog_query} -body:RECENTLY-UPDATED-POSTS" when "stock" "wip:false -#{blog_query} -body:RECENTLY-UPDATED-POSTS" end @client.posts(q: query, per_page: per_page).body["posts"] end def generate_updated_md(kind) posts = fetch_updated_pages(kind) posts.map {|post| <<-MARKDOWN