;; Copyright (C) 2023 Felisp ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Affero General Public License as published by ;; the Free Software Foundation, version 3 of the License. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU Affero General Public License for more details. ;; ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . (ns rss-thread-watch.feed-generator "Generates feed for requests" (:require [ring.middleware.params :as rp] [clj-rss.core :as rss] [clojure.string :as s] [rss-thread-watch.watcher :as cache]) (:gen-class)) (defn indices ;; https://stackoverflow.com/questions/8641305/find-index-of-an-element-matching-a-predicate-in-clojure "Returns indexes of elements passing predicate" [pred coll] (keep-indexed #(when (pred %2) %1) coll)) (defn new-guid-always "Generates always unique GUID for Feed item. This is used when user wants notification on every server update, no matter whether thread actually changed it's position. 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))) (defn update-only-guid "Generates new GUID for feed item ONLY if the threads ChoD increased This is done by concating thread-number and it's rounded chod" [thread] (assoc thread :guid (format "%d-%.2f" (:no thread) :chod thread))) (defn filter-chod-posts "Return list of all threads with equal or higher ChoD than requested" [query-vec chod-treshold repeat?] (let [time-of-generation (System/currentTimeMillis) guid-fn (if repeat? (fn [x] (new-guid-always x time-of-generation)) update-only-guid) cache-start-index (first (indices (fn [x] (>= (:chod x) chod-treshold)) @cache/chod-threads-cache)) ;; So we don't have to search thru everything we have cached needed-cache-part (subvec @cache/chod-threads-cache cache-start-index) ;Todo: remove that ugly global reference 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? title querry)) query-vec) t))) needed-cache-part)] ;; Finally generate and append GUIDs (map guid-fn actuall-matches))) (defn thread-to-rss-item "If I wasnt retarded I could have made the cached version look like rss item already but what can you do. I'll refactor I promise, I just need this done ASAP" ;Todo: do what the docstring says [t] (let [link-url (str "https://boards.4chan.org/mlp/thread/" (:no t))] ; jesus, well I said only /mlp/ is supported now so fuck it {:title (format "%.2f%% - %s" (:chod t) (:title t)) ;; :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 :guid (:guid t)})) (defn generate-feed [query-vec chod-treshold repeat?] (let [items (filter-chod-posts query-vec chod-treshold repeat?) head {:title "RSS Thread watcher v0.1" :link "https://tools.treebrary.org/thread-watcher/feed.xml" :feed-url "https://tools.treebrary.org/thread-watcher/feed.xml" :description "RSS based thread watcher"} body (map thread-to-rss-item items)] (rss/channel-xml head body))) (defn http-handler "Handles HTTP requests, returns generated feed READS FROM GLOBALS: rss-thread-watch.watcher.chod-threads-cache rss-thread-watch.core.CONFIG" [rqst] {:status 200 ;; 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 "All pony here ^:)"})