<commit_before>require "esapad/version"

require "time"
require "esa"
require "denv"
require "pry"

Denv.load

class Esapad
  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)
      @client.update_post(target_page_id, body_md: target_page_md)
      puts "Updated: #{ target_page.body["url"] }"
    end
  end

  def fetch_updated_pages(kind)
    @client.posts(q: "wip:false kind:#{kind} -body:RECENTLY-UPDATED-POSTS").body["posts"]
  end

  def generate_updated_md(kind)
    posts = fetch_updated_pages(kind)
    posts.map {|post|
      <<-MARKDOWN

      <li>
        <a href="#{ post["url"] }">#{ post["full_name"] }</a>
        <div class="recently-updated-posts-metadata" style="font-size: 90%;">
          <span class="post-list__date">#{ Time.parse(post["updated_at"]).strftime("%Y-%m-%d %H:%M") }</span>
          by <img src="#{ post["updated_by"]["icon"] }" width="20px" height="20px" />
          <a href="https://mwed.esa.io/users/#{ post["updated_by"]["screen_name"] }">#{ post["updated_by"]["screen_name"] }</a>
        </div>
      </li>
      MARKDOWN
    }.join
  end

  def generate_recently_liked_md
    posts = (1..3).
      map {|page| @client.posts(q: "wip:false", page: page).body["posts"] }.
      flatten.
      sort_by {|post| -post["stargazers_count"] }.
      slice(0, 20).
      map {|post|
        <<-MARKDOWN

        <li><commit_msg>Change blog query<commit_after>require "esapad/version"

require "time"
require "esa"
require "denv"
require "pry"

Denv.load

class Esapad
  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)
      @client.update_post(target_page_id, body_md: target_page_md)
      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).body["posts"]
  end

  def generate_updated_md(kind)
    posts = fetch_updated_pages(kind)
    posts.map {|post|
      <<-MARKDOWN

      <li>
        <a href="#{ post["url"] }">#{ post["full_name"] }</a>
        <div class="recently-updated-posts-metadata" style="font-size: 90%;">
          <span class="post-list__date">#{ Time.parse(post["updated_at"]).strftime("%Y-%m-%d %H:%M") }</span>
          by <img src="#{ post["updated_by"]["icon"] }" width="20px" height="20px" />
          <a href="https://mwed.esa.io/users/#{ post["updated_by"]["screen_name"] }">#{ post["updated_by"]["screen_name"] }</a>
        </div>
      </li>
      MARKDOWN
    }.join
  end

  def generate_recently_liked_md
    posts = (1..3).
      map {|page| @client.posts(q: "wip:false", page: page).body["posts"] }.
      flatten.
      sort_by {|post| -post["stargazers_count"] }.
      slice(0, 20).
      map {|post|
        <<-MARKDOWN

        <li>
{'fraction_non_alphanumeric': 0.15455985517085313, 'fraction_numerical': 0.004978501923512107, 'mean_word_length': 3.773218142548596, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7097591', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1620, 'n_words': 262}