<commit_before>package kernel

object Watcher {
  import java.io.ByteArrayInputStream
  import java.net.URL
  import org.jsoup.Jsoup
  import helper.OptParse.Config

  private val logger = org.slf4j.LoggerFactory.getLogger(this.getClass)

  def run(config: Config): Unit = {
    val Config(target, cycle, selector, exec) = config
    logger.info("Config: [{}]", config)
    logger.info("press Ctrl+C to exit")
    val timeout = 2000
    val url = new URL(target)
    def check =
      try { Some(Jsoup.parse(url, timeout).select(selector).toString) }
      catch { case e: Exception => logger.error(e.toString); None }
    val first = check.getOrElse(""); logger.info("Beginning content:"); println(first)
    val samples = Iterator.continually { Thread.sleep(cycle); check }
    (first /: samples) {
      case (previous, Some(current)) if (current != previous) => {
        logger.info("Content changed to:")
        println(current)
        if (exec != null) {
          import sys.process._
          logger.info("calling [{}]", exec)
          val message = "Previous:\n" + previous + "\nCurrent:\n" + current
          exec #< new ByteArrayInputStream(message.getBytes) !!
        }
        current
      }
      case (previous, _) => previous
    }
  }
}<commit_msg>call external program with pipe<commit_after>package kernel

object Watcher {
  import java.io.ByteArrayInputStream
  import java.net.URL
  import org.jsoup.Jsoup
  import helper.OptParse.Config

  private val logger = org.slf4j.LoggerFactory.getLogger(this.getClass)

  def run(config: Config): Unit = {
    val Config(target, cycle, selector, exec) = config
    logger.info("Config: [{}]", config)
    logger.info("press Ctrl+C to exit")
    val timeout = 2000
    val url = new URL(target)
    def check =
      try { Some(Jsoup.parse(url, timeout).select(selector).toString) }
      catch { case e: Exception => logger.error(e.toString); None }
    val first = check.getOrElse(""); logger.info("Beginning content:"); println(first)
    val samples = Iterator.continually { Thread.sleep(cycle); check }
    (first /: samples) {
      case (previous, Some(current)) if (current != previous) => {
        logger.info("Content changed to:")
        println(current)
        if (exec != null) {
          import sys.process._
          logger.info("calling [{}]", exec)
          val msg = "Previous:\n" + previous + "\nCurrent:\n" + current
          val is = new ByteArrayInputStream(msg.getBytes("UTF-8"))
          (exec #< is).lines_!.foreach(println)
        }
        current
      }
      case (previous, _) => previous
    }
  }
}
{'fraction_non_alphanumeric': 0.13519724243584832, 'fraction_numerical': 0.004212945231711987, 'mean_word_length': 2.8355359765051396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2474004', 'n_tokens_mistral': 786, 'n_tokens_neox': 767, 'n_words': 193}