hawkeye -> hawk
This commit is contained in:
+91
-33
@@ -2,10 +2,14 @@
|
||||
(:require [clojure.java.io :as io]
|
||||
[babashka.fs :as fs]
|
||||
[babashka.process :as p]
|
||||
[progrock.core :as prog]
|
||||
[clj-rcon.core :as rcon]
|
||||
[clojure.tools.logging :as l]
|
||||
[clojure.string :as str]
|
||||
[hawkeye.core :as hawk])
|
||||
[hawk.core :as hawk]
|
||||
[next.jdbc :as sql]
|
||||
[integrant.core :as ig])
|
||||
(:import (java.util.zip GZIPInputStream))
|
||||
(:gen-class))
|
||||
|
||||
(def log-file
|
||||
@@ -15,29 +19,40 @@
|
||||
|
||||
(def words-file "words.gz")
|
||||
|
||||
(def words-db-file "words.db")
|
||||
|
||||
(def ^:dynamic *rcon*)
|
||||
|
||||
(defn parse-plaintext-entry [s]
|
||||
(when-some [[_ word gloss] (re-matches #"([^\t]*)\t(.*)\n?" s)]
|
||||
[word gloss]))
|
||||
|
||||
(defn lookup-word-plaintext [word]
|
||||
(let [r (p/shell {:out :string :continue true}
|
||||
"zgrep" "-m1"
|
||||
(format "^%s\t[^[:space:]]" word)
|
||||
words-file)]
|
||||
(when (zero? (:exit r))
|
||||
(->> r
|
||||
:out
|
||||
str/split-lines
|
||||
(map #(-> % parse-plaintext-entry second))
|
||||
(filter #(not (empty? %))) ; remove nil and ""
|
||||
first))))
|
||||
|
||||
(defn lookup-word-sql [word])
|
||||
|
||||
(defn lookup-word [word]
|
||||
(->> (p/shell {:out :string}
|
||||
"zgrep"
|
||||
(format "^%s\t" word)
|
||||
words-file)
|
||||
:out
|
||||
str/split-lines
|
||||
(map #(nth (re-matches #"([^\t]*)\t(.*)\n?" %)
|
||||
2 nil))
|
||||
(filter #(not (empty? %))) ; remove nil and ""
|
||||
first))
|
||||
(lookup-word-plaintext word))
|
||||
|
||||
(defn parse-chat-message [x]
|
||||
(when-let [[_ _dead? author body]
|
||||
(re-matches #"(\*사망\* )?(.*?) : (.*)" x)]
|
||||
{:author author :body body}))
|
||||
|
||||
(defn say-word [word]
|
||||
(->> (format "%s: %s" word (or (lookup-word word)
|
||||
"no entry found }:("))
|
||||
(take 128)
|
||||
(defn say [s]
|
||||
(->> s
|
||||
(take 128) ; truncate to 128 chars
|
||||
(apply str)
|
||||
(format "say \"%s\"")
|
||||
(rcon/exec *rcon*)))
|
||||
@@ -50,7 +65,13 @@
|
||||
(defn do-definition [s]
|
||||
(when-some [w (find-word s)]
|
||||
(l/infof "looking up word: %s" w)
|
||||
(say-word w)))
|
||||
(let [r (lookup-word w)]
|
||||
(if r
|
||||
(l/infof "found word! %s" r)
|
||||
(l/infof "no entry... %s" w))
|
||||
(Thread/sleep 750) ; avoid ratelimit lol
|
||||
(say (format "%s: %s"
|
||||
w (or r "no entry found (u_u)"))))))
|
||||
|
||||
(defn rcon-connect [host port password]
|
||||
(l/info "attempting rcon connection...")
|
||||
@@ -87,23 +108,60 @@
|
||||
(finally (reset! prev-size size)))))
|
||||
|
||||
(defn start-watcher! []
|
||||
(hawk/watch (str (fs/parent log-file))
|
||||
(bound-fn* (fn [ev]
|
||||
(when (= (str (fs/file-name log-file))
|
||||
(:file ev))
|
||||
(handler))))
|
||||
(fn [e ctx]
|
||||
(l/error e "error in watcher"))))
|
||||
(l/infof "watching file %s" log-file)
|
||||
(hawk/watch! [{:paths [(str log-file)]
|
||||
:handler
|
||||
(bound-fn*
|
||||
(fn [_ctx ev]
|
||||
(try (handler)
|
||||
(catch Exception e
|
||||
(l/errorf e "exception in handler")
|
||||
(throw e)))))}]))
|
||||
|
||||
(defn start-logger! []
|
||||
@(rcon/exec *rcon* (format "con_logfile %s"
|
||||
(fs/file-name log-file)))
|
||||
@(rcon/exec *rcon* "echo \"wiktionary logging now\"")
|
||||
(Thread/sleep 400)
|
||||
(l/info "logfile has been set up!"))
|
||||
|
||||
|
||||
(def config
|
||||
{:rcon/connection {:ip "127.0.0.1"
|
||||
:port 27015
|
||||
:password "monitor"}
|
||||
:console/watcher
|
||||
{:log-file
|
||||
(-> (str "~/.local/share/Steam/steamapps/common/Team Fortress 2/"
|
||||
"tf/console.log")
|
||||
fs/expand-home
|
||||
fs/file)
|
||||
:handler (ig/ref :wikilinker/handler)}
|
||||
:wikilinker/handler {:rcon (ig/ref :rcon/connection)
|
||||
:dictionary-file "words.gz"}})
|
||||
|
||||
|
||||
(defmethod ig/init-key :rcon/connection [_ {:keys [ip port password]}]
|
||||
(rcon-connect ip port password))
|
||||
|
||||
(defmethod ig/halt-key! :rcon/connection [_ conn]
|
||||
(.close conn))
|
||||
|
||||
(defmethod ig/init-key :console/watcher [_ {:keys [log-file handler]}]
|
||||
(hawk/watch! [{:paths [(str log-file)]
|
||||
:handler
|
||||
(fn [_ctx _ev]
|
||||
(try (handler)
|
||||
(catch Exception e
|
||||
(l/errorf e "exception in handler")
|
||||
(throw e))))}]))
|
||||
|
||||
(defmethod ig/halt-key! :console/watcher [_ watcher]
|
||||
(hawk/stop! watcher))
|
||||
|
||||
(defmethod ig/init-key :wikilinker/handler [_ {:keys [rcon watcher]}]
|
||||
(binding [*rcon* rcon]
|
||||
(bound-fn [] (handler))))
|
||||
|
||||
|
||||
|
||||
(defn -main []
|
||||
(binding [*rcon* (rcon-connect "127.0.0.1" 27015 "monitor")]
|
||||
(start-logger!)
|
||||
(start-watcher!)
|
||||
(read-line)))
|
||||
(let [system (ig/init config)]
|
||||
(.addShutdownHook (Runtime/getRuntime)
|
||||
(Thread. #(do (ig/halt! system)
|
||||
(binding [*out* *err*]
|
||||
(println "shutting down")))))))
|
||||
|
||||
Reference in New Issue
Block a user