Implement config placeholders for more flexible target and host urls

This commit is contained in:
Felisp 2024-09-08 02:36:16 +02:00
parent 6c825bcaaa
commit 373f2f2996

View file

@ -15,6 +15,7 @@
(ns rss-thread-watch.core (ns rss-thread-watch.core
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.string :as s]
[clojure.tools.cli :refer [parse-opts]] [clojure.tools.cli :refer [parse-opts]]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[ring.middleware.params :as rp] [ring.middleware.params :as rp]
@ -35,8 +36,8 @@
:boards-defaults {:refresh-rate 300 :boards-defaults {:refresh-rate 300
:starting-page 7 :starting-page 7
:default-chod 94 :default-chod 94
:target "https://api.4chan.org" :target "https://api.4chan.org/{board}/catalog.json"
:host "https://boards.4chan.org" :host "https://boards.4chan.org/{board}/thread/{threadnum}"
:lazy-load true} :lazy-load true}
:boards-enabled {"/mlp/" {:lazy-load false} :boards-enabled {"/mlp/" {:lazy-load false}
"/g/" {:starting-page 7} "/g/" {:starting-page 7}
@ -76,17 +77,31 @@
(with-open [r (io/reader fl)] (with-open [r (io/reader fl)]
(edn/read (java.io.PushbackReader. r)))))) (edn/read (java.io.PushbackReader. r))))))
(defn config-url-expand
"Expands substitution in :target and :host fields"
[filled-config]
(let [boards (get filled-config :boards-enabled)
selecting '(:target :host)
pattern "{board}"]
(u/fmap (fn [board confs]
(->> (select-keys confs selecting)
(u/fmap (fn [k v]
(s/replace v pattern (s/replace board "/" ""))))
(merge confs)))
boards)))
(defn config-fill-board-defaults (defn config-fill-board-defaults
"Fills every enabled board with default config values" "Fills every enabled board with default config values"
[config] [config]
(let [defaults (:boards-defaults config)] (let [defaults (:boards-defaults config)]
(dissoc (update-in config (-> config
'(:boards-enabled) (update-in '(:boards-enabled)
(fn [mp] (fn [mp]
(u/fmap (fn [k v] (u/fmap (fn [k v]
(assoc (u/map-apply-defaults v defaults) :name k)) (assoc (u/map-apply-defaults v defaults) :name k))
mp))) mp)))
:boards-defaults))) (dissoc :boards-defaults)
(config-url-expand))))
(defn get-some-config (defn get-some-config
"Attempts to get config somehow, "Attempts to get config somehow,