<commit_before>require! {
  request
  EventSource: \eventsource
  jsdom
}

document = jsdom.jsdom(null, null, {fetchExternalResources: false})

text-content = ->
  div = document.create-element \div
    ..innerHTML = (it || '')replace /<br>/g '\n'
  return div.textContent

es = new EventSource \http://localhost:3500/v1/a/stream
  ..add-event-listener \error !->
      console.error "error" it
  ..add-event-listener \new-posts !->
    posts = JSON.parse it.data
    for post in posts
      post.com_html = post.com
      post.com = text-content post.com_html
      post.sub = text-content post.sub
      post.name = text-content post.name

      ts = encodeURIComponent(new Date post.time * 1000 .toISOString!)

      request.put do
        url: "http://localhost:9200/yotsuba-a/post/#{post.no}
              ?parent=#{post.resto}&timestamp=#ts"
        json: post
        (err, res, body) ->
          console.error "body" body
          console.error "error" err if err

    console.error "submitted #{posts.length} posts"

<commit_msg>Reduce memory usage of es client<commit_after>require! {
  request
  EventSource: \eventsource
  ent
}

text-content = ->
  (it || '')
    .replace /<br>/g '\n' .replace /<[^>]+>/g '' |> ent.decode

es = new EventSource \http://fountain.hakase.org/v1/a/stream
  ..add-event-listener \error !->
      console.error "error" it
  ..add-event-listener \new-posts !->
    posts = JSON.parse it.data
    for post in posts
      post.com_html = post.com
      post.com = text-content post.com_html
      post.sub = text-content post.sub
      post.name = text-content post.name

      ts = encodeURIComponent(new Date post.time * 1000 .toISOString!)

      request.put do
        url: "http://127.0.0.1:9200/yotsuba-a/post/#{post.no}
              ?parent=#{post.resto}&timestamp=#ts"
        json: post
        (err, res, body) ->
          console.error "body" body
          console.error "error" err if err

    console.error "submitted #{posts.length} posts"


{'fraction_non_alphanumeric': 0.14264264264264265, 'fraction_numerical': 0.014014014014014014, 'mean_word_length': 2.388135593220339, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '6761697', 'n_tokens_mistral': 680, 'n_tokens_neox': 656, 'n_words': 153}