Compare commits
No commits in common. "1890f14f9ea3c600e0c52a1d128735c6916a60a5" and "5178ab7366e2f32c611346a62bb827e80a0db25a" have entirely different histories.
1890f14f9e
...
5178ab7366
5 changed files with 41 additions and 40 deletions
|
@ -78,8 +78,6 @@
|
|||
boards))))
|
||||
|
||||
(defn config-fill-board-defaults
|
||||
;; TODO: must have check that if board is default, it's enabled, if it's not, give some big fat warning
|
||||
;; that users must always specify board, maybe change the error?
|
||||
"Fills every enabled board with default config values"
|
||||
[config]
|
||||
(let [defaults (:boards-defaults config)]
|
||||
|
|
|
@ -72,7 +72,6 @@
|
|||
(let [config (conf/get-some-config (:config options))]
|
||||
;; TODO: probably refactor to use separate config.clj file when validation will be added
|
||||
;; Init the few globals we have
|
||||
;; TODO: this all needs to go in separate function so it doesnt have to duplicated in repl-main
|
||||
(reset! conf/GLOBAL-CONFIG config)
|
||||
(reset! feed/boards-enabled-cache (set (keys (get config :boards-enabled))))
|
||||
(reset! watcher/chod-threads-cache (watcher/generate-chod-cache-structure config))
|
||||
|
@ -84,12 +83,6 @@
|
|||
(defn repl-main
|
||||
"Development entry point"
|
||||
[]
|
||||
(let [config (conf/get-some-config nil)]
|
||||
;; TODO: probably refactor to use separate config.clj file when validation will be added
|
||||
;; Init the few globals we have
|
||||
(reset! conf/GLOBAL-CONFIG config)
|
||||
(reset! feed/boards-enabled-cache (set (keys (get config :boards-enabled))))
|
||||
(reset! watcher/chod-threads-cache (watcher/generate-chod-cache-structure config)))
|
||||
(jetty/run-jetty (rp/wrap-params #'feed/http-handler)
|
||||
{:port (:port conf/CONFIG-DEFAULT)
|
||||
;; Dont block REPL thread
|
||||
|
|
|
@ -35,8 +35,8 @@
|
|||
This is done by always making new GUID - (concat thread-number UNIX-time-of-data-update)"
|
||||
[thread time]
|
||||
(assoc thread :guid (str (:no thread)
|
||||
"-"
|
||||
time)))
|
||||
"-"
|
||||
time)))
|
||||
|
||||
(defn new-guid-paranoid
|
||||
"Generate unique GUID on EVERY request to the feed.
|
||||
|
@ -64,13 +64,14 @@
|
|||
(let [filterable (select-keys query-string
|
||||
(keys known-filter-map))]
|
||||
(ut/fkmap (fn [k v]
|
||||
{(get known-filter-map k) (ut/vectorize v)})
|
||||
{(get known-filter-map k) v})
|
||||
filterable)))
|
||||
|
||||
(defn filter-chod-posts
|
||||
"Return list of all threads with equal or higher ChoD than requested
|
||||
;;resume
|
||||
READS FROM GLOBALS: watcher.time-of-cache"
|
||||
[filters chod-treshold repeat? board-cache]
|
||||
[query-vec chod-treshold repeat? board-cache]
|
||||
(let [{time-of-generation :time
|
||||
cache :data} board-cache
|
||||
guid-fn (case repeat?
|
||||
|
@ -78,24 +79,28 @@
|
|||
"true" (fn [x] (new-guid-always x time-of-generation))
|
||||
update-only-guid)
|
||||
cache-start-index (first (ut/indices (fn [x] (>= (:chod x) chod-treshold))
|
||||
cache))
|
||||
cache))
|
||||
;; So we don't have to search thru everything we have cached
|
||||
needed-cache-part (subvec cache cache-start-index)
|
||||
actuall-matches (keep (fn [thread]
|
||||
(some
|
||||
(fn [fun]
|
||||
(when (fun thread (get filters fun))
|
||||
thread))
|
||||
(keys filters)))
|
||||
;; Here we gonna run fmap but not really
|
||||
actuall-matches (keep (fn [t]
|
||||
(let [title (:title t)]
|
||||
;; Todo: Man, wouldn't it be cool to know which querry matched the thread?
|
||||
;; Would be so much easier for user to figure out why is it showing
|
||||
;; and it would solve the problem of super long titles (or OPs instead of titles)
|
||||
(when (some (fn [querry]
|
||||
(s/includes? (s/lower-case title) (s/lower-case querry)))
|
||||
query-vec)
|
||||
t)))
|
||||
(reverse needed-cache-part))]
|
||||
;; Finally generate and append GUIDs
|
||||
(map guid-fn actuall-matches)))
|
||||
|
||||
(defn thread-to-rss-item
|
||||
"Converts cached thread item to feed item which can be serialized into RSS"
|
||||
[t host]
|
||||
(let [link-url (s/replace host "{threadnum}" (str (:no t)))]
|
||||
{:title (format "%.2f%% - %s" (:chod t) (:title t))
|
||||
[t host board]
|
||||
(let [link-url (s/replace host "{threadnum}" (str (:no t)))] ;Hardcode emergency bugfix
|
||||
{:title (format "%.2f%% - %s" (:chod t) (:title t)) ;TODO: Generate link from the target somehow, or just include it from API response
|
||||
;; :url link-url <- this is supposed to be for images according to: https://cyber.harvard.edu/rss/rss.html
|
||||
:description (format "The thread: '%s' has %.2f%% chance of dying" (:title t) (:chod t))
|
||||
:link link-url
|
||||
|
@ -103,8 +108,9 @@
|
|||
|
||||
(defn generate-feed
|
||||
"Generates feed from matching items"
|
||||
[filters chod-treshold repeat? cache board-config self-link]
|
||||
(let [items (filter-chod-posts filters chod-treshold repeat? cache)
|
||||
[query-vec chod-treshold repeat? cache board-config self-link]
|
||||
(let [items (filter-chod-posts query-vec chod-treshold repeat? cache)
|
||||
served-filename (get @conf/GLOBAL-CONFIG :served-filename)
|
||||
head {:title (str "RSS Thread watcher v" conf/VERSION)
|
||||
;; :link is the homepage of the channel
|
||||
:link (get @conf/GLOBAL-CONFIG :homepage)
|
||||
|
@ -135,17 +141,22 @@
|
|||
query :query-string
|
||||
scheme :scheme
|
||||
server-name :server-name} rqst
|
||||
filters (make-filters prms f/known-filters)
|
||||
;; BUG if local fileserver not running -> FileNotFound exception is thrown and it fucks up the feed generation
|
||||
;; Should be handled because wrong config and thus url generation could do the same
|
||||
self-uri (str (s/replace-first scheme ":" "") ;
|
||||
filters (make-filters prms)
|
||||
;; qrs (prms "q")
|
||||
self-uri (str (s/replace-first scheme ":" "")
|
||||
"://" server-name uri "?" query)
|
||||
;; queries (if (vector? qrs) qrs [qrs]) ; to always return vector
|
||||
real-chod (if-let [ch (or (and (vector? chod)
|
||||
(first chod))
|
||||
chod)]
|
||||
(try ;If we can't parse number from chod, use default 94
|
||||
(if (or (vector? chod)
|
||||
;; TODO: Do we seriously parse this twice?
|
||||
(<= (Integer/parseInt chod) 60)) ; Never accept chod lower than 60 TODO: don't hardcode this
|
||||
60 (Integer/parseInt chod))
|
||||
(catch Exception e
|
||||
94)))
|
||||
board-config (get-in @conf/GLOBAL-CONFIG [:boards-enabled board])
|
||||
real-chod (try (max (Integer/parseInt (or (and (vector? chod)
|
||||
(first chod))
|
||||
chod)) 60) ;HARDCODED CHoD
|
||||
(catch Exception _
|
||||
(get board-config :default-chod)))
|
||||
cache @watcher/chod-threads-cache]
|
||||
(println "\n\nRCVD: " rqst)
|
||||
;; (println rqst)
|
||||
|
@ -182,7 +193,7 @@
|
|||
;; There shouldn't be any problems with this mime type but if there are
|
||||
;; replace with "text/xml", or even better, get RSS reader that is not utter shit
|
||||
:header {"Content-Type" "application/rss+xml"}
|
||||
:body (generate-feed filters real-chod repeat? (watcher/get-thread-data board @conf/GLOBAL-CONFIG) board-config self-uri)})
|
||||
:body (generate-feed queries real-chod repeat? (watcher/get-thread-data board @conf/GLOBAL-CONFIG) board-config self-uri)})
|
||||
(catch Exception e
|
||||
;; Ex-info has been crafted to match HTTP response body so we can send it
|
||||
(if-let [caught (ex-data e)]
|
||||
|
|
|
@ -20,15 +20,15 @@
|
|||
|
||||
(defn case-sensitive-filter
|
||||
"Returns true if string [s] is matched by any query. It's case insensitive"
|
||||
[{:keys [title]} queries]
|
||||
[s queries]
|
||||
(some (fn [querry]
|
||||
(cs/includes? title querry))
|
||||
(cs/includes? s querry))
|
||||
queries))
|
||||
|
||||
(defn case-insensitive-filter
|
||||
"Returns true if string [s] is case-matched by query"
|
||||
[{:keys [title]} queries]
|
||||
(case-sensitive-filter {:title (cs/lower-case title)} (map cs/lower-case queries)))
|
||||
[s queries]
|
||||
(case-sensitive-filter (cs/lower-case s) (map cs/lower-case queries)))
|
||||
|
||||
(def known-filters
|
||||
{"Q" case-sensitive-filter
|
||||
|
|
|
@ -74,7 +74,6 @@
|
|||
{k (map-apply-defaults conf-val default-val)}
|
||||
{k (nil?-else conf-val default-val)})))))
|
||||
|
||||
;; This is a shitty version of reduce-kv
|
||||
(defn fmap
|
||||
"Applies function [f] to every key and value in map [m]
|
||||
Function signature should be (f [key value]).
|
||||
|
|
Loading…
Reference in a new issue